[JEST] globalSetup and globalTeardown
See original GitHub issueCurrent 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
- Generate a nx application and a library.
- Inside an app, set the
globalSetup
into the jest.config file -> globalSetup: ‘<rootDir>/tests/setup.ts’, - In the
setup.ts
file, import the generated library. - 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:
- Created 2 years ago
- Reactions:3
- Comments:6 (3 by maintainers)
Top 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 >
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 Free
Top 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
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-setupOnly 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.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. 🎉
@barbados-clemens here you have! https://github.com/abrahamsaanchez/global-setup