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-runner Class private methods are not enabled with version 29.0.0 #13173

Closed
mihaelaLo opened this issue Aug 25, 2022 · 12 comments
Closed

Comments

@mihaelaLo
Copy link

Version

29.0.0

Steps to reproduce

jest --runInBand

Expected behavior

Tests should run without errors

Actual behavior

I receive the following error:

SyntaxError: /Users/myUser/Workspace/projectName/node_modules/jest-runner/build/index.js: Class private methods are not enabled.
  91 |   }
  92 |
> 93 |   async #createInBandTestRun(tests, watcher) {
     |   ^
  94 |     process.env.JEST_WORKER_ID = '1';
  95 |     const mutex = (0, _pLimit().default)(1);
  96 |     return tests.reduce(
    at File.buildCodeFrameError (/Users/mihaela.lorincz/Workspace/the-st-james-mobile-app/node_modules/@babel/core/lib/transformation/file/file.js:249:12)
    at NodePath.buildCodeFrameError (/Users/mihaela.lorincz/Workspace/the-st-james-mobile-app/node_modules/@babel/core/node_modules/@babel/traverse/lib/path/index.js:143:21)
    at verifyUsedFeatures (/Users/mihaela.lorincz/Workspace/the-st-james-mobile-app/node_modules/@babel/helper-create-class-features-plugin/lib/features.js:97:18)
    at PluginPass.Class (/Users/mihaela.lorincz/Workspace/the-st-james-mobile-app/node_modules/@babel/helper-create-class-features-plugin/lib/index.js:72:44)
    at newFn (/Users/mihaela.lorincz/Workspace/the-st-james-mobile-app/node_modules/@babel/core/node_modules/@babel/traverse/lib/visitors.js:177:21)
    at NodePath._call (/Users/mihaela.lorincz/Workspace/the-st-james-mobile-app/node_modules/@babel/core/node_modules/@babel/traverse/lib/path/context.js:53:20)
    at NodePath.call (/Users/mihaela.lorincz/Workspace/the-st-james-mobile-app/node_modules/@babel/core/node_modules/@babel/traverse/lib/path/context.js:40:17)
    at NodePath.visit (/Users/mihaela.lorincz/Workspace/the-st-james-mobile-app/node_modules/@babel/core/node_modules/@babel/traverse/lib/path/context.js:100:31)
    at TraversalContext.visitQueue (/Users/mihaela.lorincz/Workspace/the-st-james-mobile-app/node_modules/@babel/core/node_modules/@babel/traverse/lib/context.js:105:16)
    at TraversalContext.visitMultiple (/Users/mihaela.lorincz/Workspace/the-st-james-mobile-app/node_modules/@babel/core/node_modules/@babel/traverse/lib/context.js:74:17)

error Command failed with exit code 1.

Additional context

my jest config:

  "jest": {
    "verbose": true,
    "moduleNameMapper": {
      "^[./a-zA-Z0-9@$_-]+\\.(png|jpg)$": "<rootDir>/node_modules/react-native/jest/assetFileTransformer.js"
    },
    "modulePaths": [
      "<rootDir>/src/"
    ],
    "preset": "react-native",
    "setupFiles": [
      "./node_modules/react-native-gesture-handler/jestSetup.js",
      "./jest/setupTests.js"
    ],
    "testEnvironment": "node",
    "transform": {
      "^.+\\.js$": "babel-jest"
    },
    "transformIgnorePatterns": [
      "node_modules/(?!react-native|react-native-gesture-handler|react-navigation|@react-native-async-storage/async-storage|redux-persist|react-native-keychain|)/"
    ]
  }

I do not get this error for versions 28.1.3 and 26.6.3

Environment

System:
    OS: macOS 12.5.1
    CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
  Binaries:
    Node: 18.8.0 - /usr/local/bin/node
    Yarn: 1.22.17 - /usr/local/bin/yarn
    npm: 8.18.0 - /usr/local/bin/npm
  npmPackages:
    jest: 29.0.0 => 29.0.0
@SimenB
Copy link
Member

SimenB commented Aug 25, 2022

Why are you transpiling jest-runner?

@mihaelaLo
Copy link
Author

mihaelaLo commented Aug 26, 2022

@SimenB I do not have jest-runner in my dependencies, it is installed by jest core library. I have a JS project

@SimenB
Copy link
Member

SimenB commented Aug 26, 2022

It's babel that's complaining about the syntax, not Node itself. If you just do require('jest-runner') it'll work fine for you since the Node version supports it. So something in your setup is running Babel on Jest's modules.

Do you have a reproduction I can check out?

@mihaelaLo
Copy link
Author

No, it is in my prod app, on a private repo. Is there any other info that might help you? Thanks for help

@SimenB
Copy link
Member

SimenB commented Aug 26, 2022

Without a reproduction there's not much I can do. I'd take a look at your setupFiles, do any of them import jest-runner?

@tobua
Copy link

tobua commented Aug 26, 2022

Ran into the same issue with React Native. Seems like metro-react-native-babel-preset is involved. Here is the most basic reproduction I could come up with:

{
  "scripts": {
    "test": "jest"
  },
  "dependencies": {
    "@babel/runtime": "^7.18.9",
    "babel-jest": "^29.0.1",
    "jest": "^29.0.1",
    "metro-react-native-babel-preset": "^0.72.1"
  },
  "jest": {
    "transformIgnorePatterns": []
  }
}

babel.config.json

{ "presets": ["module:metro-react-native-babel-preset"] }

test/basic.test.js

import double from "./double";

test("Doubles the number", () => {
  expect(double(2)).toBe(4);
});
// test/double.js
export default (value) => value * 2;

As a workaround ignoring the already mentioned jest-runner from being transpiled works around the issue:

"transformIgnorePatterns": [
  "jest-runner"
]

@SimenB
Copy link
Member

SimenB commented Aug 26, 2022

Ah, nice 👍 that explains why our integration test doesn't fail: https://github.com/facebook/jest/blob/dde24c85698259d9ce887ede9646847d0fff9554/examples/react-native/jest.config.js#L7

Still, weird it triggers... I'll look into it next week 🙂


But yeah, don't transpile node_modules would be best 🙂

@tobua
Copy link

tobua commented Aug 26, 2022

Right, transforming all node_modules is kind of a bad idea. The initial post is using a popular hack to ignore all node_modules except some that are published as ESM and therefore need to be transpiled as well.

Noticed that there is a / slash at the end which prevents jest-runner from being ignored for some reason.

"transformIgnorePatterns": [
  "node_modules/(?!react-native|any-react-native-esm-package)"
]

When that slash is removed jest-runner doesn't have to be listed explicitly anymore.

@SimenB
Copy link
Member

SimenB commented Aug 26, 2022

Ah, that explains it (over-eager negation). Not sure there's anything for Jest to do? We target specific node versions - if other tooling has other syntax requirements that's not on Jest I think?

@tobua
Copy link

tobua commented Aug 26, 2022

I was able to successfully upgrade a React Native project with the error described here. Definitely nothing for Jest to do here as node_modules generally shouldn't be transformed. Metro could add @babel/plugin-proposal-private-methods to metro-react-native-babel-preset but I don't think that's necessary for now.

Edit: Some React Native packages also have to be transpiled because they are published with flow type annotations.

@SimenB
Copy link
Member

SimenB commented Aug 26, 2022

Agreed, thanks!

@SimenB SimenB closed this as not planned Won't fix, can't repro, duplicate, stale Aug 26, 2022
@github-actions
Copy link

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 Sep 26, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants