I am trying to set up testing in-browser using Mocha 2.4.5 and Should.js 8.3.0.
Failing Should assertions display a useful test failure reason.
However, when that same assertion is made in the context of a jQuery Ajax callback, I get an Uncaught Assertion Error instead.
Take the following example:
mocha.setup('bdd');
var url = 'http://localhost/api/v2/People';
describe('ajax', () => {
var that1 = this;
it('Passing async assertion', (done) => {
$.ajax({ url: url }).done(data => {
should(1).be.eql(1);
done();
});
});
it('Failing async assertion', (done) => {
var that2 = this;
$.ajax({ url: url }).done(data => {
should(0).be.eql(1);
done();
});
});
it('Failing sync assertion', (done) => {
should(0).be.eql(1);
done();
});
});
mocha.run();
'Passing async assertion' passes and looks pretty in the test runner output.
'Failing sync assertion' fails and I get a useful stacktrace
Fail sync assertion ‣
AssertionError: expected 0 to equal 1
at Assertion.fail (http://path/to/should.js:220:17)
at Assertion.Object.defineProperty.value (http://path/to/should.js:292:19)
at Context. (tests.js:337:26)
However, 'Failing async assertion' fails (which is fine), but the stacktrace is usless.
Fail async assertion ‣
Error: Uncaught AssertionError (http://path/to/should.js:200)
I've played around with setting the ajax context to that1 or that2, but it makes no difference.
Is there some other way to format the test cases so asynchronous assertions work?
EDIT
I get the same thing using a simple timeout to simulate the asynchronicity
mocha.setup('bdd');
describe('ajax', () => {
var that1 = this;
it('Failing async assertion', (done) => {
setTimeout(() => {
should(0).be.eql(1);
done();
}, 100);
}).async = true;
it('Failing sync assertion', (done) => {
should(0).be.eql(1);
done();
});
});
mocha.run();