I've recently picked up the JS unit test libraries Mocha, Chai and Chai-As-Promise. However, I'm running into a situation where I'm not sure if it's a default behaviour or it's something I've missed.
when I assert an error message rejected from a promise, it seems that as long as the expected error message is a substring of the actual error message, it assertion will pass. and below is an example:
var chai = require('chai');
var expect = chai.expect;
var chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);
var q = require('q');
describe('demo test', function(){
// a mock up promise function for demo purpose
function test(input){
var d = q.defer();
setTimeout(function() {
if(input){
d.resolve('12345');
}else{
// throw a new Error after half a sec here
d.reject(new Error('abcde fghij'));
}
}, (500));
return d.promise;
}
// assertion starts here
it('should pass if input is true', ()=>{
return expect(test(true)).to.eventually.equal('12345');
});
it('this passes when matching the first half', ()=>{
return expect(test(false)).to.be.rejectedWith('abcde');
});
it('this also passes when matching the second half', ()=>{
return expect(test(false)).to.be.rejectedWith('fghij');
});
it('this again passes when even only matching the middle', ()=>{
return expect(test(false)).to.be.rejectedWith('de fg');
});
it('this fails when the expected string is not a substring of the actual string', ()=>{
return expect(test(false)).to.be.rejectedWith('abcdefghij');
});
});
Is this the default behaviour? If it is, is there an option to force an exact match of the error message?
mocha@3.4.2 chai@4.0.2 chai-as-promised@7.1.1 q@1.5.0
Many Thanks.