This question has many, many duplicates, including questions not mentioning the Chai assertion library. Here are the basics collected together:
The assertion must call the function, instead of it evaluating immediately.
assert.throws(x.y.z); // FAIL. x.y.z throws an exception, which immediately exits the // enclosing block, so assert.throw() not called. assert.throws(()=>x.y.z); // assert.throw() is called with a function, which only throws // when assert.throw executes the function. assert.throws(function () { x.y.z }); // if you cannot use ES6 at work function badReference() { x.y.z }; assert.throws(badReference); // for the verbose assert.throws(()=>model.get(z)); // the specific example given. homegrownAssertThrows(model.get, z); // a style common in Python, but not in JavaScript
You can check for specific errors using any assertion library:
Node
assert.throws(() => x.y.z); assert.throws(() => x.y.z, ReferenceError); assert.throws(() => x.y.z, ReferenceError, /is not defined/); assert.throws(() => x.y.z, /is not defined/); assert.doesNotThrow(() => 42); assert.throws(() => x.y.z, Error); assert.throws(() => model.get.z, /Property does not exist in model schema./)
Should
should.throws(() => x.y.z); should.throws(() => x.y.z, ReferenceError); should.throws(() => x.y.z, ReferenceError, /is not defined/); should.throws(() => x.y.z, /is not defined/); should.doesNotThrow(() => 42); should.throws(() => x.y.z, Error); should.throws(() => model.get.z, /Property does not exist in model schema./)
Chai Expect
expect(() => x.y.z).to.throw(); expect(() => x.y.z).to.throw(ReferenceError); expect(() => x.y.z).to.throw(ReferenceError, /is not defined/); expect(() => x.y.z).to.throw(/is not defined/); expect(() => 42).not.to.throw(); expect(() => x.y.z).to.throw(Error); expect(() => model.get.z).to.throw(/Property does not exist in model schema./);
You must handle exceptions that 'escape' the test
it('should handle escaped errors', function () { try { expect(() => x.y.z).not.to.throw(RangeError); } catch (err) { expect(err).to.be.a(ReferenceError); } });
This can look confusing at first. Like riding a bike, it just 'clicks' forever once it clicks.