Skip to content
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
Closed

mocha Illegal Token Error #90

anderslyman opened this issue Aug 8, 2013 · 13 comments

Comments

@anderslyman
Copy link

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
Copy link
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

@anderslyman
Copy link
Author

Works great, thanks!

@BoyCook
Copy link

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
Copy link

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
Copy link
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

@EvanHongYousan
Copy link

it works ,thank you! @gotwarlost

@DevinXian
Copy link

Thank you, it works!@gotwarlost

harriha added a commit to harriha/knex that referenced this issue Aug 30, 2015
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
  - Add 'npm run babel' command and run it on 'npm test' (supersedes PR knex#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 pushed a commit to cryptocoinjs/secp256k1-node that referenced this issue Mar 1, 2016
@iongion
Copy link

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
Copy link

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
Copy link

Still encounter this issue.

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

marklagendijk pushed a commit to website-scraper/node-website-scraper that referenced this issue Jun 27, 2016
s0ph1e pushed a commit to website-scraper/node-website-scraper that referenced this issue Jun 27, 2016
* Fixed css handler deadlock.

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

* Updated test
@xunshicheng
Copy link

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

@bennycode
Copy link

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?

@bennycode
Copy link

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

rosslavni pushed a commit to rosslavni/nodejs-dashboard that referenced this issue Aug 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants