diff --git a/src/config/global-setup.ts b/src/config/global-setup.ts index fbceb008f8..18937c2486 100644 --- a/src/config/global-setup.ts +++ b/src/config/global-setup.ts @@ -1,4 +1,5 @@ import type { Config } from '@jest/types'; +import type { TsJestGlobalOptions } from 'ts-jest/dist/types'; import { runNgccJestProcessor } from '../utils/ngcc-jest-processor'; @@ -7,11 +8,13 @@ interface NgJestProjectConfig extends Omit { ngJest?: { skipNgcc: boolean; }; + tsJest?: TsJestGlobalOptions; }; } -export = async (_globalConfig: Config.GlobalConfig, projectConfig: NgJestProjectConfig) => { - if (!projectConfig.globals.ngJest?.skipNgcc) { - runNgccJestProcessor(); +export = async (_globalConfig: Config.GlobalConfig, { globals }: NgJestProjectConfig) => { + if (!globals.ngJest?.skipNgcc) { + const tsconfig = globals.tsJest?.tsconfig; + runNgccJestProcessor(typeof tsconfig === 'string' ? tsconfig : undefined); } }; diff --git a/src/utils/ngcc-jest-processor.ts b/src/utils/ngcc-jest-processor.ts index f0c18363ca..f0645e009f 100644 --- a/src/utils/ngcc-jest-processor.ts +++ b/src/utils/ngcc-jest-processor.ts @@ -20,34 +20,35 @@ function findNodeModulesDirectory(): string { const nodeModuleDirPath = findNodeModulesDirectory(); -export const runNgccJestProcessor = (): void => { +export const runNgccJestProcessor = (tsconfigPath: string | undefined): void => { if (nodeModuleDirPath) { process.stdout.write('\nngcc-jest-processor: running ngcc\n'); + + const ngccBaseArgs = [ + ngccPath, + '--source' /** basePath */, + nodeModuleDirPath, + '--properties' /** propertiesToConsider */, + /** + * There are various properties: fesm2015, fesm5, es2015, esm2015, esm5, main, module, browser to choose from. + * Normally, Jest requires `umd`. If running Jest in ESM mode, Jest will require both `umd` + `esm2015`. + */ + ...['es2015', 'main'], + '--first-only' /** compileAllFormats */, + 'false', // make sure that `ngcc` runs on subfolders as well + '--async', + ]; + if (tsconfigPath) { + ngccBaseArgs.push(...['--tsconfig', tsconfigPath]); + } // We spawn instead of using the API because: // - NGCC Async uses clustering which is problematic when used via the API which means // that we cannot setup multiple cluster masters with different options. // - We will not be able to have concurrent builds otherwise Ex: App-Shell, // as NGCC will create a lock file for both builds and it will cause builds to fails. - const { status, error } = spawnSync( - process.execPath, - [ - ngccPath, - '--source' /** basePath */, - nodeModuleDirPath, - '--properties' /** propertiesToConsider */, - /** - * There are various properties: fesm2015, fesm5, es2015, esm2015, esm5, main, module, browser to choose from. - * Normally, Jest requires `umd`. If running Jest in ESM mode, Jest will require both `umd` + `esm2015`. - */ - ...['es2015', 'main'], - '--first-only' /** compileAllFormats */, - 'false', // make sure that `ngcc` runs on subfolders as well - '--async', - ], - { - stdio: ['inherit', process.stderr, process.stderr], - }, - ); + const { status, error } = spawnSync(process.execPath, ngccBaseArgs, { + stdio: ['inherit', process.stderr, process.stderr], + }); if (status !== 0) { const errorMessage: string = error?.message ?? '';