Ensure that done() callback is being called in this Mocha test - node.js

Looking at other questions, can't really find the cause of the problem. I am trying to test using mocha.
it("Should not do the work",function(done) {
axios
.post("x/y",{ id:a2 })
.then(function(res) {
assert(false,"Should not do the work");
done();
})
.catch(function(res) {
assert.equal(HttpStatus.CONFLICT,res.status);
done();
});
});
it("Should do the work",function(done) {
axios
.post("/x/y",{ id: a1 })
.then(function(res) {
done();
})
.catch(done);
});
The result was:
√ Should not do the work (64ms)
1) Should do the work
1 passing (20s)
1 failing
1) Error: Timeout of 20000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
Increasing the timeout didn't work.

Don't forget you can simply return a promise in Mocha and it will deal with it accordingly. In your first example are you sure those blocks are actually executed?
Doing an assertion may cause an exception, which can sabotage what you're trying to do. If your promise library supports it you can always:
it("Should not do the work",function(done) {
axios.post("x/y",{ id:a2 })
.then(function(res) {
assert(false,"Should not do the work");
})
.catch(function(res) {
assert.equal(HttpStatus.CONFLICT,res.status);
})
.finally(done);
});
Ensuring that should be done regardless.
Even better:
it("Should not do the work",function() {
return axios.post("x/y",{ id:a2 })
.then(function(res) {
assert(false,"Should not do the work");
})
.catch(function(res) {
assert.equal(HttpStatus.CONFLICT,res.status);
})
});
Watch for doing catches on assertions and assertions in catches. A better plan might be async:
it("Should not do the work", async function() {
var res = await axios.post("x/y",{ id:a2 })
assert.equal(HttpStatus.CONFLICT,res.status);
});

Related

Mocha unit test for Promise throws error

I'm trying to unit test a promise. Here is the code :
it('it should return some 10 user data with ok status code when called with url ', (done) => {
return user.getUsers('https://jsonplaceholder.typicode.com/users')
.then((response) => {
console.log('me here')
assert.equal(JSON.parse(response).length, 10)
})
.catch((err)=>{
console.log('me in error')
assert.fail('err')
})
})
The above code when run throws the following error :
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (C:\Users\ajay\jay-workspace\UniTestModule\test\user.test.js)
done isn't called, this results in test timeout.
Mocha natively supports promises. done shouldn't be used when there are promises; instead, promises should be returned:
it('it should return some 10 user data with ok status code when called with url ', () => {
return user.getUsers('https://jsonplaceholder.typicode.com/users')
.then((response) => {
console.log('me here')
assert.equal(JSON.parse(response).length, 10)
});
})
Rejected promise will fail the test, no need for assert.fail as well.
You should call done() callback when using async testing (for details check https://mochajs.org/#asynchronous-code).
it('it should return some 10 user data with ok status code when called with url ', (done) => {
user.getUsers('https://jsonplaceholder.typicode.com/users')
.then((response) => {
console.log('me here')
assert.equal(JSON.parse(response).length, 10);
done();
})
.catch((err)=>{
console.log('me in error')
assert.fail('err');
done(err);
})
You should use --timeout hook and increase the number of ms like ./node_module/.bin/mocha test/ --timeout=5000
OR you can add this.timeout(5000) in the test case body
it('it should return some 10 user data with ok status code when called with url ', (done) => {
user.getUsers('https://jsonplaceholder.typicode.com/users')
.then((response) => {
console.log('me here')
assert.equal(JSON.parse(response).length, 10);
this.timeout(5000);
setTimeout(done, 3000);
})
https://mochajs.org/#test-level

Test will not call done() when failing

I am writing a unit test to test my postgres schema. I am using node-pg, mocha, sinon, and chai.
This works - the test passes with no issues:
describe('When adding a user', ()=> {
it('should reject since email is used somewhere else', (done)=> {
pool.query(`INSERT INTO users(email, id, token)
VALUES($1, $2, $3)`, ['foo#email.com', '12346', 'fooToken'])
.then((result)=> {
console.log('nothing in here runs, you will not see this');
done()
})
.catch((result) => {
result.constraint.should.have.string('email_already_exists');
done();
})
})
});
But to make sure I am not getting a false positive, I change the assert to result.constraint.should.not.have.string('email_already_exists'); to purposely make the test fail.
Instead of the test failing, I get Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test..
What am I getting this?
If you would still like to use Promises for this, the problem is that unhandled exceptions in Promises are unfortunately not propagated but rather are silently ignored. As a result, no one calls Mocha's done method, leading to the timeout.
Attaching a listener to Node's unhandledRejection event as documented here should demonstrate this.
If you modify your original code and add a call to the Promise's done method (this isn't Mocha's done method!), then you'll be able to catch all errors and pass them to Mocha's done method:
it('tests something', done => {
pool.query('...')
.then(result => {
// ...
})
.catch(result => {
// assertion that fails
})
.done(result => {}, error => { done(error); });
});
Note that Promise.done() isn't (yet) part of the standard, but is nonetheless supported by many implementations. See for example here.
Answer:
The promise chain for node-pg causes this strange issue during testing. If I work off of callback, then no issue:
describe('When adding a user', ()=> {
it('should reject since email is used somewhere else', (done)=> {
function callback(err, result) {
err.constraint.should.not.have.string('email_already_exists');
done();
}
pool.query(`INSERT INTO users(email, id, token)
VALUES($1, $2, $3)`, ['foo#email.com', '12346', 'fooToken'], callback)
})
});

How to check assertion error in Mocha when testing async code

When testing async code with Mocha and one of my asserts fails, all Mocha does is to report a timeout error. Is there a way to improve this? How to know what asserts failed and why?
mocha
Contact
#getContacts()
1) should return at least 1 contact
0 passing (3s)
1 failing
1) Contact #getContacts() should return at least 1 contact:
Error: timeout of 3000ms exceeded. Ensure the done() callback is being called in this test.
Code:
var assert = require("assert");
var contact = require("../lib/contact.js");
var chai = require('chai');
var should = chai.should();
describe('Contact', function() {
describe('#getContacts()', function() {
it('should return at least 1 contact', function(done) {
contact.getContacts().then(function(contacts) {
assert.equal(4,2)
done()
});
})
})
});
The issue is that the assertion fails, which throws an exception. This causes the promise to be rejected, but there isn't anyone to notice. Your code only checks if the promise succeeds. If you return the promise, then mocha will check for it and fail the test if the promise is rejected.
So you want
it('should return at least 1 contact', function() {
return contact.getContacts().then(function(contacts) {
assert.equal(4,2);
});
});
You should return the promise like this:
it('should return at least 1 contact', function() {
return contact.getContacts().then(function(contacts) {
assert.equal(4,2);
});
});
It seems like when the assert throws an error that error is swallowed and never shown and also the code after assert throws is skipped.
Try like this (catching the reject):
it('should return at least 1 contact', function(done) {
contact.getContacts().then(function(contacts) {
assert.equal(4,2)
done()
}).then(null, function (err) {
console.error(err);
done(err);
});
})
Or instead of then(null, rejectFunc) use catch(rejectFunc) with libs like bluebird.
Also the answer by idbehold is great. I didn't know yet that mocha supports promises directly and I always use the done param knowing if I have a timeout without a stack trace there was a swallowed error in this test.

How do I explicitly fail a unit test using should.js

I'm using Mocha and Should.js to test a promise that I am expecting to generate an error.
Because it is a promise, I don't believe I can simply use should.throwError(). This just means that I would like to fail the unit test in the .catch block of the promise.
How do I explicitly fail a unit test using without using some sort of stupid hack like 1.should.equal(2)?
Example Code (Which does not work)
it('should throw an error.', function(done) {
myPromiseGenerator().then(function() {
should.fail();
done();
}).catch(function(e) {
done();
})
}
Pass the error to the done function.
it('should throw an error.', function(done) {
myPromiseGenerator().then(function() {
done(new Error("should not succeed"));
}).catch(function(e) {
done();
})
}

In mocha testing while calling asynchronous function how to avoid the timeout Error: timeout of 2000ms exceeded

In my node application I'm using mocha to test my code. While calling many asynchronous functions using mocha, I'm getting timeout error (Error: timeout of 2000ms exceeded.). How can I resolve this?
var module = require('../lib/myModule');
var should = require('chai').should();
describe('Testing Module', function() {
it('Save Data', function(done) {
this.timeout(15000);
var data = {
a: 'aa',
b: 'bb'
};
module.save(data, function(err, res) {
should.not.exist(err);
done();
});
});
it('Get Data By Id', function(done) {
var id = "28ca9";
module.get(id, function(err, res) {
console.log(res);
should.not.exist(err);
done();
});
});
});
You can either set the timeout when running your test:
mocha --timeout 15000
Or you can set the timeout for each suite or each test programmatically:
describe('...', function(){
this.timeout(15000);
it('...', function(done){
this.timeout(15000);
setTimeout(done, 15000);
});
});
For more info see the docs.
I find that the "solution" of just increasing the timeouts obscures what's really going on here, which is either
Your code and/or network calls are way too slow (should be sub 100 ms for a good user experience)
The assertions (tests) are failing and something is swallowing the errors before Mocha is able to act on them.
You usually encounter #2 when Mocha doesn't receive assertion errors from a callback. This is caused by some other code swallowing the exception further up the stack. The right way of dealing with this is to fix the code and not swallow the error.
When external code swallows your errors
In case it's a library function that you are unable to modify, you need to catch the assertion error and pass it onto Mocha yourself. You do this by wrapping your assertion callback in a try/catch block and pass any exceptions to the done handler.
it('should not fail', function (done) { // Pass reference here!
i_swallow_errors(function (err, result) {
try { // boilerplate to be able to get the assert failures
assert.ok(true);
assert.equal(result, 'bar');
done();
} catch (error) {
done(error);
}
});
});
This boilerplate can of course be extracted into some utility function to make the test a little more pleasing to the eye:
it('should not fail', function (done) { // Pass reference here!
i_swallow_errors(handleError(done, function (err, result) {
assert.equal(result, 'bar');
}));
});
// reusable boilerplate to be able to get the assert failures
function handleError(done, fn) {
try {
fn();
done();
} catch (error) {
done(error);
}
}
Speeding up network tests
Other than that I suggest you pick up the advice on starting to use test stubs for network calls to make tests pass without having to rely on a functioning network. Using Mocha, Chai and Sinon the tests might look something like this
describe('api tests normally involving network calls', function() {
beforeEach: function () {
this.xhr = sinon.useFakeXMLHttpRequest();
var requests = this.requests = [];
this.xhr.onCreate = function (xhr) {
requests.push(xhr);
};
},
afterEach: function () {
this.xhr.restore();
}
it("should fetch comments from server", function () {
var callback = sinon.spy();
myLib.getCommentsFor("/some/article", callback);
assertEquals(1, this.requests.length);
this.requests[0].respond(200, { "Content-Type": "application/json" },
'[{ "id": 12, "comment": "Hey there" }]');
expect(callback.calledWith([{ id: 12, comment: "Hey there" }])).to.be.true;
});
});
See Sinon's nise docs for more info.
If you are using arrow functions:
it('should do something', async () => {
// do your testing
}).timeout(15000)
A little late but someone can use this in future...You can increase your test timeout by updating scripts in your package.json with the following:
"scripts": {
"test": "test --timeout 10000" //Adjust to a value you need
}
Run your tests using the command test
For me the problem was actually the describe function,
which when provided an arrow function, causes mocha to miss the
timeout, and behave not consistently. (Using ES6)
since no promise was rejected I was getting this error all the time for different tests that were failing inside the describe block
so this how it looks when not working properly:
describe('test', () => {
assert(...)
})
and this works using the anonymous function
describe('test', function() {
assert(...)
})
Hope it helps someone, my configuration for the above:
(nodejs: 8.4.0, npm: 5.3.0, mocha: 3.3.0)
My issue was not sending the response back, so it was hanging. If you are using express make sure that res.send(data), res.json(data) or whatever the api method you wanna use is executed for the route you are testing.
Make sure to resolve/reject the promises used in the test cases, be it spies or stubs make sure they resolve/reject.

Resources