From 75e73682554c38cc39e3b08807cf19d97c94d8f9 Mon Sep 17 00:00:00 2001 From: Ahn <27772165+ahnpnl@users.noreply.github.com> Date: Sun, 16 Jan 2022 17:06:58 +0100 Subject: [PATCH] refactor: deprecate `ngcc-jest-processor` and replace with global setup (#1275) --- examples/example-app-v11/jest-esm.config.js | 2 +- examples/example-app-v11/jest.config.js | 2 +- examples/example-app-v12-monorepo/jest-esm.config.js | 1 + examples/example-app-v12-monorepo/jest.config.js | 1 + .../projects/app1/jest-esm.config.js | 2 -- .../projects/app1/jest.config.js | 2 -- .../projects/app2/jest-esm.config.js | 2 -- .../projects/app2/jest.config.js | 2 -- examples/example-app-v12/jest-esm.config.js | 2 +- examples/example-app-v12/jest.config.js | 2 +- examples/example-app-v13/jest-esm.config.js | 2 +- examples/example-app-v13/jest.config.js | 2 +- .../packages/angular-app/jest-esm.config.js | 2 +- .../packages/angular-app/jest.config.js | 2 +- global-setup.js | 1 + ngcc-jest-processor.js | 10 ++++++++++ scripts/test-examples.js | 10 +++++++++- src/config/global-setup.ts | 3 +++ src/utils/ngcc-jest-processor.ts | 2 +- 19 files changed, 34 insertions(+), 18 deletions(-) create mode 100644 global-setup.js create mode 100644 src/config/global-setup.ts diff --git a/examples/example-app-v11/jest-esm.config.js b/examples/example-app-v11/jest-esm.config.js index 46dfd04ac9..24b00ead6e 100644 --- a/examples/example-app-v11/jest-esm.config.js +++ b/examples/example-app-v11/jest-esm.config.js @@ -1,4 +1,3 @@ -require('jest-preset-angular/ngcc-jest-processor'); const { pathsToModuleNameMapper } = require('ts-jest/utils'); const { paths } = require('./tsconfig.json').compilerOptions; @@ -12,6 +11,7 @@ module.exports = { tsconfig: '/tsconfig-esm.spec.json', }, }, + globalSetup: 'jest-preset-angular/global-setup', moduleNameMapper: { ...pathsToModuleNameMapper(paths, { prefix: '' }), tslib: 'tslib/tslib.es6.js', diff --git a/examples/example-app-v11/jest.config.js b/examples/example-app-v11/jest.config.js index d3104738f2..16eea0005b 100644 --- a/examples/example-app-v11/jest.config.js +++ b/examples/example-app-v11/jest.config.js @@ -1,10 +1,10 @@ -require('jest-preset-angular/ngcc-jest-processor'); const { pathsToModuleNameMapper } = require('ts-jest/utils'); const { paths } = require('./tsconfig.json').compilerOptions; /** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */ module.exports = { preset: 'jest-preset-angular', + globalSetup: 'jest-preset-angular/global-setup', moduleNameMapper: pathsToModuleNameMapper(paths, { prefix: '' }), setupFilesAfterEnv: ['/setup-jest.ts'], }; diff --git a/examples/example-app-v12-monorepo/jest-esm.config.js b/examples/example-app-v12-monorepo/jest-esm.config.js index 773fc4a44a..e8337cc69c 100644 --- a/examples/example-app-v12-monorepo/jest-esm.config.js +++ b/examples/example-app-v12-monorepo/jest-esm.config.js @@ -1,4 +1,5 @@ /** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */ module.exports = { + globalSetup: 'jest-preset-angular/global-setup', projects: ['/projects/app1/jest-esm.config.js', '/projects/app2/jest-esm.config.js'], }; diff --git a/examples/example-app-v12-monorepo/jest.config.js b/examples/example-app-v12-monorepo/jest.config.js index a927134368..6c1e0ce36d 100644 --- a/examples/example-app-v12-monorepo/jest.config.js +++ b/examples/example-app-v12-monorepo/jest.config.js @@ -1,4 +1,5 @@ /** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */ module.exports = { + globalSetup: 'jest-preset-angular/global-setup', projects: ['/projects/app1', '/projects/app2'], }; diff --git a/examples/example-app-v12-monorepo/projects/app1/jest-esm.config.js b/examples/example-app-v12-monorepo/projects/app1/jest-esm.config.js index 7daf45bb7f..5ff737f1f0 100644 --- a/examples/example-app-v12-monorepo/projects/app1/jest-esm.config.js +++ b/examples/example-app-v12-monorepo/projects/app1/jest-esm.config.js @@ -1,5 +1,3 @@ -require('jest-preset-angular/ngcc-jest-processor'); - /** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */ module.exports = { preset: 'jest-preset-angular/presets/defaults-esm', diff --git a/examples/example-app-v12-monorepo/projects/app1/jest.config.js b/examples/example-app-v12-monorepo/projects/app1/jest.config.js index ef5df4e9ef..4c868fc6ff 100644 --- a/examples/example-app-v12-monorepo/projects/app1/jest.config.js +++ b/examples/example-app-v12-monorepo/projects/app1/jest.config.js @@ -1,5 +1,3 @@ -require('jest-preset-angular/ngcc-jest-processor'); - /** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */ module.exports = { preset: 'jest-preset-angular', diff --git a/examples/example-app-v12-monorepo/projects/app2/jest-esm.config.js b/examples/example-app-v12-monorepo/projects/app2/jest-esm.config.js index 8f5527598a..f60215e1e7 100644 --- a/examples/example-app-v12-monorepo/projects/app2/jest-esm.config.js +++ b/examples/example-app-v12-monorepo/projects/app2/jest-esm.config.js @@ -1,5 +1,3 @@ -require('jest-preset-angular/ngcc-jest-processor'); - /** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */ module.exports = { preset: 'jest-preset-angular/presets/defaults-esm', diff --git a/examples/example-app-v12-monorepo/projects/app2/jest.config.js b/examples/example-app-v12-monorepo/projects/app2/jest.config.js index 60e19ac4cf..6e3c28c5bf 100644 --- a/examples/example-app-v12-monorepo/projects/app2/jest.config.js +++ b/examples/example-app-v12-monorepo/projects/app2/jest.config.js @@ -1,5 +1,3 @@ -require('jest-preset-angular/ngcc-jest-processor'); - /** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */ module.exports = { preset: 'jest-preset-angular', diff --git a/examples/example-app-v12/jest-esm.config.js b/examples/example-app-v12/jest-esm.config.js index 46dfd04ac9..24b00ead6e 100644 --- a/examples/example-app-v12/jest-esm.config.js +++ b/examples/example-app-v12/jest-esm.config.js @@ -1,4 +1,3 @@ -require('jest-preset-angular/ngcc-jest-processor'); const { pathsToModuleNameMapper } = require('ts-jest/utils'); const { paths } = require('./tsconfig.json').compilerOptions; @@ -12,6 +11,7 @@ module.exports = { tsconfig: '/tsconfig-esm.spec.json', }, }, + globalSetup: 'jest-preset-angular/global-setup', moduleNameMapper: { ...pathsToModuleNameMapper(paths, { prefix: '' }), tslib: 'tslib/tslib.es6.js', diff --git a/examples/example-app-v12/jest.config.js b/examples/example-app-v12/jest.config.js index d3104738f2..16eea0005b 100644 --- a/examples/example-app-v12/jest.config.js +++ b/examples/example-app-v12/jest.config.js @@ -1,10 +1,10 @@ -require('jest-preset-angular/ngcc-jest-processor'); const { pathsToModuleNameMapper } = require('ts-jest/utils'); const { paths } = require('./tsconfig.json').compilerOptions; /** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */ module.exports = { preset: 'jest-preset-angular', + globalSetup: 'jest-preset-angular/global-setup', moduleNameMapper: pathsToModuleNameMapper(paths, { prefix: '' }), setupFilesAfterEnv: ['/setup-jest.ts'], }; diff --git a/examples/example-app-v13/jest-esm.config.js b/examples/example-app-v13/jest-esm.config.js index 46dfd04ac9..24b00ead6e 100644 --- a/examples/example-app-v13/jest-esm.config.js +++ b/examples/example-app-v13/jest-esm.config.js @@ -1,4 +1,3 @@ -require('jest-preset-angular/ngcc-jest-processor'); const { pathsToModuleNameMapper } = require('ts-jest/utils'); const { paths } = require('./tsconfig.json').compilerOptions; @@ -12,6 +11,7 @@ module.exports = { tsconfig: '/tsconfig-esm.spec.json', }, }, + globalSetup: 'jest-preset-angular/global-setup', moduleNameMapper: { ...pathsToModuleNameMapper(paths, { prefix: '' }), tslib: 'tslib/tslib.es6.js', diff --git a/examples/example-app-v13/jest.config.js b/examples/example-app-v13/jest.config.js index d3104738f2..16eea0005b 100644 --- a/examples/example-app-v13/jest.config.js +++ b/examples/example-app-v13/jest.config.js @@ -1,10 +1,10 @@ -require('jest-preset-angular/ngcc-jest-processor'); const { pathsToModuleNameMapper } = require('ts-jest/utils'); const { paths } = require('./tsconfig.json').compilerOptions; /** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */ module.exports = { preset: 'jest-preset-angular', + globalSetup: 'jest-preset-angular/global-setup', moduleNameMapper: pathsToModuleNameMapper(paths, { prefix: '' }), setupFilesAfterEnv: ['/setup-jest.ts'], }; diff --git a/examples/example-app-yarn-workspace/packages/angular-app/jest-esm.config.js b/examples/example-app-yarn-workspace/packages/angular-app/jest-esm.config.js index 46dfd04ac9..24b00ead6e 100644 --- a/examples/example-app-yarn-workspace/packages/angular-app/jest-esm.config.js +++ b/examples/example-app-yarn-workspace/packages/angular-app/jest-esm.config.js @@ -1,4 +1,3 @@ -require('jest-preset-angular/ngcc-jest-processor'); const { pathsToModuleNameMapper } = require('ts-jest/utils'); const { paths } = require('./tsconfig.json').compilerOptions; @@ -12,6 +11,7 @@ module.exports = { tsconfig: '/tsconfig-esm.spec.json', }, }, + globalSetup: 'jest-preset-angular/global-setup', moduleNameMapper: { ...pathsToModuleNameMapper(paths, { prefix: '' }), tslib: 'tslib/tslib.es6.js', diff --git a/examples/example-app-yarn-workspace/packages/angular-app/jest.config.js b/examples/example-app-yarn-workspace/packages/angular-app/jest.config.js index d3104738f2..16eea0005b 100644 --- a/examples/example-app-yarn-workspace/packages/angular-app/jest.config.js +++ b/examples/example-app-yarn-workspace/packages/angular-app/jest.config.js @@ -1,10 +1,10 @@ -require('jest-preset-angular/ngcc-jest-processor'); const { pathsToModuleNameMapper } = require('ts-jest/utils'); const { paths } = require('./tsconfig.json').compilerOptions; /** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */ module.exports = { preset: 'jest-preset-angular', + globalSetup: 'jest-preset-angular/global-setup', moduleNameMapper: pathsToModuleNameMapper(paths, { prefix: '' }), setupFilesAfterEnv: ['/setup-jest.ts'], }; diff --git a/global-setup.js b/global-setup.js new file mode 100644 index 0000000000..e34577ea79 --- /dev/null +++ b/global-setup.js @@ -0,0 +1 @@ +module.exports = require('./build/config/global-setup'); diff --git a/ngcc-jest-processor.js b/ngcc-jest-processor.js index eca2ea7e6a..b37f8baa78 100644 --- a/ngcc-jest-processor.js +++ b/ngcc-jest-processor.js @@ -1 +1,11 @@ +console.warn( + 'ng-jest-processor is deprecated and will be removed in the next major version v12.' + + ' Please use jest-preset-angular/global-setup in Jest config instead, e.g. \n' + + ` + module.exports = { + globalSetup: 'jest-preset-angular/global-setup', + }; + ` +); + module.exports = require('./build/utils/ngcc-jest-processor'); diff --git a/scripts/test-examples.js b/scripts/test-examples.js index 4a2762fa1b..34a0b03fba 100755 --- a/scripts/test-examples.js +++ b/scripts/test-examples.js @@ -35,7 +35,15 @@ const executeTest = (projectPath) => { logger.log('installing bundled version of jest-preset-angular'); logger.log(); - ['build', 'presets', 'jest-preset.js', 'ngcc-jest-processor.js', 'package.json', 'setup-jest.js'].forEach((asset) => { + [ + 'build', + 'presets', + 'global-setup.js', + 'jest-preset.js', + 'ngcc-jest-processor.js', + 'package.json', + 'setup-jest.js', + ].forEach((asset) => { const assetToReplace = join(projectPath, 'node_modules', 'jest-preset-angular', asset); const assetToCopy = join(rootDir, asset); copySync(assetToCopy, assetToReplace); diff --git a/src/config/global-setup.ts b/src/config/global-setup.ts new file mode 100644 index 0000000000..cd1b62b6f0 --- /dev/null +++ b/src/config/global-setup.ts @@ -0,0 +1,3 @@ +export = async () => { + await import('../utils/ngcc-jest-processor'); +}; diff --git a/src/utils/ngcc-jest-processor.ts b/src/utils/ngcc-jest-processor.ts index b363d60dc7..91fef3da94 100644 --- a/src/utils/ngcc-jest-processor.ts +++ b/src/utils/ngcc-jest-processor.ts @@ -23,7 +23,7 @@ const nodeModuleDirPath = findNodeModulesDirectory(); if (!process.argv.find((arg) => IGNORE_ARGS.includes(arg))) { if (nodeModuleDirPath) { - process.stdout.write('ngcc-jest-processor: running ngcc\n'); + process.stdout.write('\nngcc-jest-processor: running ngcc\n'); // 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.