-
Notifications
You must be signed in to change notification settings - Fork 24.4k
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 is broken on v0.56 #20614
Comments
Can you run If you believe this information is irrelevant to the reported issue, you may write |
I did this – I'm not sure why the bot is complaining? |
This is probably a duplicate of #19859 and that topic in general is under work. Did you check current |
Duplicate of #19859 |
Haha - I realise it's a duplicate of that issue (I linked it in my description), but that thread was locked with a workaround that didn't seem to work, so thought it was worth making a new issue to:
Apologies if opening a duplicate issue wasn't the correct thing to do – my initial message should have made it a bit clearer. @FibreFoX I haven't tried it against master, no. |
Just as an update for anyone who found this useful: Using After following the upgrade instructions I was able to get jest working by changing my So I went back to using // jest-preprocessor.js
const fs = require("fs")
const config = fs.readFileSync("./.babelrc")
module.exports = require("babel-jest").createTransformer(JSON.parse(config)) And so to hook that up to Jest, I added this to my jest config:
Also, when you're messing around with preprocessors, make sure you run Jest with the |
I finally resolved this issue by bringing in My dependencies: "react": "16.5.1",
"react-native": "^0.57.1",
..
"@babel/core": "^7.1.0",
"@babel/runtime": "^7.0.0",
"babel-core": "^6.26.3",
"babel-eslint": "^8.1.1",
"babel-jest": "^23.6.0",
"babel-plugin-jest-hoist": "^23.2.0",
"babel-plugin-relay": "^1.5.0",
"jest": "^23.6.0",
"metro-react-native-babel-preset": "^0.47.0", and my module.exports = {
"presets": ["module:metro-react-native-babel-preset", "module:react-native-dotenv"],
"plugins": [
"relay",
"jest-hoist"
]
} |
Environment
React Native Environment Info:
System:
OS: macOS High Sierra 10.13.6
CPU: x64 Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
Memory: 844.84 MB / 16.00 GB
Shell: 5.3 - /bin/zsh
Binaries:
Node: 8.11.3 - ~/.nvm/versions/node/v8.11.3/bin/node
Yarn: 1.7.0 - /usr/local/bin/yarn
npm: 5.6.0 - ~/.nvm/versions/node/v8.11.3/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
SDKs:
iOS SDK:
Platforms: iOS 11.4, macOS 10.13, tvOS 11.4, watchOS 4.3
Android SDK:
Build Tools: 23.0.1, 25.0.0, 25.0.1, 25.0.2, 25.0.3, 26.0.1, 26.0.2, 26.0.3, 27.0.3
API Levels: 23, 24, 25, 26, 27
IDEs:
Android Studio: 2.3 AI-162.4069837
Xcode: 9.4.1/9F2000 - /usr/bin/xcodebuild
npmPackages:
react: 16.4.1 => 16.4.1
react-native: 0.56.0 => 0.56.0
npmGlobalPackages:
react-native-cli: 2.0.1
Description
Essentially – upon starting a brand new RN project (
react-native init
), runningyarn test
fails with the error:I'm not sure why this is – I think it's something to do with
babel-preset-react-native@5
. So that's the primary issue that needs fixing, and I think the RN team are already aware of it.This issue has already been discussed in some detail in #19859 and an answer has been given in #19859 (comment) but I found that the solution given didn't seem to fix things for me, so I'm opening a new issue as that thread has been locked.
The thread linked above suggests adding this line to your Jest config to fix things:
As I understand it, this means that
babel-jest
is bypassed, instead using an alternative that was originally intended to be used to run the test suite in the RN repo itself.When I did this, I found that a good number of my tests started to pass, but all the tests I had that had calls to
jest.mock
were still failing. I did some further investigation and it became clear thatjest.mock
was not being hoisted above myimport
calls (and actually, even if I explicitly movedjest.mock
calls above theimport
s, it still fails asimport
s themselves get hoisted above everything).I'm not 100% sure why my
jest.mock
calls were not getting hoisted aboveimport
s. I have a theory: the RN repo itself is written using Haste rather than ES6 imports, so the RN preprocessor does not includebabel-plugin-jest-hoist
. Maybe that's wrong though. But either way, they definitely weren't getting hoisted.So anyway, I deleted the
preprocessor
bit from my Jest config. Instead, I installedbabel-core@^7.0.0-bridge.0
(as per the advice of some of the other answers in the repo). I'm not sure why this is necessary, but I found that that made my Jest tests pass correctly.However, I then found that by adding
babel-core
as an explicit dependency of my project, RN 0.56 would crash while bundling (with the errorError: Cannot find module '@babel/parser'
). So then I added@babel/core@^7.0.0-rc.1
and now both my app and my tests run correctly.I'm aware this solution isn't very nice, as RN is supposed to take care of babel under the hood. However, it's the only way I've been able to upgrade to RN 0.56 and have Jest run correctly. So I thought it was worth flagging.
Reproducible Demo
react-native init testproject
yarn test
. You will see it fails with the errorPlugin 0 specified in "project/node_modules/babel-preset-react-native/index.js" provided an invalid property of "default" (While processing preset: "project/node_modules/babel-preset-react-native/index.js")
The text was updated successfully, but these errors were encountered: