Velvet Star Monitor

Standout celebrity highlights with iconic style.

updates

Mocha 'before each hook' message in red. How do I know what specifically is wrong?

Writer Matthew Harrington

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.

3

4 Answers

I 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.

1

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.

1

For each of your tests, when you want to append the end call,

dont use:

.end(done())

use:

.end(done)

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy