使用 Mocha 测试 javascript - 如何使用 console.log 调试测试?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10666349/
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
Testing javascript with Mocha - how can I use console.log to debug a test?
提问by Nick Perkins
I am using the javascript test-runner "Mocha".
我正在使用 javascript 测试运行程序“Mocha”。
I have a test that is failing, so I would to debug it using console.log
.
我有一个测试失败,所以我会使用console.log
.
But when the tests are run, there is no output (only the test results from Mocha).
It seems like Mocha has captured and suppressed my console.log
output!
但是当测试运行时,没有输出(只有来自 Mocha 的测试结果)。似乎摩卡已经捕获并抑制了我的console.log
输出!
How can I get Mocha to show my output? (at for tests that fail)?
如何让 Mocha 显示我的输出?(对于失败的测试)?
EDIT:
编辑:
Huge apologies! — console.log
does work during tests! I must have been expecting it to suppress the output, and I didn't properly check my own code. Thanks for responding.
So... that being said... maybe it actually would be nice to suppress the output for tests that pass? hmm...
深表歉意!—console.log
在测试期间确实有效!我一定一直期待它抑制输出,而且我没有正确检查我自己的代码。感谢您的回复。所以......话虽如此......也许抑制通过测试的输出实际上会很好?唔...
On a related note: I want to use console.log
because I am having a lot of trouble trying to get the Eclipse debugger to connect to node.js.
相关说明:我想使用,console.log
因为我在尝试让 Eclipse 调试器连接到 node.js 时遇到了很多麻烦。
Am I the only one who finds this tricky? How do you guys debug node.js? With a debugger, or with console.log
statements?
只有我觉得这很棘手吗?你们如何调试node.js?用调试器,还是用console.log
语句?
采纳答案by Zach Bonham
What Mocha options are you using?
您正在使用哪些摩卡咖啡选项?
Maybe it is something to do with reporter (-R) or ui (-ui) being used?
也许这与使用的记者 (-R) 或 ui (-ui) 有关?
console.log(msg);
works fine during my test runs, though sometimes mixed in a little goofy. Presumably due to the async nature of the test run.
在我的测试运行期间工作正常,但有时会混入一点愚蠢。大概是由于测试运行的异步性质。
Here are the options (mocha.opts) I'm using:
以下是我正在使用的选项 (mocha.opts):
--require should
-R spec
--ui bdd
Hmm..just tested without any mocha.opts and console.log
still works.
嗯..只是在没有任何 mocha.opts 的情况下进行了测试并且console.log
仍然有效。
回答by Kevin C.
If you are testing asynchronous code, you need to make sure to place done()
in the callback of that asynchronous code. I had that issue when testing http requests to a REST API.
如果您正在测试异步代码,则需要确保放置done()
在该异步代码的回调中。我在测试对 REST API 的 http 请求时遇到了这个问题。
回答by qix
You may have also put your console.log
after an expectation that fails and is uncaught, so your log line never gets executed.
您可能也将您console.log
的期望设置为失败且未被捕获,因此您的日志行永远不会被执行。