最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin5浏览0评论

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.

Share Improve this question asked Mar 9, 2015 at 11:46 mikemaccanamikemaccana 123k110 gold badges427 silver badges531 bronze badges 3
  • 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
Add a comment  | 

4 Answers 4

Reset to default 4

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.

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)
发布评论

评论列表(0)

  1. 暂无评论