Jest with TypeScript: TypeError: environment.teardown is not a function

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Jest with TypeScript: TypeError: environment.teardown is not a function



I am getting this error when I try to run my tests with Jest:


FAIL src/__tests__/jokeGenerator.test.tsx
● Test suite failed to run

TypeError: environment.teardown is not a function

at node_modules/jest-runner/build/run_test.js:230:25



I came across a possible solution here: How to solve TypeError: environment.teardown is not a function?



But after doing what was suggested: removing my yarn.lock file, node_modules folder, removing Jest from my package.json, and reinstalling everything again with yarn--I encountered a new problem:


● Test suite failed to run

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string

at assertPath (path.js:39:11)
at Object.relative (path.js:1173:5)
at Object.getCacheKey (node_modules/ts-jest/dist/utils/get-cache-key.js:15:16)



I have a hunch that the reason the previous solution worked for others was because they used create-react-app and then installed a conflicting version of jest alongside it. If that is the case, then the above solution does not apply to my problem because I did not use create-react-app.


create-react-app


create-react-app



So I reinstalled jest and @types/jest and now have the same initial problem...



This is my webpack config:


module.exports =
entry: './src/index.tsx',
devServer:
contentBase: __dirname + '/dist/',
,
module :
rules: [

test: /.tsx?$/,
exclude: /node_modules/,
use:
loader: 'ts-loader'

,

test: /.css$/,
exclude: /node_modules/,
use: [
'style-loader',

loader: 'typings-for-css-modules-loader?modules?named',
options:
modules: true,
namedExport: true


]

]




This is my package.json:



"name": "practice-testing",
"private": true,
"version": "1.0.0",
"description": "",
"scripts":
"start": "webpack-dev-server --mode development --open --hot",
"build": "webpack --mode production",
"test": "jest"
,
"keywords": ,
"author": "",
"license": "ISC",
"devDependencies":
"@types/jest": "^23.3.1",
"@types/react": "^16.4.9",
"@types/react-dom": "^16.0.7",
"babel-core": "^6.26.3",
"babel-jest": "^23.4.2",
"css-loader": "^1.0.0",
"css-modules": "^0.3.0",
"jest": "^23.5.0",
"react-testing-library": "^5.0.0",
"style-loader": "^0.22.1",
"ts-jest": "^23.1.3",
"ts-loader": "^4.4.2",
"typescript": "^3.0.1",
"typings-for-css-modules-loader": "^1.7.0",
"webpack": "^4.16.5",
"webpack-cli": "^3.1.0",
"webpack-dev-server": "^3.1.5"
,
"dependencies":
"react": "^16.4.2",
"react-dom": "^16.4.2"
,
"jest":
"testEnvironment": "node"




This my jest config:


module.exports = (\.



And lastly, this is my tsconfig:



"compilerOptions":
"module": "commonjs",
"target": "es5",
"jsx": "react",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"sourceMap": true,
,
"include": [
"./src/**/*",
],
"exclude": [
"node_modules",
]




1 Answer
1



TLDR



This error often means there is a jest-environment-jsdom and/or jest-environment-node installed at the root of node_modules that is incompatible with the version of Jest being used to run the tests.


jest-environment-jsdom


jest-environment-node


node_modules


Jest



Details



This one was interesting.



The problem is css-modules. It has a dependency on jest@20.0.4 (that should have been under its devDependencies).


css-modules


jest@20.0.4



jest@20.0.4 ends up installing jest-environment-jsdom@20.0.3 and jest-environment-node@20.0.3 which end up in the root of node_modules.


jest@20.0.4


jest-environment-jsdom@20.0.3


jest-environment-node@20.0.3


node_modules



jest@^23.5.0 is installed which installs jest-environment-jsdom@^23.4.0 and jest-environment-node@^23.4.0 in multiple places within node_modules/jest, but not at the root level of node_modules since the 20.0.3 versions are there.


jest@^23.5.0


jest-environment-jsdom@^23.4.0


jest-environment-node@^23.4.0


node_modules/jest


node_modules


20.0.3



When a testEnvironment is specified for Jest, the resolve process looks for the environment. The first place it tries is within the project which in this case is resolving to the 20.0.3 versions.


testEnvironment


Jest


20.0.3



Those earlier versions of the test environments do not contain everything required by later versions of Jest, including a definition for teardown().


Jest


teardown()



Remove css-modules from package.json, delete your package-lock.json and/or yarn.lock and node_modules and run npm install and that should clear things up.


css-modules


package.json


package-lock.json


yarn.lock


node_modules


npm install



(Note that css-modules only has 133 weekly downloads and no listed github site, I'm guessing it was added as a dependency by mistake, it is not associated with CSS Modules)


css-modules





Great catch! Thank you!
– Matt
Aug 12 at 20:34






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard