Is there a way to run a piece of code when an assertion fails inside an end block?
See original GitHub issueI find myself doing this in my tests:
chai.request(app)
.put('/user/me')
.send({ password: '123', confirmPassword: '123' })
.end(function (err, res) {
try {
expect(err).to.be.null;
expect(res).to.have.status(200);
} catch (e) {
//write err and res objects to custom log file
throw e;
}
});
I add try and catch blocks around the chai assertion checking code so that I can log out the error and response objects to help me debug a test failure. Is there another way to do this? Thanks.
Issue Analytics
- State:
- Created 7 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
How to continue execution after Assert.fail in catch block
If you want to continue your execution even after assertion failure, use soft assert instead of assert (Its called Hard Assert).
Read more >Programming With Assertions - Oracle Help Center
This document shows you how to program with assertions. It covers the topics: Introduction; Putting Assertions Into Your Code; Compiling Files That Use...
Read more >How to catch an assertion error in Java - Tutorialspoint
In order to catch the assertion error, we need to declare the assertion statement in the try block with the second expression being...
Read more >Python's assert: Debug and Test Your Code Like a Pro
In this tutorial, you'll learn how to use Python's assert statement to document, debug, and test code in development. You'll learn how ......
Read more >Assert Fail and continue question | Telerik Forums
Hi Cher, The only way for a coded step to be marked Fail is to allow the exception to happen and let Test...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The error object is the
e
in thecatch
.With chai http, the response object is attached to the error. A small exaple:
Yup, in Mocha you need to either return the promise, or call
done
. In my example:The
.then
is givendone
twice for good reason. If we expand this it might make more sense:You can see that the first
done
is called withundefined
and so Mocha considers that a test pass. The seconddone
is called witherror
and Mocha sees this as a test fail. If you want do do stuff in the catch, you totally can but you always need to call done:If mocha doesn’t log enough, you can always mix and match as you see fit. For example:
As long as you always throw the error, the Promise will reject and Mocha can work with it.
To summarise: with Promises you can add as many
.then
or.catch
commands as you want. If you want the whole chain to fail, then make sure you rethrow errors in yourcatch
.If you’re not working with promises or asynchronicity already like in the original poster’s example, then there’s no need to introduce them into the mix. Just use a
try
/catch
block: