• 0

mocha/chai: assert thrown error

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

One way to fix this is to wrap your function inside an anonymous 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 });