Skip to content

Commit

Permalink
docs(core): Add example of using global scripts for jest and nx libra…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
barbados-clemens authored Apr 11, 2022
1 parent 3e94d4c commit b3d59bd
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
17 changes: 17 additions & 0 deletions docs/shared/jest-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
44 changes: 42 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,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`
Expand All @@ -48,7 +86,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

0 comments on commit b3d59bd

Please sign in to comment.