Reputation: 301
I'm using mocha and chai to perform some unit testing of an AngularJS service. The service has different functions and each function returns a promise.
The problem I'm facing is that the test is not waiting for the promise value to be resolved before asserting. I know done() callback can be used with mocha. So, I've tried using that. But that's giving me an error as well. Here's the code:
describe('Service Test', function() {
var factory;
beforeEach(module('Test'));
beforeEach(inject(function(_QueryService_){
factory = _QueryService_;
})
);
it('should check simpleQuery method',function(done){
var promise = factory.query("args");
var value;
promise.then(function(data){
value = data;
assert.equal(1,2);
done();
}, function(error){
assert.equal(3,4);
done();
});
});
});
So the problem right now is that the test is not failing (as it should). Instead, it just times out and gives me an error:"timeout of 2000ms exceeded. Ensure the done() callback is being called in this test"
And if I don't include done callback then the test passes because it doesn't even evaluate the condition.
Can someone suggest a fix? Thanks!
Upvotes: 0
Views: 120
Reputation: 2660
I do testing in Node.js using chai and mocha. The majority of what I test are promises. Here is some sample code to get you started.
var chai = require('chai'),
expect = chai.expect;
chai.use(require('chai-things'))
.use(require('chai-as-promised'));
chai.should();
describe(' Testing:', function() {
describe('#getBlah', function() {
describe('If blah', function() {
it('Should should blah', function() {
return service.getBlahAsync(foo, bar).should.eventually.have.length(0);
});
});
describe('If blahblah', function() {
it('Should should blahblah', function() {
return service.getBlahAsync(foo, bar).should.eventually.all.have.property(foobarbaz);
});
});
You can also do things like increasing the timeout duration of the test. Documentation for that can be found here.
Upvotes: 1