I tried to write should.js style throw statements like so (new Bill(input)).should.throw(Error); but its not working. Where can I find resources wherein I can checkout documentation on should assertion style?
Our website BDD documentation is likely the best place to start: http://chaijs.com/api/bdd/ - Just select "throw" from the scrollable list on the left or Throw is the fourth from the bottom.
Alternatively, if you are looking to dive into some actual code, our test suite is rather complete. You can find the .should.throw() tests here: https://github.com/chaijs/chai/blob/master/test/should.js#L426
If you feel you Chai is not performing correctly, please provide more information, starting with what environment (node or browser), and what test runner you are using, leading into what you expected to happen vs. what actually happen.
Let me know how it goes!
My Testing environment is browser google chrome 18.x, I think there is still a problem. Check the following code
Test.js
(new Bill(input)).should.throw(Error);
Try this...
(function () {
new Bill(input);
}).should.throw(Error);
Yes, its working, thanx
throw uses try/catch so it needs a function to execute.
Let me know if you have any more questions. Good luck!
I have read above posts and tested several times. Then got following
function throwme() {
throw new Error('I am exception');
}
it('It throw exception', function() {
(function() {
throwme();
}).should.Throw('I am exception');
});
Any better idea?
Hi can any one help? it's wired that for a same function, both throw and not throw pass the test
https://jsfiddle.net/8t5bf261/
class Person {
constructor(age) {
if (Object.prototype.toString.call(age) !== '[object Number]') throw 'NOT A NUMBER'
this.age = age;
}
howold() {
console.log(this.age);
}
}
var should = chai.should();
mocha.setup('bdd');
describe('Person', function() {
it('should throw if input is not a number', function() {
(function() {
var p1 = new Person('sadf');
}).should.not.throw;
(function(){
var p2= new Person('sdfa');
}).should.throw;
})
})
mocha.run();
@sabrinaluo Try using .should.throw() and .should.not.throw() (with parenthesis).
You might also want to consider using throw Error('NOT A NUMBER') or even throw TypeError('NOT A NUMBER') in your class so that the stack is captured, though I don't think this is directly related to your issue.
@meeber thanks, it works for me!
Here's two examples with cleaner syntax that work fine for me:
it('should pass validation', function() {
should.not.throw(() => { model.fromJson(validJson) });
});
it('should fail validation', function() {
should.throw(() => { mode.fromJson(invalidJson) });
});
Most helpful comment
Try this...