diff --git a/global-setup.js b/global-setup.js deleted file mode 100644 index aa32b5bc70..0000000000 --- a/global-setup.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = require('./build/config/global-setup').globalSetup; diff --git a/global-setup.mjs b/global-setup.mjs deleted file mode 100644 index 6564ceaa39..0000000000 --- a/global-setup.mjs +++ /dev/null @@ -1,3 +0,0 @@ -import { globalSetup } from './build/config/global-setup.js'; - -export default globalSetup; diff --git a/src/config/global-setup.spec.ts b/src/config/global-setup.spec.ts deleted file mode 100644 index 92f66ecea6..0000000000 --- a/src/config/global-setup.spec.ts +++ /dev/null @@ -1,36 +0,0 @@ -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(); - globalThis.ngJest = { - skipNgcc: true, - }; - - await globalSetup(); - - expect(console.log).not.toHaveBeenCalled(); - }); - - test.each([false, undefined])( - 'should not skip ngcc-jest-processor with `skipNgcc: %s` option in `ngJest` config', - async (skipNgcc) => { - console.log = jest.fn(); - globalThis.ngJest = { - skipNgcc, - }; - - await globalSetup(); - - expect(console.log).toHaveBeenCalled(); - }, - ); -}); diff --git a/src/config/global-setup.ts b/src/config/global-setup.ts deleted file mode 100644 index 539e1f3ec5..0000000000 --- a/src/config/global-setup.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { runNgccJestProcessor } from '../utils/ngcc-jest-processor'; - -export const globalSetup = async () => { - const ngJestConfig = globalThis.ngJest; - const tsconfig = ngJestConfig?.tsconfig; - if (!ngJestConfig?.skipNgcc) { - runNgccJestProcessor(tsconfig); - } -}; diff --git a/src/utils/ngcc-jest-processor.ts b/src/utils/ngcc-jest-processor.ts deleted file mode 100644 index 7bf30bf737..0000000000 --- a/src/utils/ngcc-jest-processor.ts +++ /dev/null @@ -1,95 +0,0 @@ -/** - * Mainly copied from https://github.com/angular/angular-cli/blob/master/packages/ngtools/webpack/src/ngcc_processor.ts - * and adjusted to work with Jest - */ -import { spawnSync } from 'child_process'; -import path from 'path'; - -const ANGULAR_COMPILER_CLI_PKG_NAME = '@angular/compiler-cli'; -let ngccPath = ''; - -try { - ngccPath = require.resolve('@angular/compiler-cli/ngcc/main-ngcc.js'); -} catch { - try { - const compilerCliNgccPath = require.resolve('@angular/compiler-cli/ngcc'); - const compilerCliNgccFolder = compilerCliNgccPath.substring(0, compilerCliNgccPath.lastIndexOf(path.sep)); - ngccPath = path.resolve(compilerCliNgccFolder, 'main-ngcc.js'); - } catch { - // No ngcc in NG16 - } -} -function findNodeModulesDirectory(): string { - return ngccPath.substring(0, ngccPath.indexOf(ANGULAR_COMPILER_CLI_PKG_NAME.replace('/', path.sep))); -} - -function findAngularCompilerCliVersion(): string { - const packagePath = require.resolve(ANGULAR_COMPILER_CLI_PKG_NAME); - const substringLength = - packagePath.indexOf(ANGULAR_COMPILER_CLI_PKG_NAME.replace('/', path.sep)) + - ANGULAR_COMPILER_CLI_PKG_NAME.length; - const ngCompilerCliFolder = packagePath.substring(0, substringLength); - const ngCompilerCliPackageJson = `${ngCompilerCliFolder}/package.json`; - // eslint-disable-next-line @typescript-eslint/no-require-imports - const { version } = require(ngCompilerCliPackageJson); - - return version; -} - -const nodeModuleDirPath = findNodeModulesDirectory(); - -export const runNgccJestProcessor = (tsconfigPath: string | undefined): void => { - const ngCompilerCliVersion = findAngularCompilerCliVersion(); - const [ngMajorVersion] = ngCompilerCliVersion.split('.'); - if (parseInt(ngMajorVersion, 10) >= 16) { - console.warn(` - Running 'ngcc' is not required for Angular 16+ projects. This 'ngcc-jest-processor' script will be removed in the next major version of 'jest-preset-angular'. - Tip: To avoid this message you can remove 'jest-preset-angular/global-setup' from your jest config - `); - - return; - } - - if (ngccPath && 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, ngccBaseArgs, { - stdio: ['inherit', process.stderr, process.stderr], - }); - if (status !== 0) { - const errorMessage: string = error?.message ?? ''; - - throw new Error(`${errorMessage} NGCC failed ${errorMessage ? ', see above' : ''}.`); - } - - return; - } - - console.warn( - `Warning: Could not locate '@angular/compiler-cli' to run 'ngcc' automatically.` + - `Please make sure you are running 'ngcc-jest-processor.js' from root level of your project.` + - `'ngcc' must be run before running Jest`, - ); -};