Skip to content

Commit

Permalink
feat: allow skip ngcc via skipNgcc option in Jest globals (#1417)
Browse files Browse the repository at this point in the history
Closes #1396
  • Loading branch information
ahnpnl authored Apr 25, 2022
1 parent 2f8b77a commit 7950b5c
Show file tree
Hide file tree
Showing 14 changed files with 109 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion examples/example-app-yarn-workspace/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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==
Expand Down
44 changes: 44 additions & 0 deletions src/config/global-setup.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
},
);
});
18 changes: 16 additions & 2 deletions src/config/global-setup.ts
Original file line number Diff line number Diff line change
@@ -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<Config.ProjectConfig, 'globals'> {
globals: {
ngJest?: {
skipNgcc: boolean;
};
};
}

export = async (_globalConfig: Config.GlobalConfig, projectConfig: NgJestProjectConfig) => {
if (!projectConfig.globals.ngJest?.skipNgcc) {
runNgccJestProcessor();
}
};
5 changes: 2 additions & 3 deletions src/utils/ngcc-jest-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';

Expand All @@ -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:
Expand Down Expand Up @@ -61,4 +60,4 @@ if (!process.argv.find((arg) => IGNORE_ARGS.includes(arg))) {
`'ngcc' must be run before running Jest`,
);
}
}
};
6 changes: 4 additions & 2 deletions website/docs/getting-started/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
2 changes: 1 addition & 1 deletion website/docs/guides/angular-13+.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
34 changes: 34 additions & 0 deletions website/docs/guides/angular-ivy.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -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

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

```
Expand Down
2 changes: 1 addition & 1 deletion website/versioned_docs/version-11.0/guides/angular-13+.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

```
Expand Down
2 changes: 1 addition & 1 deletion website/versioned_docs/version-11.1/guides/angular-13+.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

```
Expand Down

0 comments on commit 7950b5c

Please sign in to comment.