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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions e2e/jest/src/jest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import {
} from '@nrwl/e2e/utils';

describe('Jest', () => {
beforeEach(() => newProject());
beforeAll(() => {
newProject({ name: uniq('proj') });
});

it('should be able test projects using jest', async () => {
const mylib = uniq('mylib');
Expand All @@ -23,7 +25,18 @@ describe('Jest', () => {
it('should merge with jest config globals', async () => {
const testGlobal = `'My Test Global'`;
const mylib = uniq('mylib');
const utilLib = uniq('util-lib');
runCLI(`generate @nrwl/workspace:lib ${mylib} --unit-test-runner jest`);
runCLI(
`generate @nrwl/workspace:lib ${utilLib} --importPath=@global-fun/globals`
);
updateFile(
`libs/${utilLib}/src/index.ts`,
stripIndents`
export function setup() {console.log('i am a global setup function')}
export function teardown() {console.log('i am a global teardown function')}
`
);

updateFile(`libs/${mylib}/src/lib/${mylib}.ts`, `export class Test { }`);

Expand All @@ -36,6 +49,22 @@ describe('Jest', () => {
`
);

updateFile(
`libs/${mylib}/setup.ts`,
stripIndents`
import {setup} from '@global-fun/globals';
export default async function() {setup();}
`
);

updateFile(
`libs/${mylib}/teardown.ts`,
stripIndents`
import {teardown} from '@global-fun/globals';
export default async function() {teardown();}
`
);

updateFile(
`libs/${mylib}/jest.config.js`,
stripIndents`
Expand All @@ -48,7 +77,9 @@ describe('Jest', () => {
moduleFileExtensions: ['ts', 'js', 'html'],
coverageReporters: ['html'],
passWithNoTests: true,
globals: { testGlobal: ${testGlobal} }
globals: { testGlobal: ${testGlobal} },
globalSetup: '<rootDir>/setup.ts',
globalTeardown: '<rootDir>/teardown.ts'
};`
);

Expand Down
3 changes: 2 additions & 1 deletion packages/jest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"@jest/test-result": "27.2.2",
"chalk": "4.1.0",
"jest-config": "27.2.2",
"jest-util": "27.2.0"
"jest-util": "27.2.0",
"tsconfig-paths": "^3.9.0"
}
}
5 changes: 5 additions & 0 deletions packages/jest/plugins/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// 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.


import { dirname, extname } from 'path';
import { resolve as resolveExports } from 'resolve.exports';
import type defaultResolver from 'jest-resolve/build/defaultResolver';
Expand Down