I have the following message, just before a failing test:
1) "before each" hook
That is the the entire message. It is in red, which makes me think there is something wrong with the before each hook, but I'm unsure of what the error is. It could be:
- A failed timeout
- A failed assertion
- An Error being thrown
How do I know what the error is?
This particular beforeEach()
normally executes perfectly fine.
I have the following message, just before a failing test:
1) "before each" hook
That is the the entire message. It is in red, which makes me think there is something wrong with the before each hook, but I'm unsure of what the error is. It could be:
- A failed timeout
- A failed assertion
- An Error being thrown
How do I know what the error is?
This particular beforeEach()
normally executes perfectly fine.
- Could you post your beforeEach code? – victorkt Commented Mar 9, 2015 at 12:12
- @victorkohl My beforeEach code requires understanding of an external API (these are integration tests) and would distract the conversation from the question: what does this message mean? – mikemaccana Commented Mar 9, 2015 at 12:35
- If the hook is asynchronous do you call callback? – zaynetro Commented Mar 9, 2015 at 15:07
4 Answers
Reset to default 4I ran into this problem when in the beforeEach I accidentally called done() twice (I called it once at the end of the beforeEach, but also called it again via an async function called in the beforeEach).
When I ran the tests in watch mode I got the error message you described without any additional information; when I ran the tests normally I did not get any errors. I reported this on a related ticket.
How do I know what the error is?
Debug it just like you would any normal code. If you are making assertions inside a beforeEach
callback, you are abusing the framework. Assertions belong in the it
callbacks, so refactor that.
It's also probably not just forgetting to call done
because mocha has a clear error message when that happens.
Thus your code is probably throwing an uncaught exception and you can use your favorite flavor of debugging to track it down. I like running mocha with --debug-brk
and debugging with node-inspector, but some console.log
statements should also suffice. Note passing only the relevant test file to mocha and using the describe.only
or it.only
techniques can keep the test suite small and focused while you track down the root cause.
This is happening due to the time limit exceeding. In mocha, 2000ms is the maximum time allocated for an async process. So to make your code successful, you to increase the time limit It is usually done by using the code:
this.timeout(4000)
Note: you can't use the arrow function because you can't use the
this
option in arrow function.
For each of your tests, when you want to append the end call,
dont use:
.end(done())
use:
.end(done)