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

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.

Details

This one was interesting.

The problem is css-modules. It has a dependency on [email protected] (that should have been under its devDependencies).

[email protected] ends up installing [email protected] and [email protected] which end up in the root of 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.

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.

Those earlier versions of the test environments do not contain everything required by later versions of Jest, including a definition for 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.

(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)


Just in case anyone else stumbles on this inside a project using yarn workspaces I resolved the same problem by not hoisting jest to the project root:

"workspaces": {
    "packages": [
        "packages/*"
    ],
    "nohoist": [
        "**/jest/**",
        "**/jest"
    ]
},