-
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
[v0.57.0-rc.3] New project unit testing doesn't work #20876
Comments
Ok, so it seems like #3 in this comment fixes this. I wonder if this should be the way the new project template is set so that folks have a functional jest setup out of the box. I've updated my demo repo. |
I can still experience this on react-native@0.57 |
As @jeremywiebe said, just add
to your |
I'm facing the same error with typescript: Here are my devDependencies in
|
Update: Here are the steps to reproduce. Create a new react native project: Install all Jest and Enzyne related packages: Add the jest configuration to
Add a file
Last but not least add a basic test (
If you now try to run the test, the error message you get is:
|
I'm experiencing the exact same problem. |
Facing the same issue. |
Below are minimal These are based on manually copying the JS
TS
|
As mentioned in facebook/metro#242 (comment), the solution presented above (i.e. using Jest's preprocessor in the In other words, tests involving manual mocks that previously succeeded do not anymore. Here's an example:
The above happens even though the manual mock defines the jest.mock('react-native-languages', () => {
return {
language: 'en-US',
addEventListener: jest.fn(),
};
}); Anyone else seeing this behavior when Jest manual mocks are involved? |
Yes @maximevaillancourt - we are having the same issue. If we use the jest preprocessor from RN, the tests "run" but Related issue on the "metro" repository: facebook/metro#242 (comment) |
We were running into the following Jest/Babel issue after upgrading to 0.57.0.
But we fixed it by using the package versions specified in @jpdriver's post. 👍
|
@manosim or @maximevaillancourt Did you find a solution/workaround for the |
No nothing yet unfortunately - I feel like that might be an issue with metro bundler. Hopefully one of the maintainers/contributors will point us to the right direction 😃 Will let you know if I figure it out! |
@joh-klein if you find a solution can you please update this thread? I've spent a few hours searching and no luck :/ |
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"
]
} |
CORRECTION: it does work, sorry! What I do now, to make sure, it works: npx jest --clearCache && watchman watch-del-all && rm -rf node_modules && rm -rf $TMPDIR/metro-* && rm -rf $TMPDIR/haste-* && rm -rf $TMPDIR/react-* && yarn |
My tests are also failing because the I tried what @hanford suggested, but still no luck. I have also made sure to clear any stale data like @joh-klein @manosim have you found a solution yet? I'm running RN 0.57.0 package.json
.babelrc
|
Ok, I got it working now by following the steps found here: https://jestjs.io/docs/en/getting-started#using-babel I also had to update to RN 0.57.1 but now I am getting a bunch of errors with |
@rcidt are you using any type of CI? My tests are now all passing locally fail when running on CI 🙇♂️ |
@hanford Yes we use a CI but I haven't pushed up my code yet. Check the node versions |
EDIT: Wow apparently |
For me the problem was what was added to
and the |
I sees lot of solutions that says to add the transform to the jest config. The huge problem with that is the Without the transform (Before Babel 7 and RN 0.56), mocking a module like that would work fine.
However, with the transform it fails because testFunction.mockImplementation is not defined. The mock as it should work stopped working. What would be the other possibility? |
@jpdriver Saved me. Thx~ |
@jpdriver tests are kinda weak-sauce. |
After doing the following:
My mocks came back to life |
UPDATE: This might be a duplicate of #19859 but it definitely exists in 0.57-rc.3 still.
Environment
Run
react-native info
in your terminal and paste its contents here.Description
Unit tests in new projects generated using the
react-native
CLI are broken. Here's the steps to reproduce:`yarn info`
Based on the
yarn list
output, I think I'm gettingbabel-core@6
instead of 7. So I followed the directions on the Jest website to install babel-core and related for Babel 7.Terminal
Further, but now I'm running into an issue which seems to point to missing babel transforms. So I add that npm module, update
.babelrc
and try again. Still no luck.Terminal
Maybe it's related to us using
.babelrc
instead of the newbabel.config.js
? Let's try that.Terminal
Ok, looks like I'm missing
@babel/plugin-transform-classes
. Let's add that:Terminal
It looks like babel isn't picking up that new plugin when I run jest. 😕
Ok, found this comment and that's the solution (item number 3!).
Let's add the
transform
!Terminal
```sh $ vim package.json $ cat package.json { "name": "rntest", "version": "0.0.1", "private": true, "scripts": { "start": "node node_modules/react-native/local-cli/cli.js start", "test": "jest" }, "dependencies": { "react": "16.4.1", "react-native": "0.57.0-rc.3" }, "devDependencies": { "@babel/core": "^7.0.0-rc.3", "babel-core": "^7.0.0-0", "babel-jest": "^23.4.2", "jest": "23.5.0", "metro-react-native-babel-preset": "^0.43.5", "react-test-renderer": "16.4.1", "regenerator-runtime": "^0.12.1" }, "jest": { "preset": "react-native", "transform": { "^.+\\.js$": "/node_modules/react-native/jest/preprocessor.js" } } } $ yarn test yarn run v1.9.4 $ jest PASS __tests__/Button.test.js Button ✓ does stuff (1ms)Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 3.462s
Ran all test suites.
✨ Done in 5.62s.
The text was updated successfully, but these errors were encountered: