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.

[JEST] globalSetup and globalTeardown

See original GitHub issue

Current Behavior

When you use globalSetup or globalTeardown in the jest configuration, the imports with custom aliases are throwing an error.

Expected Behavior

I should be able to import from libraries inside those files.

Steps to Reproduce

  1. Generate a nx application and a library.
  2. Inside an app, set the globalSetup into the jest.config file -> globalSetup: ‘<rootDir>/tests/setup.ts’,
  3. In the setup.ts file, import the generated library.
  4. Run the tests.

Failure Logs

Jest: Got error running globalSetup - nx-error/apps/administration/tests/setup.ts, reason: Cannot find module '@ditratec/shared/domain'
Require stack:
- nx-error/apps/administration/tests/setup.ts
- nx-error/node_modules/@jest/transform/node_modules/jest-util/build/requireOrImportModule.js
- nx-error/node_modules/@jest/transform/node_modules/jest-util/build/index.js
- nx-error/node_modules/@jest/transform/build/ScriptTransformer.js
- nx-error/node_modules/@jest/transform/build/index.js
- nx-error/node_modules/jest-runtime/build/index.js
- nx-error/node_modules/@jest/core/build/cli/index.js
- nx-error/node_modules/@jest/core/build/jest.js
- nx-error/node_modules/jest/build/jest.js
- nx-error/node_modules/@nrwl/jest/src/executors/jest/jest.impl.js
- nx-error/node_modules/@nrwl/tao/src/shared/workspace.js
- nx-error/node_modules/@nrwl/tao/src/commands/run.js
- nx-error/node_modules/@nrwl/tao/index.js
- nx-error/node_modules/@nrwl/cli/lib/run-cli.js

Environment

Node : 16.13.2
OS   : win32 x64
npm  : 8.3.1

nx : 13.4.4
@nrwl/angular : 13.4.4
@nrwl/cli : 13.4.4
@nrwl/cypress : 13.4.4
@nrwl/devkit : 13.4.4
@nrwl/eslint-plugin-nx : 13.4.4
@nrwl/express : undefined
@nrwl/jest : 13.4.4
@nrwl/linter : 13.4.4
@nrwl/nest : undefined
@nrwl/next : undefined
@nrwl/node : 13.4.4
@nrwl/nx-cloud : undefined
@nrwl/react : undefined
@nrwl/react-native : undefined
@nrwl/schematics : undefined
@nrwl/tao : 13.4.4
@nrwl/web : undefined
@nrwl/workspace : 13.4.4
@nrwl/storybook : 13.4.4
@nrwl/gatsby : undefined
typescript : 4.4.4
rxjs : 6.6.7
---------------------------------------
Community plugins:
       @nrwl/js: 13.4.4

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
barbados-clemenscommented, Feb 25, 2022

this is the intended experience with jest. the global setup/teardown runs outside of jest. so using things like moduleNameMappers doesn’t work as specified here. https://github.com/facebook/jest/issues/6048 But this Stackoverflow answer recommends using tsconfig-paths/register to register the tsconfig paths in your setup/teardown files. https://stackoverflow.com/questions/65475250/how-to-find-aliases-in-global-jest-setup

Only issue is they expect a tsconfig.json to contain the paths, where nx paths are contained in tsconfig.base.json. You can set this via the env var TS_NODE_PROJECT=tsconfig.base.json before running your commands. Doing this on the provided project it show running now.

Demo of working with tsconfig-paths/register in global set up files and using the TS_NODE_PROJECT env var

https://user-images.githubusercontent.com/23272162/155808942-da137f6d-1689-4df7-b062-b46f8f09bada.mp4

I was thinking of adding the path registration to the jest executor so people wouldn’t have to do any of this and it’ll “magically” work, BUT this will probably break the usage of editor integrations as they won’t be able to resolve the paths. without nx jest running the tests (not ideal).

So right now I’m thinking of not changing anything with the jest executor and updating the jest docs to mention using tsconfig-paths register with the env var so people know how to set it up themselves.

actually I can add it to the preset so it’ll be included when running jest directory or nx. 🎉

Read more comments on GitHub >

github_iconTop Results From Across the Web

Make "globalSetup" and "globalTeardown" work together with ...
Modules should be transformed. Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.
Read more >
Configuring Jest
Configuring Jest. The Jest philosophy is to work great by default, but sometimes you just need more configuration power.
Read more >
Jest global teardown runs before tests finish? - Stack Overflow
No, jest globalSetup and globalTeardown files don't necessarily get run in the same process as your tests. This is because jest parallelises ...
Read more >
jest-pg - npm Package Health Analysis - Snyk
Create postgres database scheme for each test and delete the database after test using jest globalSetup/globalTeardown. For more information about how to ...
Read more >
Setup and Teardown · Jest
Jest provides helper functions to handle this. Repeating Setup For Many Tests #. If you have some work you need to do repeatedly...
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