globalSetup, globalTeardown and testEnvironment does not recognize moduleNameMappers config
See original GitHub issue🐛 Bug Report
Modules defined in globalSetup
, globalTeardown
and in testEnvironment
are not being recognized by moduleNameMappers
(and maybe others like transform
). I think this may be a bug, otherwise consider this as a feature request.
This is maybe related to #5164 and #6179
To Reproduce
- Create a moduleNameMapper entry in jest config file;
- Create
globalSetup
,globalTeardown
ortestEnvironment
files; - Import the module alias on those files;
- Run jest;
Cannot find module '<module alias>'
error should appear.
Expected behavior
globalSetup
, globalTeardown
or testEnvironment
files should import modules defined on moduleNameMappers
config.
Link to repl or repo
- In the file
jest.config.js
uncommentglobalSetup
,globalTeardown
ortestEnvironment
config entries; - Open new terminal
- Run
npm run test
https://codesandbox.io/s/jest-module-alias-bug-v3wij
envinfo
System:
OS: Linux 5.4 Debian GNU/Linux 10 (buster) 10 (buster)
CPU: (8) x64 Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz
Binaries:
Node: 14.16.1 - ~/.nvm/versions/node/v14.16.1/bin/node
Yarn: 1.22.10 - ~/.nvm/versions/node/v14.16.1/bin/yarn
npm: 6.14.12 - ~/.nvm/versions/node/v14.16.1/bin/npm
npmPackages:
jest: ^27.0.6 => 27.0.6
System:
OS: Linux 5.4 Debian GNU/Linux 10 (buster) 10 (buster)
CPU: (8) x64 Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz
Binaries:
Node: 14.16.1 - ~/.nvm/versions/node/v14.16.1/bin/node
Yarn: 1.22.10 - ~/.nvm/versions/node/v14.16.1/bin/yarn
npm: 6.14.12 - ~/.nvm/versions/node/v14.16.1/bin/npm
npmPackages:
jest: ^27.0.6 => 27.0.6
System:
OS: Linux 5.4 Debian GNU/Linux 10 (buster) 10 (buster)
CPU: (8) x64 Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz
Binaries:
Node: 14.16.1 - ~/.nvm/versions/node/v14.16.1/bin/node
Yarn: 1.22.10 - ~/.nvm/versions/node/v14.16.1/bin/yarn
npm: 6.14.12 - ~/.nvm/versions/node/v14.16.1/bin/npm
npmPackages:
jest: ^27.0.6 => 27.0.6
Issue Analytics
- State:
- Created 2 years ago
- Reactions:24
- Comments:10 (1 by maintainers)
Top Results From Across the Web
Jest's globalSetup make my tests to not being recognized
In my custom GlobalSetup I imported the @Shelf/jest-mongodb/setup and add it to mine. const jestMongoSetup = require("@shelf/jest-mongodb/setup") ...
Read more >Configuring Jest
Any global variables that are defined through globalSetup can only be read in globalTeardown . You cannot retrieve globals defined here in your...
Read more >Top 5 ts-jest Code Examples - Snyk
Learn more about how to use ts-jest, based on ts-jest code examples created from the most popular ways it is used in public...
Read more >Configuring Jest compiled - w3resource
When you are using the --config option, the JSON file should not ... that are defined using globalSetup can only be read in...
Read more >Configuring Vitest
vitest will read your root vite.config.ts when it is present to match with the plugins and setup as your Vite app. If you...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hello fellow developer who spent
manya few hoursin ragebugfixing before stumbling across this issue, like me. I might have a quick workaround for you:Install tsconfig-paths and add
import 'tsconfig-paths/register';
on top of yourglobalSetup
orglobalTeardown
files.This is such a frustrating bug, with
testEnvironment
you can remove tonnes of boilerplate setup and teardown code from all of your tests.But unfortunately its not usable in a modern TS application, as pretty much every app will be using either
paths
or at least abaseDir
to allow relative imports.Therefore the
moduleNameMapper
is requiredSo as it currently stands this bug is blocking out this feature for TS jest users