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

[Bug]: "Jest encountered an unexpected token" from stale jest-transform cache #12117

Closed
joshkel opened this issue Dec 3, 2021 · 1 comment · Fixed by #12122
Closed

[Bug]: "Jest encountered an unexpected token" from stale jest-transform cache #12117

joshkel opened this issue Dec 3, 2021 · 1 comment · Fixed by #12122

Comments

@joshkel
Copy link
Contributor

joshkel commented Dec 3, 2021

Version

27.4.2

Steps to reproduce

  1. Clone https://github.com/joshkel/jest-transform-cache-demo
  2. Run yarn install
  3. Install and configure nvm, if you haven't already.
  4. 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
@github-actions
Copy link

github-actions bot commented Jan 5, 2022

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant