node.js - Supertest/mocha done parameter passed within tests -
below code wrote mocha, chai , supertest. have question regards segment of code below works, focus on token.
describe('authenticated usertest', function () { var token; before(function loginauth(done) { request(app) .post("/login/local") .send("username=testname") .send("password=qwe123qwe") .expect(function (res) { should.exist(res.body.token); token = res.body.token; }) .end(done); }); it('should give me defined token', function(done) { console.log("token " + token); done(); }); });
apparently, token defined , here. however, when remove done function follows:
describe('authenticated usertest', function () { var token; before(function loginauth() { //done removed here request(app) .post("/login/local") .send("username=testname") .send("password=qwe123qwe") .expect(function (res) { should.exist(res.body.token); token = res.body.token; }) .end(); //done removed here }); it('should give me defined token', function(done) { console.log("token " + token); done(); }); });
token becomes undefined. understand, done function passed down before hook various tests thereafter starts it(...)
inbuilt source code.
thus, want clarify particular question (if done passed across tests; if done accepts err parameter) , why did token become undefined after removing done parameter?
thank you.
token didn't become undefined... @ point try use it, still undefined. token
has not yet been set because mocha not know working async test.
see https://justinbellamy.com/testing-async-code-with-mocha/
Comments
Post a Comment