New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mocha Illegal Token Error #90

Closed
anderslyman opened this Issue Aug 8, 2013 · 13 comments

Comments

Projects
None yet
9 participants
@anderslyman

I'm on Windows 8.
I have a spec folder containing a single test file, and when I run:

istanbul cover _mocha -- -R spec

Inside that folder, I get:

C:\Users\alyman\AppData\Roaming\npm\_mocha.CMD:1
(function (exports, require, module, __filename, __dirname) { @IF EXIST "%~dp0
                                                              ^
No coverage information was collected, exit without writing coverage information
SyntaxError: Unexpected token ILLEGAL
    at Module._compile (module.js:439:25)
    at Module._extensions..js (module.js:474:10)
    at Object.Module._extensions..js (C:\Users\alyman\AppData\Roaming\npm\node_modules\istanbul\lib\hook.js:101:13)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at runFn (C:\Users\alyman\AppData\Roaming\npm\node_modules\istanbul\lib\command\common\run-with-cover.js:86:16)
    at C:\Users\alyman\AppData\Roaming\npm\node_modules\istanbul\lib\command\common\run-with-cover.js:189:17
    at C:\Users\alyman\AppData\Roaming\npm\node_modules\istanbul\lib\util\file-matcher.js:52:16
    at C:\Users\alyman\AppData\Roaming\npm\node_modules\istanbul\lib\util\file-matcher.js:35:9
@gotwarlost

This comment has been minimized.

Show comment
Hide comment
@gotwarlost

gotwarlost Aug 8, 2013

Owner

istanbul assumes that the _mocha you specify translates to a JS file which is true in non-windows environments. In a windows environment, this is likely a batch file that is getting loaded by node and, of course, fails.

Providing the relative path to the _mocha JS file should solve this problem. As in:

$ istanbul cover node_modules/mocha/bin/_mocha -- -R spec
Owner

gotwarlost commented Aug 8, 2013

istanbul assumes that the _mocha you specify translates to a JS file which is true in non-windows environments. In a windows environment, this is likely a batch file that is getting loaded by node and, of course, fails.

Providing the relative path to the _mocha JS file should solve this problem. As in:

$ istanbul cover node_modules/mocha/bin/_mocha -- -R spec

@gotwarlost gotwarlost closed this Aug 8, 2013

@anderslyman

This comment has been minimized.

Show comment
Hide comment
@anderslyman

anderslyman Aug 8, 2013

Works great, thanks!

Works great, thanks!

@BoyCook

This comment has been minimized.

Show comment
Hide comment
@BoyCook

BoyCook Sep 17, 2013

Hi, I'm also having a similar issue when using mocha. All my tests run fine and I get a spec report in the console, but I get this message at the end:

No coverage information was collected, exit without writing coverage information

I've tried several different combinations of running the tests (as mentioned above):

istanbul cover mocha -- -R spec
istanbul cover _mocha -- -R spec
istanbul cover node_modules/mocha/bin/mocha -- -R spec
istanbul cover node_modules/mocha/bin/_mocha -- -R spec

I've also tried with the mocha-lcov-reporter (-R mocha-lcov-reporter) and get the same result. Any help will be greatly appreciated. (I'm on OS X running node v0.10.15 if it helps).

BoyCook commented Sep 17, 2013

Hi, I'm also having a similar issue when using mocha. All my tests run fine and I get a spec report in the console, but I get this message at the end:

No coverage information was collected, exit without writing coverage information

I've tried several different combinations of running the tests (as mentioned above):

istanbul cover mocha -- -R spec
istanbul cover _mocha -- -R spec
istanbul cover node_modules/mocha/bin/mocha -- -R spec
istanbul cover node_modules/mocha/bin/_mocha -- -R spec

I've also tried with the mocha-lcov-reporter (-R mocha-lcov-reporter) and get the same result. Any help will be greatly appreciated. (I'm on OS X running node v0.10.15 if it helps).

@BoyCook

This comment has been minimized.

Show comment
Hide comment
@BoyCook

BoyCook Sep 17, 2013

I've got an update. I've realised the reason why it's not working is because the tests are not using require to include the libs being tested (I'm testing 'plain JavaScript' and don't want to do module.exports).

My tests are using vm.runInThisContext(fs.readFileSync('lib.js')); to load in the source being tested. I've tried with a different project using modules and require and it works fine.

BoyCook commented Sep 17, 2013

I've got an update. I've realised the reason why it's not working is because the tests are not using require to include the libs being tested (I'm testing 'plain JavaScript' and don't want to do module.exports).

My tests are using vm.runInThisContext(fs.readFileSync('lib.js')); to load in the source being tested. I've tried with a different project using modules and require and it works fine.

@gotwarlost

This comment has been minimized.

Show comment
Hide comment
@gotwarlost

gotwarlost Sep 17, 2013

Owner

Try the --hook-run-in-context istanbul setting and see if that helps. So your command should be:

$ istanbul cover --hook-run-in-context _mocha -- -R spec
Owner

gotwarlost commented Sep 17, 2013

Try the --hook-run-in-context istanbul setting and see if that helps. So your command should be:

$ istanbul cover --hook-run-in-context _mocha -- -R spec
@EvanHongYousan

This comment has been minimized.

Show comment
Hide comment
@EvanHongYousan

EvanHongYousan Apr 10, 2015

it works ,thank you! @gotwarlost

it works ,thank you! @gotwarlost

@DevinXian

This comment has been minimized.

Show comment
Hide comment
@DevinXian

DevinXian Aug 4, 2015

Thank you, it works!@gotwarlost

Thank you, it works!@gotwarlost

harriha added a commit to harriha/knex that referenced this issue Aug 30, 2015

npm test to use relative path to mocha binary
Makes the command work on Windows as well, see more info from
gotwarlost/istanbul#90 (comment)

blah238 added a commit to blah238/knex that referenced this issue Oct 12, 2015

NPM test script improvements
  - Add 'npm run babel' command and run it on 'npm test' (supersedes PR #1011 - Ensure /lib is up to date during CI tests)
  - Fix 'npm test' command not working on Windows due to Istanbul config issue (see gotwarlost/istanbul#90)
  - Update CONTRIBUTING.md

jimthedev added a commit to commitizen/cz-cli that referenced this issue Nov 11, 2015

tcoulter added a commit to tcoulter/secp256k1-node that referenced this issue Feb 29, 2016

fanatid added a commit to cryptocoinjs/secp256k1-node that referenced this issue Mar 1, 2016

@iongion

This comment has been minimized.

Show comment
Hide comment
@iongion

iongion Apr 19, 2016

Using windows 10
Using node v5.7.0
Using istanbul 0.4.3
Using mocha 2.4.5

No cygwin, no mingw, no linux emulation, plain old Windows with bat and cmd files
I've tried:

node_modules\.bin\istanbul cover --hook-run-in-context _mocha --compilers js:babel-core/register --bail --sort --reporter dot --require .\test\bootstrap.js --recursive test/unit/**/**/*.spec.js

I get:

No coverage information was collected, exit without writing coverage information
C:\Users\me\AppData\Roaming\npm\_mocha.CMD:1
(function (exports, require, module, __filename, __dirname) { @IF EXIST "%~dp0\node.exe" (
                                                              ^
SyntaxError: Unexpected token ILLEGAL
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:387:25)
    at Object.Module._extensions..js (module.js:422:10)
    at Object.Module._extensions.(anonymous function) [as .js]

iongion commented Apr 19, 2016

Using windows 10
Using node v5.7.0
Using istanbul 0.4.3
Using mocha 2.4.5

No cygwin, no mingw, no linux emulation, plain old Windows with bat and cmd files
I've tried:

node_modules\.bin\istanbul cover --hook-run-in-context _mocha --compilers js:babel-core/register --bail --sort --reporter dot --require .\test\bootstrap.js --recursive test/unit/**/**/*.spec.js

I get:

No coverage information was collected, exit without writing coverage information
C:\Users\me\AppData\Roaming\npm\_mocha.CMD:1
(function (exports, require, module, __filename, __dirname) { @IF EXIST "%~dp0\node.exe" (
                                                              ^
SyntaxError: Unexpected token ILLEGAL
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:387:25)
    at Object.Module._extensions..js (module.js:422:10)
    at Object.Module._extensions.(anonymous function) [as .js]
@iongion

This comment has been minimized.

Show comment
Hide comment
@iongion

iongion Apr 19, 2016

In the end it is working, like this:
(this not bash, it is windows batch)

node_modules\.bin\babel-node.cmd ./node_modules/istanbul/lib/cli cover node_modules/mocha/bin/_mocha -- --compilers js:babel-core/register --bail --sort --reporter dot --require test/bootstrap.js --recursive test/unit/**/**/*.spec.js

iongion commented Apr 19, 2016

In the end it is working, like this:
(this not bash, it is windows batch)

node_modules\.bin\babel-node.cmd ./node_modules/istanbul/lib/cli cover node_modules/mocha/bin/_mocha -- --compilers js:babel-core/register --bail --sort --reporter dot --require test/bootstrap.js --recursive test/unit/**/**/*.spec.js
@legendtang

This comment has been minimized.

Show comment
Hide comment
@legendtang

legendtang Jun 12, 2016

Still encounter this issue.

Using Windows 10
Using node v6.1.0
Using istanbul 0.4.3
Using mocha 2.5.3

Still encounter this issue.

Using Windows 10
Using node v6.1.0
Using istanbul 0.4.3
Using mocha 2.5.3

@xPaw xPaw referenced this issue Jun 19, 2016

Merged

Add code coverage #408

marklagendijk pushed a commit to website-scraper/node-website-scraper that referenced this issue Jun 27, 2016

s0ph1e added a commit to website-scraper/node-website-scraper that referenced this issue Jun 27, 2016

Fixed css handler deadlock. (#82)
* Fixed css handler deadlock.

* Fix test command to also work on Windows, see gotwarlost/istanbul#90 (comment)

* Updated test

@bergus bergus referenced this issue Jul 13, 2016

Merged

Useful stuff #20

@xunshicheng

This comment has been minimized.

Show comment
Hide comment
@xunshicheng

xunshicheng Aug 10, 2016

i do like below, it works:
npm install mocha, not add -g;
then $ istanbul cover node_modules/mocha/bin/_mocha

i do like below, it works:
npm install mocha, not add -g;
then $ istanbul cover node_modules/mocha/bin/_mocha

ZaLiTHkA pushed a commit to ZaLiTHkA/generator-modular-angular that referenced this issue Aug 25, 2016

Elgolfin added a commit to Elgolfin/Playground that referenced this issue Jan 27, 2017

Configure the unit tests
Regarding the change done in the package.json file, see below:
gotwarlost/istanbul#90 (comment)

Elgolfin added a commit to Elgolfin/Playground that referenced this issue Jan 27, 2017

Configure the unit tests
  Regarding the change done in the package.json file, see below:
  gotwarlost/istanbul#90 (comment)

@alanshaw alanshaw referenced this issue Feb 24, 2017

Closed

lookup: mark david as flaky on win32 #365

3 of 3 tasks complete

alanshaw added a commit to alanshaw/david that referenced this issue Feb 24, 2017

stephenplusplus pushed a commit to GoogleCloudPlatform/google-cloud-node that referenced this issue Mar 2, 2017

electerious added a commit to electerious/rosid-handler-ejs that referenced this issue Mar 23, 2017

@bennyn

This comment has been minimized.

Show comment
Hide comment
@bennyn

bennyn Apr 11, 2017

I am on Windows 10 and installed "istanbul" globally (v0.4.5). My "npm test" script looks like this:

"test": "istanbul cover node_modules/mocha/bin/_mocha -s 30000 -t 30000 --recursive test/harness.js test --trace-warnings"

When I am running npm test I am getting this exception:

> istanbul cover node_modules/mocha/bin/_mocha -s 30000 -t 30000 --recursive test/harness.js test --trace-warnings

No coverage information was collected, exit without writing coverage information
D:\dev\projects\wireapp\proteus.js\node_modules\mocha\lib\utils.js:634
        throw new Error("cannot resolve path (or pattern) '" + path + "'");
        ^

Error: cannot resolve path (or pattern) '30000'
    at Object.lookupFiles (D:\dev\projects\wireapp\proteus.js\node_modules\mocha\lib\utils.js:634:15)

Can you help me with this?

bennyn commented Apr 11, 2017

I am on Windows 10 and installed "istanbul" globally (v0.4.5). My "npm test" script looks like this:

"test": "istanbul cover node_modules/mocha/bin/_mocha -s 30000 -t 30000 --recursive test/harness.js test --trace-warnings"

When I am running npm test I am getting this exception:

> istanbul cover node_modules/mocha/bin/_mocha -s 30000 -t 30000 --recursive test/harness.js test --trace-warnings

No coverage information was collected, exit without writing coverage information
D:\dev\projects\wireapp\proteus.js\node_modules\mocha\lib\utils.js:634
        throw new Error("cannot resolve path (or pattern) '" + path + "'");
        ^

Error: cannot resolve path (or pattern) '30000'
    at Object.lookupFiles (D:\dev\projects\wireapp\proteus.js\node_modules\mocha\lib\utils.js:634:15)

Can you help me with this?

@bennyn

This comment has been minimized.

Show comment
Hide comment
@bennyn

bennyn Apr 11, 2018

Note: I also got a similar issue when running istanbul together with jasmine:

istanbul cover --report html jasmine
No coverage information was collected, exit without writing coverage information
D:\dev\projects\wireapp\wire-web-packages\packages\cryptobox\node_modules.bin\jasmine.CMD:1
(function (exports, require, module, __filename, __dirname) { @if EXIST "%~dp0\node.exe" (

What I needed to do is change:

istanbul cover --report html jasmine

To:

istanbul cover --report html ./node_modules/jasmine/bin/jasmine.js

bennyn commented Apr 11, 2018

Note: I also got a similar issue when running istanbul together with jasmine:

istanbul cover --report html jasmine
No coverage information was collected, exit without writing coverage information
D:\dev\projects\wireapp\wire-web-packages\packages\cryptobox\node_modules.bin\jasmine.CMD:1
(function (exports, require, module, __filename, __dirname) { @if EXIST "%~dp0\node.exe" (

What I needed to do is change:

istanbul cover --report html jasmine

To:

istanbul cover --report html ./node_modules/jasmine/bin/jasmine.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment