I can\'t test an application which was created with create-react-app.
All guides says that test is working by default, but when I try "yarn test", it requir
If you have not used create-react-app
for the project:
jest, jest-cli, jest-environment-jsdom
from package.json
.npm --save i
or yarn add
to install all of them so that each of them have same version.This should work!
In my case I was missing the jest-environment-jsdom
package.
In my case it throws this error because I create my React project using create-react-app
and install jest
. In certain way it conflicts.
I solve it by removing jest
from my project and using the create-react-app
test script.
For me, it was the module import binding. Basically, for me, it was the module export/import binding.
Basically if it was exported using module.export
, you should import using require
. If it was exported using export
or export default
, you should import using import
.
No mixing is allowed here.
You shouldn't need to install jest-cli yourself. It should come out of the box.
Try the following:
npm install
or yarn install
.Add a fresh version of "jest-environment-jsdom" to file "package.json".
After an evening of banging my head on this issue, I think I've finally resolved it. First, let me describe the problem:
I was also getting:
TypeError: environment.teardown is not a function
Because I was trying to upgrade Jest to the latest version. When starting an app with Create-React-App, it installs an earlier version of Jest that does not include all of the features. I was interested in using the inlineSnapshots feature, which allows you to assert rendered components against HTML markup inside your test file.
There are a few things not included in the documentation of either Create-React-App or Jest which you should be aware of. If you want to use newer Jest features, like inlineSnapshots, then you need to do the following:
A docblock is a JavaScript block comment with a pragma mark "@". For setting the Jest environment, you would add this to the top of every test file:
/**
* @jest-environment jsdom
*/
This instructs jest to run the test in the jsdom environment, which should match what is passed in the test scripts section of your package.json file. Mine looks something like this (ignore the parts with react-app-rewired):
"scripts": {
"start": "yarn run flow && react-app-rewired start",
"build": "yarn run flow && react-app-rewired build",
"test": "react-scripts test --env=jsdom"
}
After doing all of this, I was finally able to get inlineSnapshots to work.