-
Notifications
You must be signed in to change notification settings - Fork 2
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
jest fails with @react-native/polyfills/error-guard.js after upgrade to RN 0.64.0 and react 17.0.1 #152
Comments
Add
Also add the preset something like this in jest.config.js:
Check out for the libraries and update them accordingly to meet latest react version. |
Same issue here, but I get this failure when using typescript. |
|
Any updates on this? Still facing the same issue. |
Same here, with RN 0.63.3 |
Try to create babel.config.js with next content
|
Same here after upgrading ts-jest, jest, and react-native to latest. |
Adding the @react-native to the
|
@rholliger adding that ignore causes a |
doesn't work for me either, tried different patterns similar to the one posted by @rholliger . Still debugging, will keep you updated if I find something |
Have you tried this already: facebook/react-native#23943 (comment) |
I found out the solve ✅
as Problem was in my self jest.config.js file in root (example)
|
any update on this? still test cases breaking on RN version 0.64.2 |
Try renaming your Babel config from |
reason: unable to run jest in new RN version reference: react-native-community/upgrade-support#152
I can confirm this transform works for me! |
Solved my problem. Thanks, @johnhaup! |
This one solved my problem. Great thanks to @interhub. For my case, globals: {
'ts-jest': {
tsconfig: 'tsconfig.spec.json', // as specified by ts-jest
babelConfig: true,
},
},
// ...
transformIgnorePatterns: [], does the trick. Even if I put I am using [email protected], jest@^26.6.3, ts-jest@^26.5.6. The following is my // jest.config.js
module.exports = {
preset: 'react-native',
globals: {
'ts-jest': {
tsconfig: 'tsconfig.spec.json',
babelConfig: true,
},
},
moduleFileExtensions: [
'ts',
'tsx',
'js',
'jsx',
'json',
'node',
],
transform: {
'^.+\\.(js|jsx)$': 'babel-jest',
'^.+\\.(ts|tsx)$': 'ts-jest',
},
// This line should be kept even with nothing to be ignored, otherwise the transform will not take place.
// Not quite sure about the reason.
transformIgnorePatterns: [],
testRegex: '(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$',
testPathIgnorePatterns: [
'<rootDir>/node_modules/',
'\\.snap$',
],
cacheDirectory: '.jest/cache',
}; For |
Other folks in this discussion have mentioned TypeScript as the culprit, but these are actually Flow types. It seems like I would encourage the maintainers to ship code that does not use untranspiled Flow, as that breaks the assumptions a lot of JavaScript tooling makes. |
I'm having a hell of time with this. When I get what should be the right
I've added Related issue with a repository that I can't make work here: JacopoPatroclo/nx-react-native-expo#21 |
Same error by me and i just created new project with typescript and cant run even initial test...
My config:
jest.config.ts
|
Same error
` |
I fixed it by add this: |
It's working for me. |
After debugging I found that renaming the file from babel.config.js
jest.confg.js
|
babel config is needed to run jest tests after upgrading react-native. See: react-native-community/upgrade-support#152 Also, it's part of most libraries either way.
I was able to fix this starting with a
none of the answers above worked for me. no variation of i figured out my adding this transform fixed it:
also note that the this was my
|
Does anyone have a fix for Jest encountered an unexpected token
I have tried everything in this form and nothing can get me past this error. My current jest config is:
and my babel.config.js includes:
|
babel config is needed to run jest tests after upgrading react-native. See: react-native-community/upgrade-support#152 Also, it's part of most libraries either way.
still getting this same issue. for three days now. Have tried anything and everything: Jest Error
jest.config.js
System
package.json
babel.config.js
|
babel config is needed to run jest tests after upgrading react-native. See: react-native-community/upgrade-support#152 Also, it's part of most libraries either way.
not using typescript, not using babel, have no babel config, have no tsconfig... only using this file:
doesn't work, same issue.
feel like it's not transforming files it needs to... |
The solution that worked for me: babel.config.js module.exports = api => {
return {
presets: ['module:metro-react-native-babel-preset'],
...(api.env('test')
? {
plugins: [
'@babel/plugin-proposal-class-properties',
'@babel/plugin-transform-private-methods',
],
}
: {}),
};
}; |
Super weird, but I am working on upgrading a legacy RN application (0.68.5 => 0.71.1) and running into this issue. We are in a monorepo with our node backend, so lots of setup and config files. Long short, our react native project dir ( Definitely looks like an opportunity to clean some things up, but also glad to see some progress. |
I had my project with
So I added it into my devs and modify my My
in
Hope it helps to someone ❤️ |
babel config is needed to run jest tests after upgrading react-native. See: react-native-community/upgrade-support#152 Also, it's part of most libraries either way.
i got this error when i use or i change to specify the babel config file path in the jest.config.ts:
|
Including the above solutions, what DID work, was adding the (note: change module.exports = {
...,
projects: [
{
displayName: 'project-name',
testMatch: ['<rootDir>/src/components/**/__tests__/*.test.tsx'],
},
],
globals: {
'ts-jest': {
tsconfig: 'tsconfig.spec.json', // as specified by ts-jest
babelConfig: true,
},
},
transformIgnorePatterns: [
'/node_modules/(?!(@react-native|react-native)/).*/',
],
setupFilesAfterEnv: ['@testing-library/jest-native/extend-expect'],
testPathIgnorePatterns: [
'/node_modules/(?!(@react-native|react-native)/).*/',
],
modulePathIgnorePatterns: [
'/node_modules/(?!(@react-native|react-native)/).*/',
]
} My issue also had a separate issue, which was looking at the root directory, and I had to tell it to only worry about this project. So I had to solve the original issue above, AND tell it not to look in the root project directory. |
After testing almost every solution posted above, the only combination that worked for me was the following: Please note that I'm using EXPO 50 which recommends removing the Babel config file and use the Metro config file. I'm not sure if there is a way to solve this WITHOUT adding the Babel file. babel.config.js ({ loose: true } is key for this to work, thanks @elenitaex5 🙏🏼)
jest.config.js (remove the "jest" entry from your package.json
Make sure you install this dev dependencies: |
babel config is needed to run jest tests after upgrading react-native. See: react-native-community/upgrade-support#152 Also, it's part of most libraries either way.
Wow it's 2024 and it's amazing this issue is still open years later with so many solutions and none that actually work for me. I'm in a bare react native project, no expo "for reasons". And I just wanna write some test code. Amazing that it's a multi-hour research project just to write some unit tests. It actually saeems quicker to write a new test runner than to spend time learning how to configure jest and babel and all this mess. Sigh... |
For anyone still coming here for this issue, it could be from jest upgrade breaking the location of your |
babel config is needed to run jest tests after upgrading react-native. See: react-native-community/upgrade-support#152 Also, it's part of most libraries either way.
babel config is needed to run jest tests after upgrading react-native. See: react-native-community/upgrade-support#152 Also, it's part of most libraries either way.
babel config is needed to run jest tests after upgrading react-native. See: react-native-community/upgrade-support#152 Also, it's part of most libraries either way.
As detailed above, my project uses TypeScript. Initially, I focused only on handling TypeScript syntax and overlooked the Flow syntax within react-native (Flow syntax is officially used in js files). When running jest tests, the default configuration ignores node_modules. The first step is to add exceptions for the relevant react-native code (if you have other code to handle, you can follow the same strategy). {
...
transformIgnorePatterns: [
'node_modules/(?!(react-native|react-native-button|@react-native-community|@react-navigation)/)'
],
...
} Next, we need to add support for Flow syntax by including the following settings. {
...
transform: {
'^.+\\.(js|jsx|ts|tsx)$': 'ts-jest',
'^.+\\.(js|jsx|ts|tsx)$': ['babel-jest', {
// Specify a custom Babel configuration
presets: ['module:metro-react-native-babel-preset'],
plugins: ['@babel/plugin-transform-flow-strip-types']
}],
},
...
} Here is my complete configuration for your reference. module.exports = {
preset: 'react-native',
transform: {
'^.+\\.(js|jsx|ts|tsx)$': 'ts-jest',
'^.+\\.(js|jsx|ts|tsx)$': ['babel-jest', {
// Specify a custom Babel configuration
presets: ['module:metro-react-native-babel-preset'],
plugins: ['@babel/plugin-transform-flow-strip-types']
}],
},
setupFilesAfterEnv: ['@testing-library/jest-native/extend-expect'],
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
testPathIgnorePatterns: ['/node_modules/', '/android/', '/ios/'],
transformIgnorePatterns: [
'node_modules/(?!(react-native|react-native-button|@react-native-community|@react-navigation)/)'
],
}; |
I was having the same issue, what worked for me was giving an empty array |
This one worked for me, I was having troubles with createNativeStackNavigator from React Navigation
Setting transformIgnorePatterns with an empty array solved the error. |
babel config is needed to run jest tests after upgrading react-native. See: react-native-community/upgrade-support#152 Also, it's part of most libraries either way.
I got this error when upgrading React Native from 70.15 to 71.19, and having to upgrade jest, and ts-jest. My fix was to add the metro react native preset to the babel-jest transform like this
|
Environment
System:
OS: Linux 5.4 Ubuntu 18.04.5 LTS (Bionic Beaver)
CPU: (8) x64 Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
Memory: 1.29 GB / 15.52 GB
Shell: 4.4.20 - /bin/bash
Binaries:
Node: 14.5.0 - ~/.nvm/versions/node/v14.5.0/bin/node
Yarn: 1.22.5 - /usr/bin/yarn
npm: 6.14.9 - ~/.nvm/versions/node/v14.5.0/bin/npm
Watchman: 20200920.192359.0 - /usr/local/bin/watchman
SDKs:
Android SDK:
API Levels: 19, 24, 25, 27, 28, 29, 30
Build Tools: 28.0.3, 29.0.2, 29.0.3, 30.0.0
System Images: android-22 | Google APIs Intel x86 Atom, android-25 | Google APIs Intel x86 Atom, android-28 | Google Play Intel x86 Atom
Android NDK: Not Found
IDEs:
Android Studio: Not Found
Languages:
Java: 11.0.10 - /usr/bin/javac
npmPackages:
@react-native-community/cli: Not Found
react: 17.0.1 => 17.0.1
react-native: 0.64.0 => 0.64.0
npmGlobalPackages:
react-native: Not Found
Things I’ve done to figure out my issue
did react-native upgrade which upgraded the appplication
Upgrading version
react-native 0.64.0
react 17.0.1
Description
After the upgrade, I did yarn test but all tests fail with error:
The text was updated successfully, but these errors were encountered: