Istanbul: Windows support

Created on 19 Oct 2012  ยท  49Comments  ยท  Source: gotwarlost/istanbul

My istanbul cover commands fail on windows, but the vows --spec ./test/foo.js works fine.

I get this error:

YUI@WINDOWS7 ~/Desktop/play/cpr (master)
$ npm test

> [email protected] pretest c:\Users\YUI\Desktop\play\cpr
> jshint --config ./node_modules/yui-lint/jshint.json ./lib/


> [email protected] test c:\Users\YUI\Desktop\play\cpr
> istanbul cover --print both -- vows --spec ./tests/full.js


c:\Users\YUI\Desktop\play\cpr\node_modules\.bin\vows.CMD:1
(function (exports, require, module, __filename, __dirname) { :: Created by np
                                                              ^
Unexpected token :
SyntaxError: Unexpected token :
    at Module._compile (module.js:437:25)
    at Module._extensions..js (module.js:467:10)
    at Object.Module._extensions..js (c:\Users\YUI\Desktop\play\cpr\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:492:10)
    at runFn (c:\Users\YUI\Desktop\play\cpr\node_modules\istanbul\lib\command\co
mmon\run-with-cover.js:80:16)
    at c:\Users\YUI\Desktop\play\cpr\node_modules\istanbul\lib\command\common\ru
n-with-cover.js:148:17
    at c:\Users\YUI\Desktop\play\cpr\node_modules\istanbul\lib\util\file-matcher
.js:52:16
    at c:\Users\YUI\Desktop\play\cpr\node_modules\istanbul\lib\util\file-matcher
.js:35:9
No coverage information was collected, exit without writing coverage information

npm ERR! Test failed.  See above for more details.
npm ERR! not ok code 0

YUI@WINDOWS7 ~/Desktop/play/cpr (master)
$

For reference, that vows is running:

YUI@WINDOWS7 ~/Desktop/play/cpr (master)
$ ./node_modules/.bin/vows --spec ./tests/full.js

  โ™ข CPR Tests

  should be loaded
    โœ“ should have cpr method
  should fail on non-existant from dir
    โœ“ should return an error in the callback
  should be loaded and should NOT copy node_modules
    โœ“ does not have ./out/1
    โœ“ and threw an error
  should be loaded and should not copy graceful-fs from function
    โœ“ and has ./out/3
    โœ“ and dirs are not equal
    โœ“ and from directory has graceful-fs dir
    โœ“ and to directory does not have graceful-fs dir
  should be loaded and should copy node_modules
    โœ“ has ./out/0
    โœ“ and dirs are equal
    โœ“ and from directory has graceful-fs dir
    โœ“ and to directory has graceful-fs dir
  should be loaded and should not copy yui-lint from regex
    โœ“ and has ./out/2
    โœ“ and dirs are not equal
    โœ“ and from directory has yui-lint dir
    โœ“ and to directory does not have yui-lint dir
  should be loaded and should copy node_modules with overwrite flag
    โœ“ has ./out/0
    โœ“ and dirs are equal
    โœ“ and from directory has graceful-fs dir
    โœ“ and to directory has graceful-fs dir

โœ“ OK ยป 20 honored (10.885s)


YUI@WINDOWS7 ~/Desktop/play/cpr (master)
$

Most helpful comment

See my previous comment, reproduced for your benefit:

so the "magic" expansion of commands to JS files only works on Mac/ Unix because the executable files directly resolve to the JS files that have shebangs in them.

On windows, you need to actually pass the JS file name that the mocha command uses, since in that environment the commands resolve to batch files and not JS files.

All 49 comments

Something screwy with the colon character in the path name that is confusing istanbul.

Damn, I wish I had an easy way to reproduce windows issues. Do you have a VM or something I can use to play with this.

Windows is the biggest gap in tests for istanbul

I have a personal Windows 7 VM that I have setup with Node and git, but nothing that I can share as its in my server closet in IL ;)

Could you run istanbul cover -v and paste the output just in case there is any additional info to be had from verbose logging?

The output of -v is exactly the same as above.

Ok, here's some info for you.

If I change my test script to:

istanbul --cover -v --print both -- ./node_modules/vows/bin/vows --spec ./tests/full.js

From:

istanbul --cover -v --print both -- vows --spec ./tests/full.js

It works.

On Windows, the ./node_modules/.bin/vows.cmd is a windows bat script. So istanbul needs to know how to deal with that.

Thanks for the info. This is going to be really tricky, if not impossible....

Bascially, the "command processing" is done using node-which and the result of this operation is assumed to be a JS file that is passed directly to the module loader as in: Module.runMain(file).

Also, off-topic - does the HTML reporting work correctly on Windows? I don't have a single test for this and it's the reporting that has path-delimiter specific code.

Yeah, I figured as much. If I have some time, I may see what I can come up with to help out since I have a fully functional windows dev environment here.

The reporting works and looks ok. It renders all the / in the HTML as \, which just doesn't quite look right to me. Besides, the reports should be pretty close to exact no matter what system they are generated on. (thinking about coverage files that are checked into source control).

Let me work on getting a Windows machine up and running - I think I'll pinch my son's machine for this. Totally agree that everything should work the same on windows.

Everything seems to be working ok on Windows, but I have one small question:

Say I have the following folder structure:

lib/

  • lib1.js
  • lib2.js

tests/

  • lib1.js
  • lib2.js

index.js (main file that requires all the tests)

If I run istanbul cover index.js besides the coverage\ folder it creates .html files in lib/ and a index.html && index.js.html file inside the root.

The main html coverage stats file is in coverage\, but shouldn't the rest also be there... ? (I haven't had time to test the library in OSX / Ubuntu yet to see if it's the same there).

I really hate having all those .html files laying around everywhere. Thanks

@gotwarlost @davglass

Ok followup: so normally istanbul should put the html files inside /coverage/lcov-report/ (and specific files inside the folder ProjectName).

The thing is in Windows it only puts /coverage/lcov-report/index.html there, the rest of the html files are dispersed all over the place (wherever there's a covered_test_file.js it will put the html in the same folder).

More details about my setup: Windows 7 and:

$ node -v && npm -v
v0.6.11
1.1.1

Anyway, I'm happy it works in OSX/Linux :D

Can you install the latest node on Windows and check it against that first? 0.6.11 is quite old ;)

Anyone else still having problems in Windows? I still get that same kind of error like this:

c:\Users\JGough\AppData\Roaming\npm\_mocha.CMD:1
(function (exports, require, module, __filename, __dirname) { :: Created by np
                                                              ^
SyntaxError: Unexpected token :
    at Module._compile (module.js:437:25)
    at Module._extensions..js (module.js:467:10)
    at Object.Module._extensions..js (c:\Users\JGough\AppData\Roaming\npm\node_m
odules\istanbul\lib\hook.js:101:13)

ok, so the "magic" expansion of commands to JS files only works on Mac/ Unix because the executable files directly resolve to the JS files that have shebangs in them.

On windows, you need to actually pass the JS file name that the mocha command uses, since in that environment the commands resolve to batch files and not JS files.

Hmm.. well, I'm installing a linux vm irght now :-P

But, I tried this too:

istanbul cover test.js

And it told me it could not find 'describe'. So, I assume I somehow have to include mocha in the command.

$ istanbul cover test.js

c:\Projects\v1.NETSdk.GitHub\Versionone.AppCatalog.Web.Git\VersionOne.AppCatalog
.Web\test.js:6
;var should=require('should');__cov_xnGr93WTzh3NigP_PMZaIQ.s['3']++;describe('
                                                                    ^
ReferenceError: describe is not defined
    at Object.<anonymous> (c:\Projects\v1.NETSdk.GitHub\Versionone.AppCatalog.We
b.Git\VersionOne.AppCatalog.Web\test.js:6:194)
    at Object.<anonymous> (c:\Projects\v1.NETSdk.GitHub\Versionone.AppCatalog.We
b.Git\VersionOne.AppCatalog.Web\test.js:6:406)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (c:\Users\JGough\AppData\Roaming\npm\node_m
odules\istanbul\lib\hook.js:99:20)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:492:10)
    at runFn (c:\Users\JGough\AppData\Roaming\npm\node_modules\istanbul\lib\comm
and\common\run-with-cover.js:86:16)
    at c:\Users\JGough\AppData\Roaming\npm\node_modules\istanbul\lib\command\com
mon\run-with-cover.js:185:17
    at c:\Users\JGough\AppData\Roaming\npm\node_modules\istanbul\lib\util\file-m
atcher.js:52:16

right - describe is added as a global by mocha.

@JogoShugh @gotwarlost I ran into the issue with describe is not defined when running mocha. My solution was to update the mocha dependency to the latest version (1.13.0 at the time of writing).

When trying to run istanbul using istanbul cover _mocha -- -u exports -R spec on my project, I get the following error:

(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\Sony\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\Sony\AppData\Roaming\npm\node_modules\istanbul\lib\command\common\run-with-cover.js:110:16)
    at C:\Users\Sony\AppData\Roaming\npm\node_modules\istanbul\lib\command\common\run-with-cover.js:213:17
    at C:\Users\Sony\AppData\Roaming\npm\node_modules\istanbul\lib\util\file-matcher.js:52:16
    at C:\Users\Sony\AppData\Roaming\npm\node_modules\istanbul\lib\util\file-matcher.js:35:9

I've updated mocha to the latest version and everything works fine and all tests pass. Any idea what could be causing this?

See my previous comment, reproduced for your benefit:

so the "magic" expansion of commands to JS files only works on Mac/ Unix because the executable files directly resolve to the JS files that have shebangs in them.

On windows, you need to actually pass the JS file name that the mocha command uses, since in that environment the commands resolve to batch files and not JS files.

@gotwarlost Thanks for the response. I just skimmed through the thread - silly me. Sorry for the inconvenience!

I didn't understand at all what @gotwarlost was saying, since file expansion could apply to istanbul or mocha, but then I saw @mscdex link an issue that contains a PR with the actual fix. Posting here if anyone else was confused:

https://github.com/component/path-to-regexp/pull/29/files

@gotwarlost Could the "magic" expansion be fixed if you added a command line flag to trigger a lookup through the module loader?

Add a flag that is used like this:

istanbul cover --bin <package>[/<command>] -- 

Which transforms the parameter in a manner similar to this:

var path = require('path');
function lookupBinPath(inputCommandName) {
    var parts = inputCommandName.split('/');
    var packageName = parts[0], commandName = parts[1];
    // If no slash is present, look for a command with the same name as the package
    if (commandName == undefined) commandName = packageName;
    // Look up the package.json
    var packageJSON = require(packageName+'/package.json');
    //Extract the binary path
    var relativeBinaryPath = packageJSON.bin[commandName];
    // prepend the module name and pass it to require.resolve 
    return require.resolve(path.join(packageName, relativeBinaryPath));
}

I am using this command
istanbul cover C:\Users\abhisek.user\AppData\Roaming\npm\node_modules\mocha\bin_mocha test
But it is giving this as out put and not generating the report.
Transformation error; return original code
[TypeError: Object # has no method 'isIdentifierPart']
//
//
No coverage information was collected, exit without writing coverage information

That seems to be an error in parsing your Javascript. Are you sure it is well-formed?

Yes it is well formed. will be a reason for node.js?

Using latest version of karma...

cmi-alvinmilton-1089-mbp:ng-desc-tests alvin.milton$ istanbul cover test/*-spec.js
No coverage information was collected, exit without writing coverage information
/Users/alvin.milton/Development/ng-desc-tests/test/add-spec.js:7

describe('add', function () {
^

ReferenceError: describe is not defined

describe('add', function () { var add; beforeEach(function () {...

I can confirm that this is still an issue on windows 10.

C:\Program Files (x86)\nodejs\npm.CMD:1
(function (exports, require, module, __filename, __dirname) { :: Created by npm, please don't edit manually.
                                                              ^

SyntaxError: Unexpected token :
...

I am running it this way (without flags for readability):

istanbul cover npm run test:server

Please understand that this command works fine

npm run test:server

So there is no "magic" expansion of variables and I have no idea what you mean by that.

Technical details:

$ node -v
v5.1.1
$ npm -v
3.5.1
$ cat package.json | grep istanbul
    "istanbul": "0.3.17",
$ cat package.json | grep mocha
    "mocha": "2.3.3",

I tested and can confirm that it persists in istanbul 0.4.1

I am using the latest version of Istanbul and I have a similiar problem in Windows 8. This does not work:

istanbul cover jasmine-node spec

No coverage information was collected, exit without writing coverage information
C:\Users\x\AppData\Roaming\npm\jasmine-node.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:373:25)
    at Object.Module._extensions..js (module.js:416:10)
    at Object.Module._extensions.(anonymous function) [as .js] (C:\Users\x\AppData\Roaming\npm\node_modules\istanbul\lib\hook.js:109:37)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at runFn (C:\Users\x\AppData\Roaming\npm\node_modules\istanbul\lib\command\common\run-with-cover.js:122:16)
    at C:\Users\x\AppData\Roaming\npm\node_modules\istanbul\lib\command\common\run-with-cover.js:251:17
    at C:\Users\x\AppData\Roaming\npm\node_modules\istanbul\lib\util\file-matcher.js:68:16

But if I add the path to the jasmine executable it works:

istanbul cover node_modules\jasmine-node\bin\jasmine-node spec

....

Finished in 12.51 seconds
4 tests, 7 assertions, 0 failures, 0 skipped

This is still an issue for me on Windows 10 using git-bash from git for Windows 2.8.3-64 from [1].

$ istanbul cover _mocha -- --recursive
No coverage information was collected, exit without writing coverage information
C:\Users\scott\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:373:25)
    at Object.Module._extensions..js (module.js:416:10)
    at Object.Module._extensions.(anonymous function) [as .js] (C:\Users\scott\AppData\Roaming\npm\node_modules\istanbul\lib\hook.js:109:37)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at runFn (C:\Users\scott\AppData\Roaming\npm\node_modules\istanbul\lib\command\common\run-with-cover.js:122:16)
    at C:\Users\scott\AppData\Roaming\npm\node_modules\istanbul\lib\command\common\run-with-cover.js:251:17
    at C:\Users\scott\AppData\Roaming\npm\node_modules\istanbul\lib\util\file-matcher.js:68:16

but if I pass the full path to _mocha it works,
$ istanbul cover node_modules/mocha/bin/_mocha -- --recursive

Here are some details of what is installed on my system:

$ istanbul help 2>&1 |grep version
istanbul version:0.4.3
$ node -v
v4.4.4
$ npm --version
2.15.1
$ mocha --version
2.4.5

Could it be due to previously npm install -g mocha and npm install -g istanbul ? The failing call stack is referencing the %APPDATA% paths of mocha and istanbul so maybe that is what is messed up?

$ where mocha
C:\Users\scott\AppData\Roaming\npm\mocha
C:\Users\scott\AppData\Roaming\npm\mocha.cmd
$ ls /c/Users/scott/AppData/Roaming/npm/node_modules/mocha/bin/
_mocha*  mocha*  options.js
$ where istanbul
C:\Users\scott\AppData\Roaming\npm\istanbul
C:\Users\scott\AppData\Roaming\npm\istanbul.cmd
$ where _mocha
C:\Users\scott\AppData\Roaming\npm\_mocha
C:\Users\scott\AppData\Roaming\npm\_mocha.cmd
$ ls ./node_modules/mocha/
bin/          images/   lib/     mocha.css  node_modules/
CHANGELOG.md  index.js  LICENSE  mocha.js   package.json
$ ls ./node_modules/istanbul/
CHANGELOG.md  index.js  lib/  LICENSE  node_modules/  package.json  README.md
$ istanbul cover -v _mocha -- --recursive
Using configuration
-------------------
verbose: true
instrumentation:
    root: .
    extensions:
        - .js
    default-excludes: true
    excludes: []
    embed-source: false
    variable: __coverage__
    compact: true
    preserve-comments: false
    complete-copy: false
    save-baseline: false
    baseline-file: ./coverage/coverage-baseline.json
    include-all-sources: false
    include-pid: false
    es-modules: false
    preload-sources: false
reporting:
    print: summary
    reports:
        - lcov
    dir: ./coverage
    watermarks:
        statements: [50, 80]
        lines: [50, 80]
        functions: [50, 80]
        branches: [50, 80]
    report-config:
        clover: {file: clover.xml}
        cobertura: {file: cobertura-coverage.xml}
        json: {file: coverage-final.json}
        json-summary: {file: coverage-summary.json}
        lcovonly: {file: lcov.info}
        teamcity: {file: null, blockName: Code Coverage Summary}
        text: {file: null, maxCols: 0}
        text-lcov: {file: lcov.info}
        text-summary: {file: null}
hooks:
    hook-run-in-context: false
    post-require-hook: null
    handle-sigint: false
check:
    global:
        statements: 0
        lines: 0
        branches: 0
        functions: 0
        excludes: []
    each:
        statements: 0
        lines: 0
        branches: 0
        functions: 0
        excludes: []

-------------------

Running: node C:\Users\scott\AppData\Roaming\npm\_mocha.CMD --recursive
No coverage information was collected, exit without writing coverage information
C:\Users\scott\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:373:25)
    at Object.Module._extensions..js (module.js:416:10)
    at Object.Module._extensions.(anonymous function) [as .js] (C:\Users\scott\AppData\Roaming\npm\node_modules\istanbul\lib\hook.js:109:37)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at runFn (C:\Users\scott\AppData\Roaming\npm\node_modules\istanbul\lib\command\common\run-with-cover.js:122:16)
    at C:\Users\scott\AppData\Roaming\npm\node_modules\istanbul\lib\command\common\run-with-cover.js:251:17
    at C:\Users\scott\AppData\Roaming\npm\node_modules\istanbul\lib\util\file-matcher.js:68:16
$ cat /c/Users/scott/AppData/Roaming/npm/_mocha.cmd
@IF EXIST "%~dp0\node.exe" (
  "%~dp0\node.exe"  "%~dp0\node_modules\mocha\bin\_mocha" %*
) ELSE (
  @SETLOCAL
  @SET PATHEXT=%PATHEXT:;.JS;=;%
  node  "%~dp0\node_modules\mocha\bin\_mocha" %*
)
$ cat /c/Users/scott/AppData/Roaming/npm/_mocha
#!/bin/sh
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")

case `uname` in
    *CYGWIN*) basedir=`cygpath -w "$basedir"`;;
esac

if [ -x "$basedir/node" ]; then
  "$basedir/node"  "$basedir/node_modules/mocha/bin/_mocha" "$@"
  ret=$?
else
  node  "$basedir/node_modules/mocha/bin/_mocha" "$@"
  ret=$?
fi
exit $ret
$ uname
MINGW64_NT-10.0

Let me know if gathering any additional command output or file contents would be helpful.

[1] https://git-scm.com/download/win

Still an issue with

istanbul cover mocha

but

istanbul cover node_modules/mocha/bin/_mocha

works

node -v

v6.2.2

npm -v

3.9.5

use istanbul as devDependencies. used script:

istanbul cover -x *.test.js node_modules/mocha/bin/_mocha -- -R spec src/index.test.js

it works!

same as @scott-plutovr I alse see this error under git-bash

Still relevant.

I also have this issue with node 10.1.0 on a windows machine.

I guess we should setup appveyor and try to find solutions.

It seems like this is working for node versions 6 and 8, but it doesn't work for node version 10. I had to uninstall 10.15.3 and revert back to 8.15.1.

It seems like this is working for node versions 6 and 8, but it doesn't work for node version 10. I had to uninstall 10.15.3 and revert back to 8.15.1.

You can use nvm and alternatives to switch versions.

It seems like this is working for node versions 6 and 8, but it doesn't work for node version 10. I had to uninstall 10.15.3 and revert back to 8.15.1.

You can use nvm and alternatives to switch versions.

NVM doesn't work for me on an x64 Windows 10 system. I'm noticing there's no stdout when I'm running my coverage combined with _mocha.

node ./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha --harmony -- -R spec

produces the coverage results without outputting the describe and it blocks during testing.

and alternatives

I did not say that nvm can be used on Windows (in general).

you could use npx node@8 to use node version 8 even when you have a different version installed. source

something like:

npx node@8 node_modules/istanbul/lib/cli.js cover *.test.js node_modules/mocha/bin/_mocha

On WSL and Git Bash much more solutions should work.

WSL is not Windows either.

Damn, I wish I had an easy way to reproduce windows issues. Do you have a VM or something I can use to play with this.

Windows is the biggest gap in tests for istanbul

We can test with Travis CI.
I will open a PR for this.

@DanielRuf I don't think this repo is maintained.

According to the readme:

Deprecation Notice: this version of _istanbul_ is deprecated, we will not be
landing pull requests or releasing new versions. But don't worry, the Istanbul 2.0
API is now available
and is being actively developed
in the new istanbuljs organization.

https://github.com/gotwarlost/istanbul/pull/910

But I think it is better to use another code coverage solution (v8 internally provides this already).

The last (tagged) release was in 2016 and last commit in 2017:
https://github.com/gotwarlost/istanbul/releases.

According to the readme:

_Deprecation Notice:_ this version of _istanbul_ is deprecated, we will not be
landing pull requests or releasing new versions. But don't worry, the Istanbul 2.0
API is now available
and is being actively developed
in the new istanbuljs organization.

Right, time to migrate then I guess. Was not clear enough in the readme.

If you're having this issue and you're on a Windows machine, all you need to do is to specify the full path of mocha to istanbul like so;
istanbul cover node_modules/mocha/bin/_mocha

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mbielski picture mbielski  ยท  37Comments

asciidisco picture asciidisco  ยท  47Comments

ottes picture ottes  ยท  36Comments

graemej picture graemej  ยท  22Comments

briancullinan picture briancullinan  ยท  22Comments