So, I'm testing a component that relies on an event-emitter. To do so I came up with a solution using Promises with Mocha+Chai:
it('should transition with the correct event', (done) => {
const cFSM = new CharacterFSM({}, emitter, transitions);
let timeout = null;
let resolved = false;
new Promise((resolve, reject) => {
emitter.once('action', resolve);
emitter.emit('done', {});
timeout = setTimeout(() => {
if (!resolved) {
reject('Timedout!');
}
clearTimeout(timeout);
}, 100);
}).then((state) => {
resolved = true;
assert(state.action === 'DONE', 'should change state');
done();
}).catch((error) => {
assert.isNotOk(error,'Promise error');
done();
});
});
On the console I'm getting an 'UnhandledPromiseRejectionWarning' even though the reject function is getting called since it instantly shows the message 'AssertionError: Promise error'
(node:25754) UnhandledPromiseRejectionWarning: Unhandled promise
rejection (rejection id: 2): AssertionError: Promise error: expected
{ Object (message, showDiff, ...) } to be falsy
should transition with the correct event
And then, after 2 sec I get
Error: timeout of 2000ms exceeded. Ensure the done() callback is
being called in this test.
Which is even weirder since the catch callback was executed(I think that for some reason the assert failure prevented the rest of the execution)
Now the funny thing, if I comment out the assert.isNotOk(error...) the test runs fine without any warning in the console. It stills 'fails' in the sense that it executes the catch.
But still, I can't understand these errors with promise. Can someone enlighten me?
The issue is caused by this:
.catch((error) => {
assert.isNotOk(error,'Promise error');
done();
});
If the assertion fails, it will throw an error. This error will cause done() never to get called, because the code errored out before it. That's what causes the timeout.
The "Unhandled promise rejection" is also caused by the failed assertion, because if an error is thrown in a catch() handler, and there isn't a subsequent catch() handler, the error will get swallowed (as explained in this article). The UnhandledPromiseRejectionWarning warning is alerting you to this fact.
In general, if you want to test promise-based code in Mocha, you should rely on the fact that Mocha itself can handle promises already. You shouldn't use done(), but instead, return a promise from your test. Mocha will then catch any errors itself.
Like this:
it('should transition with the correct event', () => {
...
return new Promise((resolve, reject) => {
...
}).then((state) => {
assert(state.action === 'DONE', 'should change state');
})
.catch((error) => {
assert.isNotOk(error,'Promise error');
});
});
For those who are looking for the error/warning UnhandledPromiseRejectionWarning outside of a testing environment, It could be probably because nobody in the code is taking care of the eventual error in a promise:
For instance, this code will show the warning reported in this question:
new Promise((resolve, reject) => {
return reject('Error reason!');
});
(node:XXXX) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Error reason!
and adding the .catch() or handling the error should solve the warning/error
new Promise((resolve, reject) => {
return reject('Error reason!');
}).catch(() => { /* do whatever you want here */ });
Or using the second parameter in the then function
new Promise((resolve, reject) => {
return reject('Error reason!');
}).then(null, () => { /* do whatever you want here */ });
I got this error when stubbing with sinon.
The fix is to use npm package sinon-as-promised when resolving or rejecting promises with stubs.
Instead of ...
sinon.stub(Database, 'connect').returns(Promise.reject( Error('oops') ))
Use ...
require('sinon-as-promised');
sinon.stub(Database, 'connect').rejects(Error('oops'));
There is also a resolves method (note the s on the end).
See http://clarkdave.net/2016/09/node-v6-6-and-asynchronously-handled-promise-rejections
The assertion libraries in Mocha work by throwing an error if the assertion was not correct. Throwing an error results in a rejected promise, even when thrown in the executor function provided to the catch method.
.catch((error) => {
assert.isNotOk(error,'Promise error');
done();
});
In the above code the error objected evaluates to true so the assertion library throws an error... which is never caught. As a result of the error the done method is never called. Mocha's done callback accepts these errors, so you can simply end all promise chains in Mocha with .then(done,done). This ensures that the done method is always called and the error would be reported the same way as when Mocha catches the assertion's error in synchronous code.
it('should transition with the correct event', (done) => {
const cFSM = new CharacterFSM({}, emitter, transitions);
let timeout = null;
let resolved = false;
new Promise((resolve, reject) => {
emitter.once('action', resolve);
emitter.emit('done', {});
timeout = setTimeout(() => {
if (!resolved) {
reject('Timedout!');
}
clearTimeout(timeout);
}, 100);
}).then(((state) => {
resolved = true;
assert(state.action === 'DONE', 'should change state');
})).then(done,done);
});
I give credit to this article for the idea of using .then(done,done) when testing promises in Mocha.
I faced this issue:
(node:1131004) UnhandledPromiseRejectionWarning: Unhandled promise rejection (re
jection id: 1): TypeError: res.json is not a function
(node:1131004) DeprecationWarning: Unhandled promise rejections are deprecated.
In the future, promise rejections that are not handled will terminate the Node.j
s process with a non-zero exit code.
It was my mistake, I was replacing res object in then(function(res), so changed res to result and now it is working.
Wrong
module.exports.update = function(req, res){
return Services.User.update(req.body)
.then(function(res){//issue was here, res overwrite
return res.json(res);
}, function(error){
return res.json({error:error.message});
}).catch(function () {
console.log("Promise Rejected");
});
Correction
module.exports.update = function(req, res){
return Services.User.update(req.body)
.then(function(result){//res replaced with result
return res.json(result);
}, function(error){
return res.json({error:error.message});
}).catch(function () {
console.log("Promise Rejected");
});
Service code:
function update(data){
var id = new require('mongodb').ObjectID(data._id);
userData = {
name:data.name,
email:data.email,
phone: data.phone
};
return collection.findAndModify(
{_id:id}, // query
[['_id','asc']], // sort order
{$set: userData}, // replacement
{ "new": true }
).then(function(doc) {
if(!doc)
throw new Error('Record not updated.');
return doc.value;
});
}
module.exports = {
update:update
}
Here's my take experience with E7 async/await:
In case you have an async helperFunction() called from your test... (one explicilty with the ES7 async keyword, I mean)
→ make sure, you call that as await helperFunction(whateverParams) (well, yeah, naturally, once you know...)
And for that to work (to avoid ‘await is a reserved word’), your test-function must have an outer async marker:
it('my test', async () => { ...
I had a similar experience with Chai-Webdriver for Selenium.
I added await to the assertion and it fixed the issue:
Example using Cucumberjs:
Then(/I see heading with the text of Tasks/, async function() {
await chai.expect('h1').dom.to.contain.text('Tasks');
});
Just a heads-up that you can get a UnhandledPromiseRejectionWarning if you accidentally put your test code outside of the it-function. 😬
describe('My Test', () => {
context('My Context', () => {
it('should test something', () => {})
const result = testSomething()
assert.isOk(result)
})
})
I want to make an assertion in a catch block of a promise chain, but it reaches the timeout. Assertions work in then blocks, but it seems in the catch block, done() is never reached. Is it being suppressed? Is there a better way to test promise rejections?
import assert from 'assert';
import { apicall } from '../lib/remoteapi';
describe('API calls', function () {
it('should test remote api calls', function (done) {
apicall([])
.then((data) => {
assert.equal(data.items.length, 2); // this works fine
done();
})
.catch((e) => {
console.log('e', e);
assert.equal(e, 'empty array'); // ?
done(); // not reached?
});
});
});
The promise rejection
apicall(channelIds) {
if(channelIds.length === 0) return Promise.reject('empty array');
...
}
I get this error:
Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.
If this is mocha and you're using Promises, don't use the callback functionality; it makes things very awkward as you're discovering. Instead, mocha lets you return the promise in your test. A rejected Promise means a failed test, and a successful Promise means a succeeding test. A failed assertion causes the code to throw an exception, which will automatically fail the Promise, which is usually what you'll want. In short:
describe('API calls', function () {
it('should test remote api calls', function () {
return apicall([])
.then((data) => {
assert.equal(data.items.length, 2);
});
});
});
This question already has answers here:
Bluebird Each loop in Mocha not working
(2 answers)
Closed 6 years ago.
I'm new to JavaScript test frameworks. I would like to do a little optimization but I run into some problems. The project is using should.js
Here is the simplified version of my original test cases:
describe('Simple', function() {
describe('Test', function() {
it('should do something', function(done) {
somePromise.then(function(data) {
data.should.above(100);
done();
});
}
it('should do something else but alike', function(done) {
somePromise.then(function(data) {
data.should.above(100);
done();
});
}
}
});
I'm trying to do it this way:
var testFunc = function(data) {
it('should do something', function(done) {
data.should.above(100);
done();
});
}
describe('Simple', function() {
describe('Test', function() {
somePromise.then(function(data) {
testFunc(data);
});
somePromise.then(function(data) {
testFunc(data);
});
}
});
The promise is asynchronous, and maybe that's the reason why my "optimization" didn't work? I've found no "done" callback for describe function in docs.
Thanks in advance! Any help will be appreciate!
Your example does not work since Mocha has finished registering test cases when your promise resolves.
Testing the same promise with different assertions
To test a single promise using several assertions you simply need to create the promise at the start of the tests and then use it in the it blocks like the following:
describe('A module', function() {
var promise;
before(function () {
promise = createPromise();
});
it('should do something', function() {
return promise.then(function (value) {
value.should.be.above(100);
});
});
it('should do something else', function() {
return promise.then(function (value) {
value.should.be.below(200);
});
});
});
Note that if the promise is returned from an API call, the call will only be made once. The result is simply cached in the promise for the two test cases.
This also makes use of the fact that you can return promises from test cases instead of using a done callback. The test case will then fail if the promise is rejected or if any of the assertions in the then() calls fail.
Testing different promises with the same assertions
Assuming that you want to test different promises using the same assertions you could pass a function to testFunc that creates the promise to be tested.
var testFunc = function(promiseFactory) {
it('should do something', function(done) {
promiseFactory().then(function(data) {
data.should.above(100);
done();
});
});
}
describe('Simple', function() {
describe('Test', function() {
testFunc(function () { return createSomePromise(); });
testFunc(function () { return createSomeOtherPromise(); });
});
});
This works since Mocha's it function is run immediately inside the describe block. The promises are then created using the promiseFactory callback when the test cases are actually run.
As you can also return promises from test cases you can change testFunc to return an assertion as a promise like this:
var testFunc = function(promiseFactory) {
it('should do something', function() {
return promiseFactory().should.eventually.be.above(100);
});
}
I have a db.js set up to do all my database calls. This is an example of one of the functions that query the database.
db.getUserCount = function () {
return new Promise (function (resolve, reject) {
db.users.count().then(function (result) {
resolve (result);
}, function(e) {
reject (e);
});
});
};
I am pretty new to JavaScript and testing. I have used mocha and chai to test that it resolves like this:
describe('getUserCount', function() {
it('should be fulfilled when called', function() {
return db.getUserCount().should.be.fulfilled;
});
});
How can I test the reject part of the promises. Do I have to use something like sinon or is there some simple way to make the promise fail?
Make db.users.count() call to fail by either causing some change in database entry or in your api.
I ended up using sinon stubs so the other test would still pass.
describe('db.getUserCount rejection test', function() {
sinon.stub(db, 'getUserCount').returns(Q.reject(new Error(errorMessage)));
it('should be rejected when called', function() {
return db.getUserCount().should.be.rejected;
});
it.only('getUserCount responds with correct error message', function() {
return db.getUserCount().catch(function(err) {
expect(err.message).to.equal('Error could not connect to database');
});
});
});
I have a fairly straightforward test that works against an Angular promise, which I'm resolving in the beforeEach function, but the then in my code is not ever firing and I can't see what I'm missing. These are written with TypeScript, but that doesn't really have any bearing on the problem.
Here is my test
describe('Refresh->', () => {
var controller = new Directives.Reporting.ReportDirectiveController($scope, $q, $location);
var called = false;
var defer: any;
beforeEach((done) => {
controller.drillReport = (drillReport: Models.drillReport): ng.IPromise<Models.drillData> => {
defer = $q.defer();
called = true;
defer.resolve({});
return defer.promise;
};
spyOn(controller, 'processResults');
controller.refresh();
done();
});
it('Calls DrillReport', () => {
expect(called).toBeTruthy();
});
it('Calls ProcessResults', () => {
expect(controller.processResults).toHaveBeenCalled();
});
});
The Refresh method in the controller looks like this:
refresh() {
this.drillReport({ drillReport: drillReport })
.then((results: Models.drillData) => {
parent.processResults(results, parent.availableDrills, this.columns, this.gridOptions, undefined, undefined);
});
}
What you are missing is that you will need access to use $scope, or $rootScope, so that you can call and force a digest cycle...
$scope.$digest();
The reason this is needed is that the resolved and rejected promises are processed during the digest loop. So while you are resolving the promise in your mock, the actual promise callback is not being invoked.
Following up on what #Brocco said, your code is calling done() before the promise is processed.
What you need it a way for your test code to know that that parent.processResults() has been called.
I suggest you have refresh return a promise that will be resolved just after parent.processResults(), and add controller.refresh().finally(() => { done(); }); in your test code.