Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Mocha display correct line numbers in source files if a test fails?

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)

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

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

like image 956
Anthony O. Avatar asked Sep 10 '14 16:09

Anthony O.


People also ask

What is beforeEach in mocha?

The beforeEach method is a feature (hook) in test libraries that you can use to set preconditions for each test. Just like the name suggests, it runs before each test in your test suite. For example, the beforeEach method will run four times in a suite with four tests.

How do you ignore a test in mocha?

Find out how you can ignore some tests in Mocha You can use the skip() method (on describe() or it() ) to ignore some tests: In case of describe , it will ignore all tests in the describe block (which includes all nested describe and it functions); In case of it , it will ignore the individual test.


1 Answers

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.

like image 106
SystemParadox Avatar answered Sep 23 '22 12:09

SystemParadox