Skip to content

Commit

Permalink
docs(testing): add example on how to use ts path aliases within jest …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
barbados-clemens committed Mar 23, 2022
1 parent d5102f0 commit fb3f4c9
Showing 1 changed file with 17 additions and 0 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

0 comments on commit fb3f4c9

Please sign in to comment.