Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
177
rated 0 times [  178] [ 1]  / answers: 1 / hits: 37567  / 9 Years ago, tue, november 17, 2015, 12:00:00

In JUnit you can fail a test by doing:



fail(Exception not thrown);


What's the best way to achieve the same using Chai.js?


More From » node.js

 Answers
2

There are many ways to fake a failure – like the assert.fail() mentioned by @DmytroShevchenko –, but usually, it is possible to avoid these crutches and express the intent of the test in a better way, which will lead to more meaningful messages if the tests fail.



For instance, if you expect a exception to be thrown, why not say so directly:



expect( function () {
// do stuff here which you expect to throw an exception
} ).to.throw( Error );


As you can see, when testing exceptions, you have to wrap your code in an anonymous function.



Of course, you can refine the test by checking for a more specific error type, expected error message etc. See .throw in the Chai docs for more.


[#64375] Friday, November 13, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
anniejulietteb

Total Points: 740
Total Questions: 125
Total Answers: 97

Location: Benin
Member since Fri, Mar 24, 2023
1 Year ago
;