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

feat(testing): add ability to use nx libraries in jest global setup/teardown #9145

Closed
wants to merge 1 commit into from

Conversation

barbados-clemens
Copy link
Contributor

@barbados-clemens barbados-clemens commented Feb 28, 2022

auto register tsconfig paths in jest resolver to allow for global setup/teardown files to reference other libs as needed

This removed 1 less configuration that developers need to do when using jest with nx.

Current Behavior

unable to use nx libraries within a global setup/teardown file for jest

Expected Behavior

I am able to use nx libraries within a global setup/teardown file provided to jest

Related Issue(s)

Fixes #8709

@nx-cloud
Copy link

nx-cloud bot commented Feb 28, 2022

☁️ Nx Cloud Report

CI ran the following commands for commit 9e3596d. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this branch


🟥 Failed Commands
nx affected --target=e2e --base=cef4bbd4cf4c429b0456b2e130a3b1812fde926a --head=9e3596dfadc24f4eaf89b66eaf59d86cc5372452 --exclude=e2e-detox,e2e-js,e2e-next,e2e-workspace-create,e2e-workspace-integrations,e2e-workspace-core,e2e-react,e2e-web,e2e-angular-extensions,e2e-angular-core,e2e-cli,e2e-nx-plugin,e2e-cypress,e2e-node,e2e-linter,e2e-jest,e2e-a...
✅ Successfully ran 6 targets

Sent with 💌 from NxCloud.

@vercel
Copy link

vercel bot commented Feb 28, 2022

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/nrwl/nx-dev/3u6VBbYbvMJrtDHaAKzM8Bmb8EJW
✅ Preview: Canceled

[Deployment for 9e3596d canceled]

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 barbados-clemens added the scope: testing tools Issues related to Cypress / Jest / Playwright / Vitest support in Nx label Feb 28, 2022
@barbados-clemens barbados-clemens self-assigned this Feb 28, 2022
@barbados-clemens barbados-clemens changed the title feat(testing): add tsconfig-paths in jest resolver feat(testing): add ability to use nx libraries in jest global setup/teardown Feb 28, 2022
// allow tspaths to work within jest global files like setup/teardown
process.env.TS_NODE_PROJECT =
process.env.TS_NODE_PROJECT || 'tsconfig.base.json';
require('tsconfig-paths/register');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

running into some strange behaviors when this util with the cleanup logic.
since we have this util now. I think this might make more sense to do in userland before running the global scripts and cleaning up after the setup/teardown logic is run.
i.e.

//userland-setup.ts

import { registerTsProject } from 'nx/src/utils/register';
const cleanup = registerTsProject('.', 'tsconfig.base.json');

import {teardown} from '@global-fun/globals';
export default async function() {teardown();}

cleanup();

vs nx trying to run this logic. mainly the issues I'm seeing when trying to run this via the resolver/preset/config is it will not resolve the path alias but still pass the tests and then subsequent runs will pass. I think this has to do with the ts path registration not being cleaned up properly. but it can't be cleaned up until the setup/teardown has taken place. which can only be specified in userland setup/teardown files.

We could do this in the jest executor, but I'm hesitant because that will break editors running tests via jest command and those manually running the jest command.

This looks like I should just update Jest docs to show how to register tspaths for the global setup/teardown vs trying doing the path registration.

@barbados-clemens
Copy link
Contributor Author

barbados-clemens commented Mar 23, 2022

Closing in favor of explaining how to set up via jest docs in PR #9483

@FrozenPandaz FrozenPandaz deleted the feat/jest-tsconfig-path-register branch June 7, 2022 23:31
@github-actions
Copy link

This pull request has already been merged/closed. If you experience issues related to these changes, please open a new issue referencing this pull request.

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

Successfully merging this pull request may close these issues.

[JEST] globalSetup and globalTeardown
2 participants