如果测试失败,如何让Mocha在源文件中显示正确的行号? [英] How to make Mocha display correct line numbers in source files if a test fails?

查看:69
本文介绍了如果测试失败,如何让Mocha在源文件中显示正确的行号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Mocha进行NodeJS测试,当测试由于我的源代码抛出错误而失败时(例如 TypeError:无法读取属性'prop'的null ),显示的堆栈跟踪中的行号是错误的(它们与原始源文件不匹配,但是更大)。

I'm using Mocha for my NodeJS tests, and when a test fails due to an Error thrown by my source code (for example "TypeError: Cannot read property 'prop' of null"), the line numbers in the displayed stacktrace are wrong (they don't match with the original source file, but are far bigger).

  1) MyApp should do something:
     TypeError: Cannot read property 'prop' of null
      at MyApp.<anonymous> (/path/to/my-project/lib/my-project.js:515:93)
      at MyApp.build (/path/to/my-project/lib/my-project.js:774:16)
      at Context.<anonymous> (/path/to/my-project/test/test.js:62:67)
      at Test.Runnable.run (/path/to/my-project/node_modules/mocha/lib/runnable.js:216:15)
      at Runner.runTest (/path/to/my-project/node_modules/mocha/lib/runner.js:373:10)
      at /path/to/my-project/node_modules/mocha/lib/runner.js:451:12
      at next (/path/to/my-project/node_modules/mocha/lib/runner.js:298:14)
      at /path/to/my-project/node_modules/mocha/lib/runner.js:308:7
      at next (/path/to/my-project/node_modules/mocha/lib/runner.js:246:23)
      at Object._onImmediate (/path/to/my-project/node_modules/mocha/lib/runner.js:275:5)
      at processImmediate [as _immediateCallback] (timers.js:330:15)

(此处 my-project.js 只有279行!)

(Here my-project.js only has 279 lines !)

有没有办法告诉Mocha正确显示它们?

Is there a way to tell Mocha to display them correctly?

推荐答案

当代码由覆盖工具检测时会发生这种情况(例如空白et istanbul 等)。仔细检查以确保您没有错误地在正常测试中加载它。

This happens when the code is instrumented by a coverage tool (e.g. blanket, istanbul, etc). Double-check to make sure you aren't loading it in your normal tests by mistake.

这篇关于如果测试失败,如何让Mocha在源文件中显示正确的行号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆