-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Closed
Closed
Copy link
Labels
Description
Version
27.4.2
Steps to reproduce
- Clone https://github.com/joshkel/jest-transform-cache-demo
- Run
yarn install - Install and configure nvm, if you haven't already.
- Run the commands from the README:
nvm use stable yarn jest test.ts nvm use lts/fermium yarn jest test.ts
Expected behavior
Successful test results in both Node stable (16.x) and Node.js LTS/fermium (14.x).
Actual behavior
When jest-transform processes the test file, it detects that Node.js 16.x supports the ??= operator, so it doesn't transform it, and it caches the resulting transformed file. When Jest is rerun under Node 14.x, jest-transform reuses that cache, even though it's no longer valid, resulting in the following error:
yarn run v1.22.17
$ /Users/joshkel/tmp/node_modules/.bin/jest test.ts
FAIL ./test.ts
● Test suite failed to run
Jest encountered an unexpected token
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
describe("test", () => {
By default "node_modules" folder is ignored by transformers.
Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
• If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation
Details:
/Users/joshkel/tmp/test.ts:6
a ??= 2;
^^^
SyntaxError: Unexpected token '??='
Additional context
Brainstorming possible solutions:
- jest-transform could include the Node version (full version? major version?) when determining its cache directory. (Would that suffice, or are there other factors that go into transformed files that are also not considered?)
- When Jest hits a "Jest encountered an unexpected token" error, it could decide that the jest-transform results must be invalid and clear the cache.
- When Jest hits a "Jest encountered an unexpected token" error, its "Here's what you can do:" guidance could provide instructions on clearing the jest-transform cache.
Environment
System:
OS: macOS 12.0.1
CPU: (8) x64 Intel(R) Core(TM) i7-4980HQ CPU @ 2.80GHz
Binaries:
Node: 14.17.1 - ~/.nvm/versions/node/v14.17.1/bin/node
Yarn: 1.22.17 - /usr/local/bin/yarn
npm: 6.14.13 - ~/.nvm/versions/node/v14.17.1/bin/npm
npmPackages:
jest: ^27.0.0 => 27.4.3