javascript Mocha 'before each hook' 消息为红色。我怎么知道具体出了什么问题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28941245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 09:48:12  来源:igfitidea点击:

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

javascriptnode.jstestingmocha

提问by mikemaccana

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.

这个特殊的beforeEach()通常执行得很好。

采纳答案by cuttlefish

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

我在 beforeEach 中不小心调用了 done() 两次时遇到了这个问题(我在 beforeEach 的末尾调用了一次,但也通过 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.

当我在监视模式下运行测试时,我收到了您描述的错误消息,但没有任何其他信息;当我正常运行测试时,我没有收到任何错误。我在相关票证上报告了这一点。

回答by Peter Lyons

How do I know what the error is?

我怎么知道错误是什么?

Debug it just like you would any normal code. If you are making assertions inside a beforeEachcallback, you are abusing the framework. Assertions belong in the itcallbacks, so refactor that.

像调试任何普通代码一样调试它。如果您在beforeEach回调中进行断言,那么您就是在滥用框架。断言属于it回调,所以重构它。

It's also probably not just forgetting to call donebecause mocha has a clear error message when that happens.

也可能不仅仅是忘记调用,done因为发生这种情况时 mocha 有一个明确的错误消息。

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-brkand debugging with node-inspector, but some console.logstatements should also suffice. Note passing only the relevant test file to mocha and using the describe.onlyor it.onlytechniques can keep the test suite small and focused while you track down the root cause.

因此,您的代码可能会抛出一个未捕获的异常,您可以使用您最喜欢的调试方式来跟踪它。我喜欢用 node-inspector运行 mocha--debug-brk调试,但一些console.log语句也应该足够了。请注意,仅将相关的测试文件传递给 mocha 并使用describe.onlyit.only技术可以在您追踪根本原因时保持测试套件小而集中。

回答by Gowtham Karnan

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:

这是由于超出时间限制而发生的。在 mocha 中,2000 毫秒是分配给异步进程的最长时间。所以为了让你的代码成功,你增加时间限制通常是通过使用代码来完成的:

this.timeout(4000)

Note: you can't use the arrow function because you can't use the

注意:你不能使用箭头函数,因为你不能使用

this

option in arrow function.

箭头函数中的选项。

回答by Steven Victor

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

对于您的每个测试,当您想要附加结束调用时,

dont use:

不要使用:

.end(done())

use:

利用:

.end(done)