Repository navigation
Expand file tree
/
Copy pathjest.patterns.js
More file actions
40 lines (38 loc) · 3.01 KB
/
Copy pathjest.patterns.js
File metadata and controls
40 lines (38 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* The `transformIgnorePatterns` every Jest project of the workspace uses, in one place (see `jest.preset.js` for why
* each package is listed). Two variants, because the two kinds of project load ES modules differently:
*
* - `transformIgnorePatterns` (the preset's default) also transforms every `.mjs` file. The Angular projects need
* that: Angular and its ecosystem ship `fesm2022/*.mjs` bundles that jest-preset-angular compiles (and links).
*
* - `nodeTransformIgnorePatterns` leaves every ES module alone. The Node projects run with
* `NODE_OPTIONS=--experimental-vm-modules` (the workspace `.env.test`; CI sets it for them), so Jest loads an ES
* module natively, also one a CommonJS spec requires (`require(esm)`, Node >= 24.9). NestJS 12 ships as ES modules,
* which is why the flag is on. Under it Jest decides how to load a file by the file alone — a `.mjs`, or a `.js` in
* a `"type": "module"` package, is evaluated as an ES module — so a transform to CommonJS breaks such a package
* instead of fixing it: Nest found no named exports in the `.mjs` of `uid` ("The requested module 'uid' does not
* provide an export named 'uid'"), and uuid 14 and jose 6, both ESM-only, threw "exports is not defined" in the
* auth, pricing, catalog and inventory suites. Only a package that puts ES module syntax in files Node takes for
* CommonJS still needs the transform: `@datorama/akita` (plain `.js`, no `"type"`), and `@nestjs/axios` (see
* `jest.preset.js`). Every other package on the list above loads natively.
*
* The Angular projects run WITHOUT that flag (each one's `.env.test` clears `NODE_OPTIONS`; CI runs them separately):
* under it, Jest would evaluate every `.mjs` bundle as an ES module after jest-preset-angular compiled it to CommonJS
* ("module is not defined" in `setup-env/zone`, and every Angular suite failed to load).
*
* No project sets the key: `jest.preset.js` picks the variant, and a project's own `transformIgnorePatterns` would
* REPLACE the preset's.
*
* Nx loads a `.env.test` value only when the variable is not set yet, so a `NODE_OPTIONS` that is already set wins
* over both files: `yarn ng test <node project>` (the `ng` script sets a heap size) runs WITHOUT the flag and the
* suites fail to load NestJS. Run them with `yarn nx test <project>`, or add the flag to your own NODE_OPTIONS.
*/
const ESM_PACKAGES = String.raw`(?:.*/)?(sanitize-html|htmlparser2|domelementtype|domhandler|domutils|dom-serializer|entities|nanoid|parse-srcset|uuid|camelcase|@faker-js|@nestjs/axios|@datorama|@ngneat|lodash-es|d3-[a-z-]+|internmap|jose|marked|hash-it)/`;
/** The packages a Node project still transforms: ES module syntax in files Node loads as CommonJS (see above). */
const NODE_TRANSFORMED_PACKAGES = String.raw`(?:.*/)?(@datorama|@nestjs/axios)/`;
module.exports = {
ESM_PACKAGES,
NODE_TRANSFORMED_PACKAGES,
transformIgnorePatterns: [String.raw`node_modules/(?!.*\.mjs$|${ESM_PACKAGES})`],
nodeTransformIgnorePatterns: [String.raw`node_modules/(?!${NODE_TRANSFORMED_PACKAGES})`]
};