question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

  1. Create a moduleNameMapper entry in jest config file;
  2. Create globalSetup, globalTeardown or testEnvironment files;
  3. Import the module alias on those files;
  4. Run jest;
  5. 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

  1. In the file jest.config.js uncomment globalSetup, globalTeardown or testEnvironment config entries;
  2. Open new terminal
  3. 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:open
  • Created 2 years ago
  • Reactions:24
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

11reactions
haschucommented, Jun 30, 2022

Hello fellow developer who spent many a few hours in rage bugfixing 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 your globalSetup or globalTeardown files.

8reactions
dan-cookecommented, May 31, 2022

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 a baseDir to allow relative imports.

Therefore the moduleNameMapper is required

So as it currently stands this bug is blocking out this feature for TS jest users

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found