jestjs / jest

Delightful JavaScript Testing.
https://jestjs.io
MIT License
44.05k stars 6.44k forks source link

Coverage report not valid JSON on Windows #1611

Closed IljaKroonen closed 7 years ago

IljaKroonen commented 8 years ago

Do you want to request a feature or report a bug? bug

What is the current behavior? When generating a coverage report on windows, the output coverage-final.json is not valid JSON because the path to the files contains unescaped backslashes (in the keys). Example of the start of a file:

{"C:\Users\Ilja\Documents\XXX\XXX\XXX\domain-store.js": {"path":"C:\\Users\\Ilja\\Documents\\XXX\\XXX\\XXX\\domain-store.js",
...

JSON.parse output:

> JSON.parse(fileStr)
SyntaxError: Unexpected token U

What is the expected behavior? The coverage-final.json file should be a JSON file.

Run Jest again with --debug and provide the full configuration it prints. Please mention your node and npm version and operating system. Windows 10

$ npm version
{ 'XXX': '1.0.0',
  npm: '3.8.6',
  ares: '1.10.1-DEV',
  http_parser: '2.7.0',
  icu: '56.1',
  modules: '47',
  node: '5.12.0',
  openssl: '1.0.2h',
  uv: '1.8.0',
  v8: '4.6.85.32',
  zlib: '1.2.8' }

Jest is in version v15.1.1

aaronabramov commented 8 years ago

we should probably implement some kind of path normalization function that will lowercase the C:\ and escape all special characters

cc @Daniel15

Daniel15 commented 8 years ago

Hmm... Are we hand crafting JSON here? JSON.stringify should be escaping the backslashes.

Case sensitivity issues are also present on Mac OSX, which also uses a case insensitive file system by default. We probably need to normalize all file paths regardless of OS. There's a separate issue for these case sensitivity issues (on Windows it's often c:\ vs C:)

Sent from my phone.

On Sep 6, 2016 7:11 PM, "Dmitrii Abramov" notifications@github.com wrote:

we should probably implement some kind of path normalization function that will lowercase the C:\ and escape all special characters

cc @Daniel15 https://github.com/Daniel15

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/facebook/jest/issues/1611#issuecomment-245154393, or mute the thread https://github.com/notifications/unsubscribe-auth/AAFnHa6NuhHHyoAKKcMssLFa379tGsO8ks5qnh1qgaJpZM4J02fw .

aaronabramov commented 8 years ago

hm... we shouldn't be writing any JSON manually. this is strange

Daniel15 commented 8 years ago

I confirmed that JSON.stringify correctly escapes \ characters, so there must be something else that's causing this JSON to be invalid.

aaronabramov commented 8 years ago

@Daniel15 i'll look inside istanbul coverage reporters. most likely it's something in there that's causing this

cpojer commented 8 years ago

@IljaKroonen we'll need more info to be able to troubleshoot this issue. Can you provide a repository that we can npm install and npm test that highlights the issue you are running into? Thanks!

IljaKroonen commented 8 years ago

Any basic Jest setup has the problem on my system. I made a repo: https://github.com/IljaKroonen/jest-json-bug

This is my output:

> npm install && npm run test
fetchMetadata -> resolveW / |#############################------------------------------------------------------------|
npm WARN deprecated minimatch@2.0.10: Please update to minimatch 3.0.2 or higher to avoid a RegExp DoS issue
npm WARN prefer global marked@0.3.6 should be installed with -g
jest-json-bug@1.0.0 C:\Users\Ilja\Documents\jest-json-bug
`-- jest-cli@15.1.1
  +-- ansi-escapes@1.4.0
  +-- callsites@2.0.0
  +-- chalk@1.1.3
  | +-- ansi-styles@2.2.1
  | +-- escape-string-regexp@1.0.5
  | +-- has-ansi@2.0.0
  | | `-- ansi-regex@2.0.0
  | +-- strip-ansi@3.0.1
  | `-- supports-color@2.0.0
  +-- graceful-fs@4.1.6
  +-- istanbul-api@1.0.0-aplha.10
  | +-- async@1.5.2
  | +-- clone@1.0.2
  | +-- fileset@0.2.1
  | | +-- glob@5.0.15
  | | | +-- inflight@1.0.5
  | | | `-- inherits@2.0.3
  | | `-- minimatch@2.0.10
  | +-- istanbul-lib-hook@1.0.0-alpha.4
  | | `-- append-transform@0.3.0
  | +-- istanbul-lib-report@1.0.0-alpha.3
  | | +-- path-parse@1.0.5
  | | +-- rimraf@2.5.4
  | | | `-- glob@7.0.6
  | | |   +-- fs.realpath@1.0.0
  | | |   `-- minimatch@3.0.3
  | | `-- supports-color@3.1.2
  | |   `-- has-flag@1.0.0
  | +-- istanbul-lib-source-maps@1.0.1
  | | `-- source-map@0.5.6
  | +-- istanbul-reports@1.0.0-alpha.8
  | | `-- handlebars@4.0.5
  | |   +-- optimist@0.6.1
  | |   | +-- minimist@0.0.10
  | |   | `-- wordwrap@0.0.3
  | |   +-- source-map@0.4.4
  | |   | `-- amdefine@1.0.0
  | |   `-- uglify-js@2.7.3
  | |     +-- async@0.2.10
  | |     +-- uglify-to-browserify@1.0.2
  | |     `-- yargs@3.10.0
  | |       +-- camelcase@1.2.1
  | |       +-- cliui@2.1.0
  | |       | +-- center-align@0.1.3
  | |       | | +-- align-text@0.1.4
  | |       | | | `-- longest@1.0.1
  | |       | | `-- lazy-cache@1.0.4
  | |       | +-- right-align@0.1.3
  | |       | `-- wordwrap@0.0.2
  | |       `-- window-size@0.1.0
  | +-- js-yaml@3.6.1
  | | +-- argparse@1.0.7
  | | | `-- sprintf-js@1.0.3
  | | `-- esprima@2.7.3
  | +-- mkdirp@0.5.1
  | | `-- minimist@0.0.8
  | `-- once@1.4.0
  |   `-- wrappy@1.0.2
  +-- istanbul-lib-coverage@1.0.0
  +-- istanbul-lib-instrument@1.1.3
  | +-- babel-generator@6.14.0
  | | +-- babel-messages@6.8.0
  | | +-- babel-runtime@6.11.6
  | | | +-- core-js@2.4.1
  | | | `-- regenerator-runtime@0.9.5
  | | +-- detect-indent@3.0.1
  | | | +-- get-stdin@4.0.1
  | | | `-- repeating@1.1.3
  | | |   `-- is-finite@1.0.1
  | | `-- lodash@4.15.0
  | +-- babel-template@6.15.0
  | +-- babel-traverse@6.15.0
  | | +-- babel-code-frame@6.11.0
  | | | `-- js-tokens@2.0.0
  | | +-- debug@2.2.0
  | | | `-- ms@0.7.1
  | | +-- globals@8.18.0
  | | `-- invariant@2.2.1
  | |   `-- loose-envify@1.2.0
  | |     `-- js-tokens@1.0.3
  | +-- babel-types@6.15.0
  | | +-- esutils@2.0.2
  | | `-- to-fast-properties@1.0.2
  | `-- babylon@6.9.2
  +-- jest-changed-files@15.0.0
  +-- jest-config@15.1.1
  | +-- istanbul@0.4.5
  | | +-- abbrev@1.0.9
  | | +-- escodegen@1.8.1
  | | | +-- estraverse@1.9.3
  | | | +-- optionator@0.8.1
  | | | | +-- deep-is@0.1.3
  | | | | +-- fast-levenshtein@1.1.4
  | | | | +-- levn@0.3.0
  | | | | +-- prelude-ls@1.1.2
  | | | | +-- type-check@0.3.2
  | | | | `-- wordwrap@1.0.0
  | | | `-- source-map@0.2.0
  | | +-- nopt@3.0.6
  | | +-- supports-color@3.1.2
  | | `-- wordwrap@1.0.0
  | `-- jest-environment-node@15.1.1
  +-- jest-environment-jsdom@15.1.1
  | `-- jsdom@9.5.0
  |   +-- abab@1.0.3
  |   +-- acorn@2.7.0
  |   +-- acorn-globals@1.0.9
  |   +-- array-equal@1.0.0
  |   +-- cssom@0.3.1
  |   +-- cssstyle@0.2.37
  |   +-- iconv-lite@0.4.13
  |   +-- nwmatcher@1.3.8
  |   +-- parse5@1.5.1
  |   +-- request@2.74.0
  |   | +-- aws-sign2@0.6.0
  |   | +-- aws4@1.4.1
  |   | +-- bl@1.1.2
  |   | | `-- readable-stream@2.0.6
  |   | |   +-- core-util-is@1.0.2
  |   | |   +-- isarray@1.0.0
  |   | |   +-- process-nextick-args@1.0.7
  |   | |   +-- string_decoder@0.10.31
  |   | |   `-- util-deprecate@1.0.2
  |   | +-- caseless@0.11.0
  |   | +-- combined-stream@1.0.5
  |   | | `-- delayed-stream@1.0.0
  |   | +-- extend@3.0.0
  |   | +-- forever-agent@0.6.1
  |   | +-- form-data@1.0.1
  |   | | `-- async@2.0.1
  |   | +-- har-validator@2.0.6
  |   | | +-- commander@2.9.0
  |   | | | `-- graceful-readlink@1.0.1
  |   | | `-- is-my-json-valid@2.13.1
  |   | |   +-- generate-function@2.0.0
  |   | |   +-- generate-object-property@1.2.0
  |   | |   | `-- is-property@1.0.2
  |   | |   `-- jsonpointer@2.0.0
  |   | +-- hawk@3.1.3
  |   | | +-- boom@2.10.1
  |   | | +-- cryptiles@2.0.5
  |   | | +-- hoek@2.16.3
  |   | | `-- sntp@1.0.9
  |   | +-- http-signature@1.1.1
  |   | | +-- assert-plus@0.2.0
  |   | | +-- jsprim@1.3.1
  |   | | | +-- extsprintf@1.0.2
  |   | | | +-- json-schema@0.2.3
  |   | | | `-- verror@1.3.6
  |   | | `-- sshpk@1.10.0
  |   | |   +-- asn1@0.2.3
  |   | |   +-- assert-plus@1.0.0
  |   | |   +-- bcrypt-pbkdf@1.0.0
  |   | |   | `-- tweetnacl@0.14.3
  |   | |   +-- dashdash@1.14.0
  |   | |   | `-- assert-plus@1.0.0
  |   | |   +-- ecc-jsbn@0.1.1
  |   | |   +-- getpass@0.1.6
  |   | |   | `-- assert-plus@1.0.0
  |   | |   +-- jodid25519@1.0.2
  |   | |   +-- jsbn@0.1.0
  |   | |   `-- tweetnacl@0.13.3
  |   | +-- is-typedarray@1.0.0
  |   | +-- isstream@0.1.2
  |   | +-- json-stringify-safe@5.0.1
  |   | +-- mime-types@2.1.11
  |   | | `-- mime-db@1.23.0
  |   | +-- node-uuid@1.4.7
  |   | +-- oauth-sign@0.8.2
  |   | +-- qs@6.2.1
  |   | +-- stringstream@0.0.5
  |   | `-- tunnel-agent@0.4.3
  |   +-- sax@1.2.1
  |   +-- symbol-tree@3.1.4
  |   +-- tough-cookie@2.3.1
  |   +-- webidl-conversions@3.0.1
  |   +-- whatwg-url@3.0.0
  |   | `-- tr46@0.0.3
  |   `-- xml-name-validator@2.0.1
  +-- jest-file-exists@15.0.0
  +-- jest-haste-map@15.0.1
  | +-- fb-watchman@1.9.0
  | | `-- bser@1.0.2
  | |   `-- node-int64@0.4.0
  | `-- multimatch@2.1.0
  |   +-- array-differ@1.0.0
  |   +-- array-union@1.0.2
  |   | `-- array-uniq@1.0.3
  |   +-- arrify@1.0.1
  |   `-- minimatch@3.0.3
  +-- jest-jasmine2@15.1.1
  | +-- jasmine-check@0.1.5
  | | `-- testcheck@0.1.4
  | `-- jest-matchers@15.1.1
  |   `-- jest-matcher-utils@15.1.0
  +-- jest-mock@15.0.0
  +-- jest-resolve@15.0.1
  | +-- browser-resolve@1.11.2
  | `-- resolve@1.1.7
  +-- jest-resolve-dependencies@15.0.1
  +-- jest-runtime@15.1.1
  | +-- babel-core@6.14.0
  | | +-- babel-helpers@6.8.0
  | | +-- babel-register@6.14.0
  | | | +-- home-or-tmp@1.0.0
  | | | | +-- os-tmpdir@1.0.1
  | | | | `-- user-home@1.1.1
  | | | `-- source-map-support@0.2.10
  | | |   `-- source-map@0.1.32
  | | +-- convert-source-map@1.3.0
  | | +-- json5@0.4.0
  | | +-- minimatch@3.0.3
  | | +-- path-exists@1.0.0
  | | +-- path-is-absolute@1.0.0
  | | +-- private@0.1.6
  | | +-- shebang-regex@1.0.0
  | | `-- slash@1.0.0
  | +-- babel-jest@15.0.0
  | | `-- babel-preset-jest@15.0.0
  | |   `-- babel-plugin-jest-hoist@15.0.0
  | `-- babel-plugin-istanbul@2.0.1
  |   +-- find-up@1.1.2
  |   | +-- path-exists@2.1.0
  |   | `-- pinkie-promise@2.0.1
  |   |   `-- pinkie@2.0.4
  |   `-- test-exclude@2.1.2
  |     `-- micromatch@2.3.11
  |       +-- arr-diff@2.0.0
  |       | `-- arr-flatten@1.0.1
  |       +-- array-unique@0.2.1
  |       +-- braces@1.8.5
  |       | +-- expand-range@1.8.2
  |       | | `-- fill-range@2.2.3
  |       | |   +-- is-number@2.1.0
  |       | |   +-- isobject@2.1.0
  |       | |   +-- randomatic@1.1.5
  |       | |   `-- repeat-string@1.5.4
  |       | +-- preserve@0.2.0
  |       | `-- repeat-element@1.1.2
  |       +-- expand-brackets@0.1.5
  |       | `-- is-posix-bracket@0.1.1
  |       +-- extglob@0.3.2
  |       +-- filename-regex@2.0.0
  |       +-- is-extglob@1.0.0
  |       +-- is-glob@2.0.1
  |       +-- kind-of@3.0.4
  |       | `-- is-buffer@1.1.4
  |       +-- normalize-path@2.0.1
  |       +-- object.omit@2.0.0
  |       | +-- for-own@0.1.4
  |       | | `-- for-in@0.1.6
  |       | `-- is-extendable@0.1.1
  |       +-- parse-glob@3.0.4
  |       | +-- glob-base@0.3.0
  |       | | `-- glob-parent@2.0.0
  |       | `-- is-dotfile@1.0.2
  |       `-- regex-cache@0.4.3
  |         +-- is-equal-shallow@0.1.3
  |         `-- is-primitive@2.0.0
  +-- jest-snapshot@15.1.1
  | +-- jest-diff@15.1.0
  | `-- pretty-format@3.8.0
  +-- jest-util@15.1.1
  | `-- diff@3.0.0
  +-- json-stable-stringify@1.0.1
  | `-- jsonify@0.0.0
  +-- node-notifier@4.6.1
  | +-- cli-usage@0.1.4
  | | +-- marked@0.3.6
  | | `-- marked-terminal@1.6.2
  | |   +-- cardinal@1.0.0
  | |   | +-- ansicolors@0.2.1
  | |   | `-- redeyed@1.0.0
  | |   +-- cli-table@0.3.1
  | |   | `-- colors@1.0.3
  | |   `-- node-emoji@1.4.1
  | |     `-- string.prototype.codepointat@0.2.0
  | +-- growly@1.3.0
  | +-- lodash.clonedeep@3.0.2
  | | +-- lodash._baseclone@3.3.0
  | | | +-- lodash._arraycopy@3.0.0
  | | | +-- lodash._arrayeach@3.0.0
  | | | +-- lodash._baseassign@3.2.0
  | | | | `-- lodash._basecopy@3.0.1
  | | | +-- lodash._basefor@3.0.3
  | | | +-- lodash.isarray@3.0.4
  | | | `-- lodash.keys@3.1.2
  | | |   +-- lodash._getnative@3.9.1
  | | |   `-- lodash.isarguments@3.1.0
  | | `-- lodash._bindcallback@3.0.1
  | +-- minimist@1.2.0
  | +-- semver@5.3.0
  | `-- shellwords@0.1.0
  +-- sane@1.4.1
  | +-- exec-sh@0.2.0
  | | `-- merge@1.2.0
  | +-- minimatch@3.0.3
  | | `-- brace-expansion@1.1.6
  | |   +-- balanced-match@0.4.2
  | |   `-- concat-map@0.0.1
  | +-- walker@1.0.7
  | | `-- makeerror@1.0.11
  | |   `-- tmpl@1.0.4
  | `-- watch@0.10.0
  +-- which@1.2.11
  | `-- isexe@1.1.2
  +-- worker-farm@1.3.1
  | +-- errno@0.1.4
  | | `-- prr@0.0.0
  | `-- xtend@4.0.1
  `-- yargs@5.0.0
    +-- cliui@3.2.0
    | `-- wrap-ansi@2.0.0
    +-- decamelize@1.2.0
    +-- get-caller-file@1.0.2
    +-- lodash.assign@4.2.0
    +-- os-locale@1.4.0
    | `-- lcid@1.0.0
    |   `-- invert-kv@1.0.0
    +-- read-pkg-up@1.0.1
    | `-- read-pkg@1.1.0
    |   +-- load-json-file@1.1.0
    |   | +-- parse-json@2.2.0
    |   | | `-- error-ex@1.3.0
    |   | |   `-- is-arrayish@0.2.1
    |   | +-- pify@2.3.0
    |   | `-- strip-bom@2.0.0
    |   |   `-- is-utf8@0.2.1
    |   +-- normalize-package-data@2.3.5
    |   | +-- hosted-git-info@2.1.5
    |   | +-- is-builtin-module@1.0.0
    |   | | `-- builtin-modules@1.1.1
    |   | `-- validate-npm-package-license@3.0.1
    |   |   +-- spdx-correct@1.0.2
    |   |   | `-- spdx-license-ids@1.2.2
    |   |   `-- spdx-expression-parse@1.0.3
    |   `-- path-type@1.1.0
    +-- require-directory@2.1.1
    +-- require-main-filename@1.0.1
    +-- set-blocking@2.0.0
    +-- string-width@1.0.2
    | +-- code-point-at@1.0.0
    | | `-- number-is-nan@1.0.0
    | `-- is-fullwidth-code-point@1.0.0
    +-- which-module@1.0.0
    +-- window-size@0.2.0
    +-- y18n@3.2.1
    `-- yargs-parser@3.2.0
      `-- camelcase@3.0.0

npm WARN jest-json-bug@1.0.0 No description
npm WARN jest-json-bug@1.0.0 No repository field.

> jest-json-bug@1.0.0 test C:\Users\Ilja\Documents\jest-json-bug
> jest && node ./check-json.js

 PASS  .\program.test.js
  √ program (3ms)

------------|----------|----------|----------|----------|----------------|
File        |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
------------|----------|----------|----------|----------|----------------|
All files   |       50 |      100 |        0 |       50 |                |
 program.js |       50 |      100 |        0 |       50 |              2 |
------------|----------|----------|----------|----------|----------------|
Test Summary
 › Ran all tests.
 › 1 test passed (1 total in 1 test suite, run time 1.166s)
Coverage is not valid JSON! [SyntaxError: Unexpected token U]

npm ERR! Windows_NT 10.0.14393
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "test"
npm ERR! node v5.12.0
npm ERR! npm  v3.8.6
npm ERR! code ELIFECYCLE
npm ERR! jest-json-bug@1.0.0 test: `jest && node ./check-json.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the jest-json-bug@1.0.0 test script 'jest && node ./check-json.js'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the jest-json-bug package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     jest && node ./check-json.js
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs jest-json-bug
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls jest-json-bug
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     C:\Users\Ilja\Documents\jest-json-bug\npm-debug.log
kwonoj commented 7 years ago

I came to hit this issue as well and spent some time to dig into, and found istanbul's json reporter write its key without stringify makes escape chars are losing on windows machines. Created PR to amend those behavior at https://github.com/istanbuljs/istanbul-reports/pull/12 .

Daniel15 commented 7 years ago

Wow, good catch @kwonoj!

cpojer commented 7 years ago

thanks @kwonoj. This should be within the semver range when istanbul gets published so it should be fixed in Jest automatically with a reinstall once your PR gets merged and released. Closing here.

github-actions[bot] commented 3 years ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.