Test for expected failure in Mocha

后端 未结 9 1968
终归单人心
终归单人心 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:22

    If you are using should.js you can do (new ErrorThrowingObject).should.throw('Option Error Text or Regular Expression here')

    If you don't want to should a separate library, you could also do something like this:

    it('should do whatever', function(done) {
        try {
            ...
        } catch(error) {
            done();
        }
    }
    

    This way, you know the error is caught if the test finishes. Otherwise, you will get a timeout error.

提交回复
热议问题