问题
i cant test application which created with create-react-app, all guides says that test is working by default, but when i try "yarn test" it requires install 'jest-cli' and after installation gives an error "TypeError: environment.teardown is not a function".
回答1:
You shouldn't need to install jest-cli yourself, it should come out of the box.
Try the following:
- Delete package-lock.json, yarn.lock and node_modules
- Remove jest from the dependencies in package.json
- Then do
npm installoryarn install.
回答2:
Add a fresh version of "jest-environment-jsdom" to "package.json".
回答3:
In my case it throw 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 create-react-app test script.
回答4:
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's 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:
- yarn add a fresh version of jest and jest-environment-jsdom
- yarn add prettier --dev (This is to facilitate rewriting back into the source file.)
- Add a jest env docblock to the very top of each test script.
A docblock is a JS 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. 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.
回答5:
If you have not used create-react-app for the project:
- Just remove
jest, jest-cli, jest-environment-jsdomfrompackage.json. - Delete node-modules, package-lock.json and yarn.lock.
- Use
npm --save ioryarn addto 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.
回答6:
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.
来源:https://stackoverflow.com/questions/50696201/how-to-solve-typeerror-environment-teardown-is-not-a-function