How to get Mocha to display the correct line numbers in the source files if the test fails?

I use Mocha for my NodeJS tests, and when the test failed due to an error generated 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 "t match the original source file, but much more).

  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) 

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

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

+5
source share
1 answer

This happens when the code is controlled by a coverage tool (e.g. blanket , istanbul , etc.). Double check to make sure you are not loading it into your regular tests by mistake.

+5
source

Source: https://habr.com/ru/post/1202260/


All Articles