Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Testing truthy / falsy values with assert/expect

when I write tests I often use such constructions with assert

 try {
   await asyncFunction(); // expect error
   assert(false) // to make 100% fail
 } catch (err) {
   assert(err) // means 'assert(true) 
 }

Now I need to use "expect" from chai lib and I don’t know how to write exactly the same test with ‘expect’ syntax

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You might try

expect.fail("This should've not happenned");

or another "more readable" alternative

should.fail("This should've not happenned");

Chai docs

In this section looks like there is a cool idiomatic way to perform what you want:

const action = async function() { await asyncFunction() }
expect(action).to.throw(YourError)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading