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

How do i use jest with create react app #2564

Closed
gate3 opened this issue Jun 19, 2017 · 13 comments

Comments

@gate3
Copy link

commented Jun 19, 2017

Hi, it says in the documentation that create-react-app uses jest. I tried using it directly with npm test and setting jest as my test in package.json, but it says jest isn't recognized. I installed jest and then tried to run the test again, it shows me error on es6 import and export functionalities.

What is the actual way of using jest with create-react-app. Thank you.

@luftywiranda13

This comment has been minimized.

Copy link
Contributor

commented Jun 19, 2017

it's configured by default under the hood. no need to install it.
the default configuration is also more than enough in most common situation.

BTW, is there any special case you wanna do?
basically to run test, you just need to type npm test.
https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#npm-test

@Timer

This comment has been minimized.

Copy link
Collaborator

commented Jun 19, 2017

You shouldn't need to change anything to use Jest!

If you changed the test script in your package.json, please revert that.

@Timer

This comment has been minimized.

Copy link
Collaborator

commented Jun 20, 2017

I'll close this, but please let us know if you need more help!

@Timer Timer closed this Jun 20, 2017
@gate3

This comment has been minimized.

Copy link
Author

commented Jun 26, 2017

Hi, I created a new app and it works out of the box now. Could not quite figure out why the last one didn't work. Thanks

@gaearon

This comment has been minimized.

Copy link
Member

commented Jun 26, 2017

Maybe you were using npm@5 which is currently buggy, and maybe it hasn't installed Jest properly.

@pRoy24

This comment has been minimized.

Copy link

commented Oct 24, 2017

The Jest test runner is unable to resolve absolute path imports inside my component to be tested.

image

@charlestati

This comment has been minimized.

Copy link

commented Jan 20, 2018

In case someone (like me) only wants to use Jest's --coverage option, it can be used with CRA:

yarn test -- --coverage

See #coverage-reporting

@gaearon

This comment has been minimized.

Copy link
Member

commented Jan 20, 2018

Btw if you use Yarn 1.0+ you don't need the extra -- there. Just yarn test --coverage is good.

@m-gonalons-camps

This comment has been minimized.

Copy link

commented Mar 4, 2018

@pRoy24 I'm having the same problem. Did you solve it?

@bgits

This comment has been minimized.

Copy link

commented Apr 6, 2018

When I import a component that uses absolute paths the test fails to run with:

    TypeError: (0 , _utils.makeActionCreator) is not a function

However when I use a component that does not use absolute paths such as:(https://github.com/facebook/jest/blob/master/examples/snapshot/Link.react.js), the test runs fine.

  System:
    OS: macOS High Sierra 10.13.3
    CPU: x64 Intel(R) Core(TM) i7-4750HQ CPU @ 2.00GHz
  Binaries:
    Node: 8.11.0
    Yarn: 1.5.1
    npm: 5.8.0
@m-gonalons-camps

This comment has been minimized.

Copy link

commented Apr 7, 2018

If anyone is having the same problem as me and pRoy24, I solved it by adding this option --modulePaths=src to package.json test script.

package.json

  "scripts": {
    "start": "react-scripts-ts start",
    "build": "react-scripts-ts build",
    "test": "react-scripts-ts test --env=jsdom --modulePaths=src",
    "eject": "react-scripts-ts eject"
  },

src being the root folder name of your code :)

@chidananda-mn

This comment has been minimized.

Copy link

commented Jul 1, 2018

I have installed jest
I tried npm test command to run test cases but unfortunately am facing below issue
any solutions

Error: Cannot find module 'E:\ReactTesting\TestProject\my-app1 - loginappcopy\node_modules\jest-cli'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:594:15)
at Function.Module._load (internal/modules/cjs/loader.js:520:25)
at Module.require (internal/modules/cjs/loader.js:650:17)
at require (internal/modules/cjs/helpers.js:20:18)
at getJest (E:\ReactTesting\TestProject\my-app1 - loginappcopy\node_modules\react-scripts\node_modules\jest\node_modules\jest-cli\build\cli\getJest.js:22:12)
at Object.run (E:\ReactTesting\TestProject\my-app1 - loginappcopy\node_modules\react-scripts\node_modules\jest\node_modules\jest-cli\build\cli\index.js:39:48)
at Object. (E:\ReactTesting\TestProject\my-app1 - loginappcopy\node_modules\react-scripts\scripts\test.js:104:6)
at Module._compile (internal/modules/cjs/loader.js:702:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
at Module.load (internal/modules/cjs/loader.js:612:32)
npm ERR! Test failed. See above for more details.

@bugzpodder

This comment has been minimized.

Copy link
Contributor

commented Jul 1, 2018

might be worth removing node_modules/ and re-install using npm install

@lock lock bot locked and limited conversation to collaborators Jan 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
10 participants
You can’t perform that action at this time.