Skip to content

Commit e8c9689

Browse files
authored
feat: add ngcc-jest-processor util script (#853)
1 parent 986b127 commit e8c9689

File tree

9 files changed

+68
-3
lines changed

9 files changed

+68
-3
lines changed

e2e/test-app-v10/jest.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require('jest-preset-angular/ngcc-jest-processor');
2+
13
/** @type {import('@jest/types').Config.InitialOptions} */
24
module.exports = {
35
moduleNameMapper: {

e2e/test-app-v10/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"build": "ng build",
88
"test-cjs-uniso": "jest --clearCache && jest -c=jest-cjs-uniso.config.js",
99
"test-cjs-iso": "jest --clearCache && jest -c=jest-cjs-iso.config.js",
10-
"postinstall": "ngcc --properties es2015 browser module main",
1110
"lint": "ng lint"
1211
},
1312
"private": true,

e2e/test-app-v11/jest.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require('jest-preset-angular/ngcc-jest-processor');
2+
13
/** @type {import('@jest/types').Config.InitialOptions} */
24
module.exports = {
35
moduleNameMapper: {

e2e/test-app-v11/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"build": "ng build",
88
"test-cjs-uniso": "jest --clearCache && jest -c=jest-cjs-uniso.config.js",
99
"test-cjs-iso": "jest --clearCache && jest -c=jest-cjs-iso.config.js",
10-
"postinstall": "ngcc --properties es2015 browser module main",
1110
"lint": "ng lint"
1211
},
1312
"private": true,

e2e/test-app-v9/jest.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require('jest-preset-angular/ngcc-jest-processor');
2+
13
/** @type {import('@jest/types').Config.InitialOptions} */
24
module.exports = {
35
moduleNameMapper: {

e2e/test-app-v9/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"build": "ng build",
88
"test-cjs-uniso": "jest --clearCache && jest -c=jest-cjs-uniso.config.js",
99
"test-cjs-iso": "jest --clearCache && jest -c=jest-cjs-iso.config.js",
10-
"postinstall": "ngcc --properties es2015 browser module main",
1110
"lint": "ng lint"
1211
},
1312
"private": true,

ngcc-jest-processor.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./build/utils/ngcc-jest-processor');

scripts/e2e.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const executeTest = (projectRealPath) => {
4444

4545
copySync(join(cwd, 'jest-preset.js'), `${presetDir}/jest-preset.js`);
4646
copySync(join(cwd, 'index.js'), `${presetDir}/index.js`);
47+
copySync(join(cwd, 'ngcc-jest-processor.js'), `${presetDir}/ngcc-jest-processor.js`);
4748
copySync(join(cwd, 'package.json'), `${presetDir}/package.json`);
4849
copySync(join(cwd, 'build'), `${presetDir}/build`);
4950

src/utils/ngcc-jest-processor.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* Mainly copied from https://github.com/angular/angular-cli/blob/master/packages/ngtools/webpack/src/ngcc_processor.ts
3+
* and adjusted to work with Jest
4+
*/
5+
import { spawnSync } from 'child_process';
6+
import { existsSync } from 'fs';
7+
import { dirname, join } from 'path';
8+
9+
const IGNORE_ARGS = ['--clearCache', '--help', '--init', '--listTests', '--showConfig'];
10+
const canRunNgcc = !process.argv.find((arg) => IGNORE_ARGS.includes(arg));
11+
function findNodeModulesDirectory(startPoint: string): string {
12+
let current = startPoint;
13+
while (dirname(current) !== current) {
14+
const nodePath = join(current, 'node_modules');
15+
if (existsSync(nodePath)) {
16+
return nodePath;
17+
}
18+
19+
current = dirname(current);
20+
}
21+
22+
throw new Error(
23+
`Cannot locate the 'node_modules' directory. Please make sure you are running jest from root level of your project`,
24+
);
25+
}
26+
27+
if (canRunNgcc) {
28+
process.stdout.write('ngcc-jest-processor: running ngcc\n');
29+
// We spawn instead of using the API because:
30+
// - NGCC Async uses clustering which is problematic when used via the API which means
31+
// that we cannot setup multiple cluster masters with different options.
32+
// - We will not be able to have concurrent builds otherwise Ex: App-Shell,
33+
// as NGCC will create a lock file for both builds and it will cause builds to fails.
34+
const { status, error } = spawnSync(
35+
process.execPath,
36+
[
37+
require.resolve('@angular/compiler-cli/ngcc/main-ngcc.js'),
38+
'--source' /** basePath */,
39+
findNodeModulesDirectory(process.cwd()),
40+
'--properties' /** propertiesToConsider */,
41+
/**
42+
* There are various properties: fesm2015, fesm5, es2015, esm2015, esm5, main, module, browser to choose from.
43+
* Currently Jest requires `commonjs` so we only need to ask `ngcc` to produce `umd` outputs. Later when switching
44+
* to ESM, we can change to different properties to produce ESM outputs.
45+
*/
46+
...['es2015', 'main'],
47+
'--first-only' /** compileAllFormats */,
48+
'false', // make sure that `ngcc` runs on subfolders as well
49+
'--async',
50+
],
51+
{
52+
stdio: ['inherit', process.stderr, process.stderr],
53+
},
54+
);
55+
if (status !== 0) {
56+
const errorMessage: string = error?.message ?? '';
57+
58+
throw new Error(`${errorMessage} NGCC failed ${errorMessage ? ', see above' : ''}.`);
59+
}
60+
}

0 commit comments

Comments
 (0)