jest: globalSetup, globalTeardown and testEnvironment does not recognize moduleNameMappers config
π 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,globalTeardownortestEnvironmentfiles; - 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.jsuncommentglobalSetup,globalTeardownortestEnvironmentconfig 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
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 46
- Comments: 21 (1 by maintainers)
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 yourglobalSetuporglobalTeardownfiles.This is such a frustrating bug, with
testEnvironmentyou 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
pathsor at least abaseDirto allow relative imports.Therefore the
moduleNameMapperis requiredSo as it currently stands this bug is blocking out this feature for TS jest users
Still running into the same issue, even with
import 'tsconfig-paths/register';I can share that I am facing the same issue. For my case, I am importing another module which uses aliases in the module defined for
testEnvironment. Initially I had thought it was a misconfiguration in my jest config and I tried all sorts of fixes before I chanced upon this thread and realised that it may be becausemoduleNameMappersis not respected bytestEnvironmentπsame issue here, typescript is great but all typescript ecosystem is pure insanity
Working on an older project that isnβt Typescript related, and also running into the same issue.
Helpful. If it still not works for you guys, make sure that this
importgoes BEFORE any otherimportinglobalSetupfile.Just figured out I can add
--runInBandoption on Jest CLI to fix the problem. Itβs highly possible that it ainβt work well within the parallel context that Jest provides π€globalsdonβt apply as well. π’i also just run into this while trying to change my code work for both CJS and ESM (because ESM requires explicit endings, but those files do not exist when using
ts-jestand having to useglobalSetup/globalTeardownfor some setup)using jest 28