From fb3f4c9a632f5a8938ce55484fdd9081d42429b6 Mon Sep 17 00:00:00 2001 From: Caleb Ukle Date: Wed, 23 Mar 2022 14:03:34 -0500 Subject: [PATCH] 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 --- docs/shared/jest-plugin.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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.