Skip to content

Commit

Permalink
fix(utils): throw error when cannot find @angular/core for `ngcc-je…
Browse files Browse the repository at this point in the history
…st-preprocessor` util (#981)
  • Loading branch information
ahnpnl authored Aug 11, 2021
1 parent 32cef4c commit c35d3f2
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/utils/ngcc-jest-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import { existsSync } from 'fs';
import { dirname, join } from 'path';

const IGNORE_ARGS = ['--clearCache', '--help', '--init', '--listTests', '--showConfig'];
const canRunNgcc = !process.argv.find((arg) => IGNORE_ARGS.includes(arg));
const nodeModuleDirPath = findNodeModulesDirectory(process.cwd());
const canRunNgcc =
!process.argv.find((arg) => IGNORE_ARGS.includes(arg)) && existsSync(join(nodeModuleDirPath, '@angular', 'core'));
function findNodeModulesDirectory(startPoint: string): string {
let current = startPoint;
while (dirname(current) !== current) {
Expand Down Expand Up @@ -36,12 +38,11 @@ if (canRunNgcc) {
[
require.resolve('@angular/compiler-cli/ngcc/main-ngcc.js'),
'--source' /** basePath */,
findNodeModulesDirectory(process.cwd()),
nodeModuleDirPath,
'--properties' /** propertiesToConsider */,
/**
* There are various properties: fesm2015, fesm5, es2015, esm2015, esm5, main, module, browser to choose from.
* Currently Jest requires `commonjs` so we only need to ask `ngcc` to produce `umd` outputs. Later when switching
* to ESM, we can change to different properties to produce ESM outputs.
* Normally, Jest requires `umd`. If running Jest in ESM mode, Jest will require both `umd` + `esm2015`.
*/
...['es2015', 'main'],
'--first-only' /** compileAllFormats */,
Expand All @@ -57,4 +58,8 @@ if (canRunNgcc) {

throw new Error(`${errorMessage} NGCC failed ${errorMessage ? ', see above' : ''}.`);
}
} else {
throw new Error(
`Cannot locate the '@angular/core' directory. Please make sure you are running 'ngcc-jest-processor.js' from root level of your project`,
);
}

0 comments on commit c35d3f2

Please sign in to comment.