Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JEST] globalSetup and globalTeardown #8709

Closed
abrahamsaanchez opened this issue Jan 26, 2022 · 7 comments · Fixed by #9483
Closed

[JEST] globalSetup and globalTeardown #8709

abrahamsaanchez opened this issue Jan 26, 2022 · 7 comments · Fixed by #9483
Assignees
Labels
outdated scope: testing tools Issues related to Cypress / Jest / Playwright / Vitest support in Nx type: bug

Comments

@abrahamsaanchez
Copy link

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: '/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
@FrozenPandaz FrozenPandaz added the scope: testing tools Issues related to Cypress / Jest / Playwright / Vitest support in Nx label Jan 29, 2022
@lidanh
Copy link

lidanh commented Feb 8, 2022

Same here, any update?

@barbados-clemens barbados-clemens self-assigned this Feb 18, 2022
@barbados-clemens
Copy link
Contributor

@abrahamsaanchez or @lidanh can you provide a repo where this is happening? I can't seem to replicate on my side?

@abrahamsaanchez
Copy link
Author

@barbados-clemens here you have! https://github.com/abrahamsaanchez/global-setup

@barbados-clemens
Copy link
Contributor

barbados-clemens commented 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.
jestjs/jest#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

WkMac-c8xtI3hO.0225.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 added a commit that referenced this issue Feb 25, 2022
explain using tsconfig-paths/register and TS_NODE_PROJECT to allow usage of tsconfig path aliases
within the jest global setup/teardown files

ISSUES CLOSED: #8709
barbados-clemens added a commit that referenced this issue Feb 28, 2022
auto register tsconfig paths in jest preset to allow
for global setup/teardown files to reference other libs as needed

ISSUES CLOSED: #8709
barbados-clemens added a commit that referenced this issue Feb 28, 2022
auto register tsconfig paths in jest preset to allow
for global setup/teardown files to reference other libs as needed

ISSUES CLOSED: #8709
barbados-clemens added a commit that referenced this issue Feb 28, 2022
auto register tsconfig paths in jest preset to allow
for global setup/teardown files to reference other libs as needed

ISSUES CLOSED: #8709
barbados-clemens added a commit that referenced this issue Mar 23, 2022
…global setup/teardown

Jest global setup/teardown scripts run before path aliaes are mapped, so the path resigstration must
happen in the userland global scripts that are set to run
initially the thought of doing this within
the jest executor was thought, but this will provide an inconsistent way to running tests if the
tests are run via an editor plugin or calling `jest` directly; therefore, it's deferred to register
the paths within userland files that are needing the paths to be registered in order to allow for
proper handling (cleanup) and not to overcall unneeded features when global scripts are not in use
for those not using the feature.

ISSUES CLOSED: #8709
barbados-clemens added a commit that referenced this issue Apr 11, 2022
…global setup/teardown

Jest global setup/teardown scripts run before path aliaes are mapped, so the path resigstration must
happen in the userland global scripts that are set to run
initially the thought of doing this within
the jest executor was thought, but this will provide an inconsistent way to running tests if the
tests are run via an editor plugin or calling `jest` directly; therefore, it's deferred to register
the paths within userland files that are needing the paths to be registered in order to allow for
proper handling (cleanup) and not to overcall unneeded features when global scripts are not in use
for those not using the feature.

ISSUES CLOSED: #8709
barbados-clemens added a commit that referenced this issue Apr 11, 2022
…ries (#9483)

* docs(testing): add example on how to use ts path aliases within jest global setup/teardown

Jest global setup/teardown scripts run before path aliaes are mapped, so the path resigstration must
happen in the userland global scripts that are set to run
initially the thought of doing this within
the jest executor was thought, but this will provide an inconsistent way to running tests if the
tests are run via an editor plugin or calling `jest` directly; therefore, it's deferred to register
the paths within userland files that are needing the paths to be registered in order to allow for
proper handling (cleanup) and not to overcall unneeded features when global scripts are not in use
for those not using the feature.

ISSUES CLOSED: #8709

* cleanup(testing): test the usage of jest global scripts to ensure no regressions

streamline e2e jest project testing and update global tests to test global functions are working as
intended
@MikoVelascoRamirez
Copy link

@barbados-clemens here you have! https://github.com/abrahamsaanchez/global-setup

The link is down.

@barbados-clemens
Copy link
Contributor

@github-actions
Copy link

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated scope: testing tools Issues related to Cypress / Jest / Playwright / Vitest support in Nx type: bug
Projects
None yet
5 participants