jestjs / jest

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

Jest 21 and broken package.json #4420

Closed ai closed 7 years ago

ai commented 7 years ago

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

bug

What is the current behavior?

Jest raise error if it finds broken package.json somewhere in the project (for example, in text fixtures).

If the current behavior is a bug, please provide the steps to reproduce and either a repl.it demo through https://repl.it/languages/jest or a minimal repository on GitHub that we can yarn install and yarn test.

  1. Add test/fixtures/package.json file with { content.
  2. npx jest

What is the expected behavior?

Raise error only if PROJECT/package.json is broken.

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.

I saw error only after update to Jest 21.

➤ node -v
v8.4.0
➤ yarn --version
0.27.5

Stacktrace:

SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at module.exports (/home/ai/Dev/browserslist/node_modules/jest-haste-map/build/worker.js:66:29)
    at handle (/home/ai/Dev/browserslist/node_modules/worker-farm/lib/child/index.js:44:8)
    at process.<anonymous> (/home/ai/Dev/browserslist/node_modules/worker-farm/lib/child/index.js:51:3)
    at emitTwo (events.js:125:13)
    at process.emit (events.js:213:7)
    at emit (internal/child_process.js:774:12)
    at _combinedTickCallback (internal/process/next_tick.js:141:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)
cpojer commented 7 years ago

If you have invalid JSON files in your project intentionally, you can ignore them using modulePathIgnorePatterns in your config.

juanpicado commented 6 years ago

@cpojer could you provide an example? I'm not sure how modulePathIgnorePatterns resolves those paths. I have the same test/fixtures/package.json pattern but in my case the json file is broken intentionally. I'm getting the same SyntaxError: Unexpected token } in JSON at position 32 all the time trying multiple patterns.

juanpicado commented 6 years ago

Well ... digging a bit into jest-haste-map I found they user internally an option called ignorePattern which is being set by jest-runtime in the following part:

const ignorePattern = new RegExp(
      [config.cacheDirectory].concat(config.modulePathIgnorePatterns).join('|'),
    );

This becames a huge RegEx that ignore those files/folders to be crawled by jest-haste-map and thus display errors or warnings. In my case, it was inteded to have some corrupted JSON files in my fixtures for testing purposes that jest was trying to parse them causing the origin issue of this ticket.

With the following setup (be careful with the Regex otherwise will never match) it works perfectly.

 modulePathIgnorePatterns: [
    '<rootDir>/unit/partials/mock-store/.*/package.json',
    '<rootDir>/functional/store/.*/package.json',
    '<rootDir>/unit/partials/store/.*/package.json',
    '<rootDir>/../coverage',
    '<rootDir>/../docs',
    '<rootDir>/../debug',
    '<rootDir>/../scripts',
    '<rootDir>/../.circleci',
    '<rootDir>/../tools',
    '<rootDir>/../wiki',
    '<rootDir>/../systemd',
    '<rootDir>/../flow-typed',
    '<rootDir>unit/partials/mock-store/.*/package.json',
    '<rootDir>functional/store/.*/package.json',
    '<rootDir>/../build',
    '<rootDir>/../.vscode/',
  ],

more info: https://github.com/verdaccio/verdaccio/blob/master/jest.config.unit.js

One more thing, excluding files from jest runtime execution, it seems to boost a bit since has less file to parse. https://github.com/verdaccio/verdaccio/commit/ccb73404a89bd08ba53672e735424ffa05ab924d

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.