diff --git a/docs/shared/jest-plugin.md b/docs/shared/jest-plugin.md index 1af0644bd526d..1e4a8c9717ec0 100644 --- a/docs/shared/jest-plugin.md +++ b/docs/shared/jest-plugin.md @@ -118,6 +118,23 @@ By default, coverage reports will be generated in the `coverage/` directory unde > `coverageDirectory` and `coverageReporters` are configurable via the project configuration file as well. +### Global setup/teardown with nx libraries + +In order to use Jest's global setup/teardown functions that reference nx libraries, you'll need to register the TS path for jest to resolve the libraries. +Nx provides a helper function that you can import within your setup/teardown file. + +```ts +import { registerTsProject } from 'nx/src/utils/register'; +const cleanupRegisteredPaths = registerTsProject('.', 'tsconfig.base.json'); + +import { yourFancyFunction } from '@some-org/my-util-library'; +export default async function () { + yourFancyFunction(); +} +// make sure to run the clean up! +cleanupRegisteredPaths(); +``` + ## Debugging Failing Tests If your code editor doesn't provide a way to debug your tests, you can leverage the Chrome DevTools to debug your tests with the `--inspect-brk` flag for node. diff --git a/e2e/jest/src/jest.test.ts b/e2e/jest/src/jest.test.ts index ed3674cd0f912..34ac3520830ba 100644 --- a/e2e/jest/src/jest.test.ts +++ b/e2e/jest/src/jest.test.ts @@ -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'); @@ -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 { }`); @@ -36,6 +49,31 @@ describe('Jest', () => { ` ); + updateFile( + `libs/${mylib}/setup.ts`, + stripIndents` + const { registerTsProject } = require('nx/src/utils/register'); + const cleanup = registerTsProject('.', 'tsconfig.base.json'); + + import {setup} from '@global-fun/globals'; + export default async function() {setup();} + + cleanup(); + ` + ); + + updateFile( + `libs/${mylib}/teardown.ts`, + stripIndents` + 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(); + ` + ); + updateFile( `libs/${mylib}/jest.config.js`, stripIndents` @@ -48,7 +86,9 @@ describe('Jest', () => { moduleFileExtensions: ['ts', 'js', 'html'], coverageReporters: ['html'], passWithNoTests: true, - globals: { testGlobal: ${testGlobal} } + globals: { testGlobal: ${testGlobal} }, + globalSetup: '/setup.ts', + globalTeardown: '/teardown.ts' };` );