mocha/chai: assert thrown error

You want to test a func­tion which trhwos an error. But you can not catch that func­tion inside the chai assertions.

One way to fix this is to wrap your func­tion inside an anony­mous function.


class SystemUnderTest {

    run() {
        throw new Error('I always puke!');
    }
}

module.exports = SystemUnderTest;



const SUT = require('../../utils/test.js'); ... it('catching thrown errors', () => { let sut = new SUT(); // expect(sut.run()).to.throw(); // FAILS expect(() => sut.run()).to.throw(); // PASS });

You may also like...