docs(core): Add example of using global scripts for jest and nx libraries (#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
This commit is contained in:
Caleb Ukle 2022-04-11 08:51:29 -05:00 committed by GitHub
parent 3e94d4cb3a
commit b3d59bdcc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 2 deletions

View File

@ -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.

View File

@ -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: '<rootDir>/setup.ts',
globalTeardown: '<rootDir>/teardown.ts'
};`
);