Test for expected failure in Mocha

后端 未结 9 1970
终归单人心
终归单人心 2020-12-10 01:29

Using Mocha, I am attempting to test whether a constructor throws an error. I haven\'t been able to do this using the expect syntax, so I\'d like to do the following:

<
9条回答
  •  既然无缘
    2020-12-10 02:10

    With Chai throw (ES2016)

    http://chaijs.com/api/bdd/#method_throw

    For clarity... This works

    it('Should fail if ...', done => {
        let ret = () => {
            MyModule.myFunction(myArg);
        };
        expect(ret).to.throw();
        done();
    });
    

    This doesn't work

    it('Should fail if ...', done => {
        let ret = MyModule.myFunction(myArg);
        expect(ret).to.throw();
        done();
    });
    

提交回复
热议问题