diff --git a/examples/example-app-yarn-workspace/packages/angular-app/package.json b/examples/example-app-yarn-workspace/packages/angular-app/package.json index bd687b610f..e685ae322e 100644 --- a/examples/example-app-yarn-workspace/packages/angular-app/package.json +++ b/examples/example-app-yarn-workspace/packages/angular-app/package.json @@ -23,7 +23,7 @@ "angular-in-memory-web-api": "^0.12.0", "rxjs": "~6.6.0", "tslib": "^2.3.1", - "zone.js": "latest" + "zone.js": "~0.11.5" }, "devDependencies": { "@angular-devkit/build-angular": "~12.2.17", diff --git a/examples/example-app-yarn-workspace/yarn.lock b/examples/example-app-yarn-workspace/yarn.lock index f98ede4d0e..635aaab030 100644 --- a/examples/example-app-yarn-workspace/yarn.lock +++ b/examples/example-app-yarn-workspace/yarn.lock @@ -9155,7 +9155,7 @@ yocto-queue@^0.1.0: resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b" integrity sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q== -zone.js@latest: +zone.js@~0.11.5: version "0.11.5" resolved "https://registry.yarnpkg.com/zone.js/-/zone.js-0.11.5.tgz#ab0b449e91fadb5ebb2db189ffe1b7b6048dc8b1" integrity sha512-D1/7VxEuQ7xk6z/kAROe4SUbd9CzxY4zOwVGnGHerd/SgLIVU5f4esDzQUsOCeArn933BZfWMKydH7l7dPEp0g== diff --git a/src/config/global-setup.spec.ts b/src/config/global-setup.spec.ts new file mode 100644 index 0000000000..e0f3b78dac --- /dev/null +++ b/src/config/global-setup.spec.ts @@ -0,0 +1,44 @@ +import globalSetup from './global-setup'; + +jest.mock('../utils/ngcc-jest-processor', () => { + return { + runNgccJestProcessor() { + console.log('Mock ngcc jest processor'); + }, + }; +}); + +describe('global-setup', () => { + test('should skip ngcc-jest-processor with `skipNgcc: true` option in `ngJest` config', async () => { + console.log = jest.fn(); + + await globalSetup(Object.create(null), { + globals: { + ngJest: { + skipNgcc: true, + }, + }, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + } as any); + + expect(console.log).not.toHaveBeenCalled(); + }); + + test.each([false, undefined, null])( + 'should not skip ngcc-jest-processor with `skipNgcc: %s` option in `ngJest` config', + async (skipNgcc) => { + console.log = jest.fn(); + + await globalSetup(Object.create(null), { + globals: { + ngJest: { + skipNgcc, + }, + }, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + } as any); + + expect(console.log).toHaveBeenCalled(); + }, + ); +}); diff --git a/src/config/global-setup.ts b/src/config/global-setup.ts index cd1b62b6f0..fbceb008f8 100644 --- a/src/config/global-setup.ts +++ b/src/config/global-setup.ts @@ -1,3 +1,17 @@ -export = async () => { - await import('../utils/ngcc-jest-processor'); +import type { Config } from '@jest/types'; + +import { runNgccJestProcessor } from '../utils/ngcc-jest-processor'; + +interface NgJestProjectConfig extends Omit { + globals: { + ngJest?: { + skipNgcc: boolean; + }; + }; +} + +export = async (_globalConfig: Config.GlobalConfig, projectConfig: NgJestProjectConfig) => { + if (!projectConfig.globals.ngJest?.skipNgcc) { + runNgccJestProcessor(); + } }; diff --git a/src/utils/ngcc-jest-processor.ts b/src/utils/ngcc-jest-processor.ts index 91fef3da94..f0c18363ca 100644 --- a/src/utils/ngcc-jest-processor.ts +++ b/src/utils/ngcc-jest-processor.ts @@ -5,7 +5,6 @@ import { spawnSync } from 'child_process'; import path from 'path'; -const IGNORE_ARGS = ['--clearCache', '--help', '--init', '--listTests', '--showConfig']; const ANGULAR_COMPILER_CLI_PKG_NAME = `@angular${path.sep}compiler-cli`; let ngccPath = ''; @@ -21,7 +20,7 @@ function findNodeModulesDirectory(): string { const nodeModuleDirPath = findNodeModulesDirectory(); -if (!process.argv.find((arg) => IGNORE_ARGS.includes(arg))) { +export const runNgccJestProcessor = (): void => { if (nodeModuleDirPath) { process.stdout.write('\nngcc-jest-processor: running ngcc\n'); // We spawn instead of using the API because: @@ -61,4 +60,4 @@ if (!process.argv.find((arg) => IGNORE_ARGS.includes(arg))) { `'ngcc' must be run before running Jest`, ); } -} +}; diff --git a/website/docs/getting-started/options.md b/website/docs/getting-started/options.md index 0754497911..6a31740600 100644 --- a/website/docs/getting-started/options.md +++ b/website/docs/getting-started/options.md @@ -10,8 +10,10 @@ More information about `ts-jest` options, see https://kulshekhar.github.io/ts-je :::important -Since **9.0.0**, `jest-preset-angular` default Jest configuration no longer provides `moduleNameMapper`. If you wish to reuse -the old `moduleNameMapper` configuration, you can put this into your Jest config +Since **v9.0.0**, `jest-preset-angular` default Jest configuration no longer provides `moduleNameMapper`. If you wish to reuse +the old `moduleNameMapper` configuration, you can put this into your Jest config. + +Since **v12.0.0**, `jest-preset-angular` has some own config options under `ngJest` option in Jest `globals` config. ``` moduleNameMapper: { diff --git a/website/docs/guides/angular-13+.md b/website/docs/guides/angular-13+.md index 84438629d1..9fa2d9feb0 100644 --- a/website/docs/guides/angular-13+.md +++ b/website/docs/guides/angular-13+.md @@ -7,7 +7,7 @@ title: Angular >=13 currently supports testing with Jest in `CommonJS` mode with **Angular 13** using [default preset](../getting-started/presets.md). Jest ESM support with **Angular 13** is new and may have issues. -Starting from **11.0.0**, `jest-preset-angular` introduces a few extra changes to be able to run Jest with **Angular 13**: +Starting from **v11.0.0**, `jest-preset-angular` introduces a few extra changes to be able to run Jest with **Angular 13**: - `ng-jest-resolver` is introduced as a custom Jest resolver to resolve `.mjs` files. diff --git a/website/docs/guides/angular-ivy.md b/website/docs/guides/angular-ivy.md index a951ac938b..f18b517d6c 100644 --- a/website/docs/guides/angular-ivy.md +++ b/website/docs/guides/angular-ivy.md @@ -28,3 +28,37 @@ module.exports = { } } ``` + +Since **v12.0.0**, `jest-preset-angular` has some own config options under `ngJest` option in Jest `globals` config. One of those allows to skip `ngcc` processing. + +To skip `ngcc` which runs by `jest-preset-angular/global-setup`, one can do the following + +- in the `jest.config.js` where one is using `jest-preset-angular/global-setup` + +```js +// jest.config.js +module.exports = { + // [...] + globalSetup: 'jest-preset-angular/global-setup', + globals: { + ngJest: { + skipNgcc: true, + }, + }, +}; +``` + +- or in the `package.json` where one is using `jest-preset-angular/global-setup` + +```json +{ + "jest": { + "globalSetup": "jest-preset-angular/global-setup", + "globals": { + "ngJest": { + "skipNgcc": true + } + } + } +} +``` diff --git a/website/versioned_docs/version-10.x/getting-started/options.md b/website/versioned_docs/version-10.x/getting-started/options.md index 6b4f76a48c..556c0570ad 100644 --- a/website/versioned_docs/version-10.x/getting-started/options.md +++ b/website/versioned_docs/version-10.x/getting-started/options.md @@ -10,7 +10,7 @@ More information about `ts-jest` options, see https://kulshekhar.github.io/ts-je :::important -Since **9.0.0**, `jest-preset-angular` default Jest configuration no longer provides `moduleNameMapper`. If you wish to reuse +Since **v9.0.0**, `jest-preset-angular` default Jest configuration no longer provides `moduleNameMapper`. If you wish to reuse the old `moduleNameMapper` configuration, you can put this into your Jest config ``` diff --git a/website/versioned_docs/version-11.0/getting-started/options.md b/website/versioned_docs/version-11.0/getting-started/options.md index 0754497911..eb0a530106 100644 --- a/website/versioned_docs/version-11.0/getting-started/options.md +++ b/website/versioned_docs/version-11.0/getting-started/options.md @@ -10,7 +10,7 @@ More information about `ts-jest` options, see https://kulshekhar.github.io/ts-je :::important -Since **9.0.0**, `jest-preset-angular` default Jest configuration no longer provides `moduleNameMapper`. If you wish to reuse +Since **v9.0.0**, `jest-preset-angular` default Jest configuration no longer provides `moduleNameMapper`. If you wish to reuse the old `moduleNameMapper` configuration, you can put this into your Jest config ``` diff --git a/website/versioned_docs/version-11.0/guides/angular-13+.md b/website/versioned_docs/version-11.0/guides/angular-13+.md index 436c5c1258..e3a4cf434f 100644 --- a/website/versioned_docs/version-11.0/guides/angular-13+.md +++ b/website/versioned_docs/version-11.0/guides/angular-13+.md @@ -7,7 +7,7 @@ title: Angular >=13 currently supports testing with Jest in `CommonJS` mode with **Angular 13** using [default preset](../getting-started/presets.md). Jest ESM support with **Angular 13** is new and may have issues. -Starting from **11.0.0**, `jest-preset-angular` introduces a few extra changes to be able to run Jest with **Angular 13**: +Starting from **v11.0.0**, `jest-preset-angular` introduces a few extra changes to be able to run Jest with **Angular 13**: - `ng-jest-resolver` is introduced as a custom Jest resolver to resolve `.mjs` files. diff --git a/website/versioned_docs/version-11.1/getting-started/options.md b/website/versioned_docs/version-11.1/getting-started/options.md index 0754497911..eb0a530106 100644 --- a/website/versioned_docs/version-11.1/getting-started/options.md +++ b/website/versioned_docs/version-11.1/getting-started/options.md @@ -10,7 +10,7 @@ More information about `ts-jest` options, see https://kulshekhar.github.io/ts-je :::important -Since **9.0.0**, `jest-preset-angular` default Jest configuration no longer provides `moduleNameMapper`. If you wish to reuse +Since **v9.0.0**, `jest-preset-angular` default Jest configuration no longer provides `moduleNameMapper`. If you wish to reuse the old `moduleNameMapper` configuration, you can put this into your Jest config ``` diff --git a/website/versioned_docs/version-11.1/guides/angular-13+.md b/website/versioned_docs/version-11.1/guides/angular-13+.md index 84438629d1..9fa2d9feb0 100644 --- a/website/versioned_docs/version-11.1/guides/angular-13+.md +++ b/website/versioned_docs/version-11.1/guides/angular-13+.md @@ -7,7 +7,7 @@ title: Angular >=13 currently supports testing with Jest in `CommonJS` mode with **Angular 13** using [default preset](../getting-started/presets.md). Jest ESM support with **Angular 13** is new and may have issues. -Starting from **11.0.0**, `jest-preset-angular` introduces a few extra changes to be able to run Jest with **Angular 13**: +Starting from **v11.0.0**, `jest-preset-angular` introduces a few extra changes to be able to run Jest with **Angular 13**: - `ng-jest-resolver` is introduced as a custom Jest resolver to resolve `.mjs` files. diff --git a/website/versioned_docs/version-9.x/getting-started/options.md b/website/versioned_docs/version-9.x/getting-started/options.md index 6b4f76a48c..556c0570ad 100644 --- a/website/versioned_docs/version-9.x/getting-started/options.md +++ b/website/versioned_docs/version-9.x/getting-started/options.md @@ -10,7 +10,7 @@ More information about `ts-jest` options, see https://kulshekhar.github.io/ts-je :::important -Since **9.0.0**, `jest-preset-angular` default Jest configuration no longer provides `moduleNameMapper`. If you wish to reuse +Since **v9.0.0**, `jest-preset-angular` default Jest configuration no longer provides `moduleNameMapper`. If you wish to reuse the old `moduleNameMapper` configuration, you can put this into your Jest config ```