From b0073b0f3a7e24f06d136367a1c2e676ac76e59e Mon Sep 17 00:00:00 2001 From: Ahn Date: Mon, 11 Jan 2021 11:11:03 +0100 Subject: [PATCH] feat(presets): add ESM preset (#723) Related to #710 --- e2e/test-app-v10/jest-cjs-iso.config.js | 2 +- e2e/test-app-v10/jest-cjs-uniso.config.js | 8 +- e2e/test-app-v10/jest-esm-iso.config.js | 4 +- e2e/test-app-v10/jest-esm-uniso.config.js | 11 +-- e2e/test-app-v10/jest.config.js | 3 +- e2e/test-app-v11/jest-cjs-iso.config.js | 2 +- e2e/test-app-v11/jest-cjs-uniso.config.js | 8 +- e2e/test-app-v11/jest-esm-iso.config.js | 4 +- e2e/test-app-v11/jest-esm-uniso.config.js | 11 +-- e2e/test-app-v11/jest.config.js | 3 +- e2e/test-app-v9/jest-cjs-iso.config.js | 2 +- e2e/test-app-v9/jest-cjs-uniso.config.js | 8 +- e2e/test-app-v9/jest-esm-iso.config.js | 4 +- e2e/test-app-v9/jest-esm-uniso.config.js | 11 +-- e2e/test-app-v9/jest.config.js | 3 +- jest-preset.js | 24 +----- jest.config.js | 2 + package.json | 6 +- presets/defaults-esm/jest-preset.js | 1 + presets/defaults/jest-preset.js | 1 + presets/index.js | 47 ++++++++++++ scripts/e2e.js | 1 + .../__snapshots__/jest-preset.spec.ts.snap | 73 +++++++++++++++++++ src/__tests__/jest-preset.spec.ts | 10 +++ 24 files changed, 157 insertions(+), 92 deletions(-) create mode 100644 presets/defaults-esm/jest-preset.js create mode 100644 presets/defaults/jest-preset.js create mode 100644 presets/index.js create mode 100644 src/__tests__/__snapshots__/jest-preset.spec.ts.snap create mode 100644 src/__tests__/jest-preset.spec.ts diff --git a/e2e/test-app-v10/jest-cjs-iso.config.js b/e2e/test-app-v10/jest-cjs-iso.config.js index 9886578a65..feaaef5280 100644 --- a/e2e/test-app-v10/jest-cjs-iso.config.js +++ b/e2e/test-app-v10/jest-cjs-iso.config.js @@ -1,9 +1,9 @@ -require('jest-preset-angular/ngcc-jest-processor'); const baseConfig = require('./jest.config'); /** @type {import('ts-jest/dist/types').ProjectConfigTsJest} */ module.exports = { ...baseConfig, + preset: 'jest-preset-angular', globals: { 'ts-jest': { tsconfig: '/tsconfig-cjs.spec.json', diff --git a/e2e/test-app-v10/jest-cjs-uniso.config.js b/e2e/test-app-v10/jest-cjs-uniso.config.js index bc6d8ddef7..2e57cc6635 100644 --- a/e2e/test-app-v10/jest-cjs-uniso.config.js +++ b/e2e/test-app-v10/jest-cjs-uniso.config.js @@ -1,13 +1,7 @@ -require('jest-preset-angular/ngcc-jest-processor'); const baseConfig = require('./jest.config'); /** @type {import('ts-jest/dist/types').ProjectConfigTsJest} */ module.exports = { ...baseConfig, - globals: { - 'ts-jest': { - tsconfig: '/tsconfig-cjs.spec.json', - stringifyContentPathRegex: '\\.html$', - } - }, + preset: 'jest-preset-angular', }; diff --git a/e2e/test-app-v10/jest-esm-iso.config.js b/e2e/test-app-v10/jest-esm-iso.config.js index 98289d0076..ca4ef72bf8 100644 --- a/e2e/test-app-v10/jest-esm-iso.config.js +++ b/e2e/test-app-v10/jest-esm-iso.config.js @@ -1,10 +1,9 @@ -require('jest-preset-angular/ngcc-jest-processor'); const baseConfig = require('./jest.config'); /** @type {import('ts-jest/dist/types').ProjectConfigTsJest} */ module.exports = { ...baseConfig, - extensionsToTreatAsEsm: ['.ts'], + preset: 'jest-preset-angular/presets/defaults-esm', globals: { 'ts-jest': { tsconfig: '/tsconfig-esm.spec.json', @@ -17,5 +16,4 @@ module.exports = { ...baseConfig.moduleNameMapper, 'tslib': '/node_modules/tslib/tslib.es6.js', }, - transformIgnorePatterns: ['node_modules/(?!tslib)'], }; diff --git a/e2e/test-app-v10/jest-esm-uniso.config.js b/e2e/test-app-v10/jest-esm-uniso.config.js index 9997a613e9..42ea0ed46b 100644 --- a/e2e/test-app-v10/jest-esm-uniso.config.js +++ b/e2e/test-app-v10/jest-esm-uniso.config.js @@ -1,20 +1,11 @@ -require('jest-preset-angular/ngcc-jest-processor'); const baseConfig = require('./jest.config'); /** @type {import('ts-jest/dist/types').ProjectConfigTsJest} */ module.exports = { ...baseConfig, - extensionsToTreatAsEsm: ['.ts'], - globals: { - 'ts-jest': { - tsconfig: '/tsconfig-esm.spec.json', - stringifyContentPathRegex: '\\.html$', - useESM: true, - } - }, + preset: 'jest-preset-angular/presets/defaults-esm', moduleNameMapper: { ...baseConfig.moduleNameMapper, 'tslib': '/node_modules/tslib/tslib.es6.js', }, - transformIgnorePatterns: ['node_modules/(?!tslib)'], }; diff --git a/e2e/test-app-v10/jest.config.js b/e2e/test-app-v10/jest.config.js index 7f8f3301f2..1cd86121c3 100644 --- a/e2e/test-app-v10/jest.config.js +++ b/e2e/test-app-v10/jest.config.js @@ -1,6 +1,7 @@ +require('jest-preset-angular/ngcc-jest-processor'); + /** @type {import('@jest/types').Config.InitialOptions} */ module.exports = { - preset: 'jest-preset-angular', moduleNameMapper: { '\\.(jpg|jpeg|png)$': '/src/__tests__/__mocks__/image.js', }, diff --git a/e2e/test-app-v11/jest-cjs-iso.config.js b/e2e/test-app-v11/jest-cjs-iso.config.js index 9886578a65..feaaef5280 100644 --- a/e2e/test-app-v11/jest-cjs-iso.config.js +++ b/e2e/test-app-v11/jest-cjs-iso.config.js @@ -1,9 +1,9 @@ -require('jest-preset-angular/ngcc-jest-processor'); const baseConfig = require('./jest.config'); /** @type {import('ts-jest/dist/types').ProjectConfigTsJest} */ module.exports = { ...baseConfig, + preset: 'jest-preset-angular', globals: { 'ts-jest': { tsconfig: '/tsconfig-cjs.spec.json', diff --git a/e2e/test-app-v11/jest-cjs-uniso.config.js b/e2e/test-app-v11/jest-cjs-uniso.config.js index bc6d8ddef7..2e57cc6635 100644 --- a/e2e/test-app-v11/jest-cjs-uniso.config.js +++ b/e2e/test-app-v11/jest-cjs-uniso.config.js @@ -1,13 +1,7 @@ -require('jest-preset-angular/ngcc-jest-processor'); const baseConfig = require('./jest.config'); /** @type {import('ts-jest/dist/types').ProjectConfigTsJest} */ module.exports = { ...baseConfig, - globals: { - 'ts-jest': { - tsconfig: '/tsconfig-cjs.spec.json', - stringifyContentPathRegex: '\\.html$', - } - }, + preset: 'jest-preset-angular', }; diff --git a/e2e/test-app-v11/jest-esm-iso.config.js b/e2e/test-app-v11/jest-esm-iso.config.js index 98289d0076..ca4ef72bf8 100644 --- a/e2e/test-app-v11/jest-esm-iso.config.js +++ b/e2e/test-app-v11/jest-esm-iso.config.js @@ -1,10 +1,9 @@ -require('jest-preset-angular/ngcc-jest-processor'); const baseConfig = require('./jest.config'); /** @type {import('ts-jest/dist/types').ProjectConfigTsJest} */ module.exports = { ...baseConfig, - extensionsToTreatAsEsm: ['.ts'], + preset: 'jest-preset-angular/presets/defaults-esm', globals: { 'ts-jest': { tsconfig: '/tsconfig-esm.spec.json', @@ -17,5 +16,4 @@ module.exports = { ...baseConfig.moduleNameMapper, 'tslib': '/node_modules/tslib/tslib.es6.js', }, - transformIgnorePatterns: ['node_modules/(?!tslib)'], }; diff --git a/e2e/test-app-v11/jest-esm-uniso.config.js b/e2e/test-app-v11/jest-esm-uniso.config.js index 9997a613e9..42ea0ed46b 100644 --- a/e2e/test-app-v11/jest-esm-uniso.config.js +++ b/e2e/test-app-v11/jest-esm-uniso.config.js @@ -1,20 +1,11 @@ -require('jest-preset-angular/ngcc-jest-processor'); const baseConfig = require('./jest.config'); /** @type {import('ts-jest/dist/types').ProjectConfigTsJest} */ module.exports = { ...baseConfig, - extensionsToTreatAsEsm: ['.ts'], - globals: { - 'ts-jest': { - tsconfig: '/tsconfig-esm.spec.json', - stringifyContentPathRegex: '\\.html$', - useESM: true, - } - }, + preset: 'jest-preset-angular/presets/defaults-esm', moduleNameMapper: { ...baseConfig.moduleNameMapper, 'tslib': '/node_modules/tslib/tslib.es6.js', }, - transformIgnorePatterns: ['node_modules/(?!tslib)'], }; diff --git a/e2e/test-app-v11/jest.config.js b/e2e/test-app-v11/jest.config.js index 7f8f3301f2..1cd86121c3 100644 --- a/e2e/test-app-v11/jest.config.js +++ b/e2e/test-app-v11/jest.config.js @@ -1,6 +1,7 @@ +require('jest-preset-angular/ngcc-jest-processor'); + /** @type {import('@jest/types').Config.InitialOptions} */ module.exports = { - preset: 'jest-preset-angular', moduleNameMapper: { '\\.(jpg|jpeg|png)$': '/src/__tests__/__mocks__/image.js', }, diff --git a/e2e/test-app-v9/jest-cjs-iso.config.js b/e2e/test-app-v9/jest-cjs-iso.config.js index 9886578a65..feaaef5280 100644 --- a/e2e/test-app-v9/jest-cjs-iso.config.js +++ b/e2e/test-app-v9/jest-cjs-iso.config.js @@ -1,9 +1,9 @@ -require('jest-preset-angular/ngcc-jest-processor'); const baseConfig = require('./jest.config'); /** @type {import('ts-jest/dist/types').ProjectConfigTsJest} */ module.exports = { ...baseConfig, + preset: 'jest-preset-angular', globals: { 'ts-jest': { tsconfig: '/tsconfig-cjs.spec.json', diff --git a/e2e/test-app-v9/jest-cjs-uniso.config.js b/e2e/test-app-v9/jest-cjs-uniso.config.js index bc6d8ddef7..2e57cc6635 100644 --- a/e2e/test-app-v9/jest-cjs-uniso.config.js +++ b/e2e/test-app-v9/jest-cjs-uniso.config.js @@ -1,13 +1,7 @@ -require('jest-preset-angular/ngcc-jest-processor'); const baseConfig = require('./jest.config'); /** @type {import('ts-jest/dist/types').ProjectConfigTsJest} */ module.exports = { ...baseConfig, - globals: { - 'ts-jest': { - tsconfig: '/tsconfig-cjs.spec.json', - stringifyContentPathRegex: '\\.html$', - } - }, + preset: 'jest-preset-angular', }; diff --git a/e2e/test-app-v9/jest-esm-iso.config.js b/e2e/test-app-v9/jest-esm-iso.config.js index 98289d0076..ca4ef72bf8 100644 --- a/e2e/test-app-v9/jest-esm-iso.config.js +++ b/e2e/test-app-v9/jest-esm-iso.config.js @@ -1,10 +1,9 @@ -require('jest-preset-angular/ngcc-jest-processor'); const baseConfig = require('./jest.config'); /** @type {import('ts-jest/dist/types').ProjectConfigTsJest} */ module.exports = { ...baseConfig, - extensionsToTreatAsEsm: ['.ts'], + preset: 'jest-preset-angular/presets/defaults-esm', globals: { 'ts-jest': { tsconfig: '/tsconfig-esm.spec.json', @@ -17,5 +16,4 @@ module.exports = { ...baseConfig.moduleNameMapper, 'tslib': '/node_modules/tslib/tslib.es6.js', }, - transformIgnorePatterns: ['node_modules/(?!tslib)'], }; diff --git a/e2e/test-app-v9/jest-esm-uniso.config.js b/e2e/test-app-v9/jest-esm-uniso.config.js index 9997a613e9..42ea0ed46b 100644 --- a/e2e/test-app-v9/jest-esm-uniso.config.js +++ b/e2e/test-app-v9/jest-esm-uniso.config.js @@ -1,20 +1,11 @@ -require('jest-preset-angular/ngcc-jest-processor'); const baseConfig = require('./jest.config'); /** @type {import('ts-jest/dist/types').ProjectConfigTsJest} */ module.exports = { ...baseConfig, - extensionsToTreatAsEsm: ['.ts'], - globals: { - 'ts-jest': { - tsconfig: '/tsconfig-esm.spec.json', - stringifyContentPathRegex: '\\.html$', - useESM: true, - } - }, + preset: 'jest-preset-angular/presets/defaults-esm', moduleNameMapper: { ...baseConfig.moduleNameMapper, 'tslib': '/node_modules/tslib/tslib.es6.js', }, - transformIgnorePatterns: ['node_modules/(?!tslib)'], }; diff --git a/e2e/test-app-v9/jest.config.js b/e2e/test-app-v9/jest.config.js index 7f8f3301f2..1cd86121c3 100644 --- a/e2e/test-app-v9/jest.config.js +++ b/e2e/test-app-v9/jest.config.js @@ -1,6 +1,7 @@ +require('jest-preset-angular/ngcc-jest-processor'); + /** @type {import('@jest/types').Config.InitialOptions} */ module.exports = { - preset: 'jest-preset-angular', moduleNameMapper: { '\\.(jpg|jpeg|png)$': '/src/__tests__/__mocks__/image.js', }, diff --git a/jest-preset.js b/jest-preset.js index d328b69c6b..aa999a5bac 100644 --- a/jest-preset.js +++ b/jest-preset.js @@ -1,23 +1 @@ -const snapshotSerializers = require('./build/serializers'); - -module.exports = { - globals: { - 'ts-jest': { - tsconfig: '/tsconfig.spec.json', - stringifyContentPathRegex: '\\.html$', - }, - }, - testEnvironment: 'jsdom', - transform: { - '^.+\\.(ts|js|html)$': 'jest-preset-angular', - }, - moduleFileExtensions: ['ts', 'html', 'js', 'json'], - moduleNameMapper: { - '^src/(.*)$': '/src/$1', - '^app/(.*)$': '/src/app/$1', - '^assets/(.*)$': '/src/assets/$1', - '^environments/(.*)$': '/src/environments/$1', - }, - transformIgnorePatterns: ['node_modules/(?!@ngrx)'], - snapshotSerializers, -}; +module.exports = require('./presets').defaults; diff --git a/jest.config.js b/jest.config.js index 41302de09a..9f3f9213e0 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,3 +1,5 @@ +require('./ngcc-jest-processor'); + /** @type {import('ts-jest/dist/types').ProjectConfigTsJest} */ module.exports = { preset: 'ts-jest', diff --git a/package.json b/package.json index 949cea8780..df5c9a0ccb 100644 --- a/package.json +++ b/package.json @@ -23,9 +23,9 @@ "build": "tsc -p tsconfig.build.json", "lint": "eslint --ext .js,.ts .", "lint:fix": "eslint --fix --ext .js,.ts .", - "pretest": "node ./ngcc-jest-processor.js", - "test-cjs:unit": "yarn pretest && jest -i", - "test-esm:unit": "yarn pretest && node --experimental-vm-modules ./node_modules/jest/bin/jest.js -i -c=jest-esm.config.js", + "test:unit": "yarn test-cjs:unit && yarn test-esm:unit", + "test-cjs:unit": "jest -i", + "test-esm:unit": "node --experimental-vm-modules ./node_modules/jest/bin/jest.js -i -c=jest-esm.config.js", "test:e2e": "node scripts/e2e.js", "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 1" }, diff --git a/presets/defaults-esm/jest-preset.js b/presets/defaults-esm/jest-preset.js new file mode 100644 index 0000000000..1de8f2cb4c --- /dev/null +++ b/presets/defaults-esm/jest-preset.js @@ -0,0 +1 @@ +module.exports = require('..').defaultsESM; diff --git a/presets/defaults/jest-preset.js b/presets/defaults/jest-preset.js new file mode 100644 index 0000000000..e5b54b3f78 --- /dev/null +++ b/presets/defaults/jest-preset.js @@ -0,0 +1 @@ +module.exports = require('..').defaults; diff --git a/presets/index.js b/presets/index.js new file mode 100644 index 0000000000..e645586955 --- /dev/null +++ b/presets/index.js @@ -0,0 +1,47 @@ +const snapshotSerializers = require('../build/serializers'); + +const basePreset = { + globals: { + 'ts-jest': { + tsconfig: '/tsconfig.spec.json', + stringifyContentPathRegex: '\\.html$', + }, + }, + testEnvironment: 'jsdom', + transform: { + '^.+\\.(ts|js|html)$': 'jest-preset-angular', + }, + moduleFileExtensions: ['ts', 'html', 'js', 'json'], + moduleNameMapper: { + '^src/(.*)$': '/src/$1', + '^app/(.*)$': '/src/app/$1', + '^assets/(.*)$': '/src/assets/$1', + '^environments/(.*)$': '/src/environments/$1', + }, + snapshotSerializers, +} + +module.exports = { + get defaults() { + return basePreset; + }, + get defaultsESM() { + return { + ...basePreset, + extensionsToTreatAsEsm: ['.ts'], + globals: { + 'ts-jest': { + ...basePreset.globals['ts-jest'], + useESM: true, + }, + }, + moduleNameMapper: { + ...basePreset.moduleNameMapper, + 'tslib': '/node_modules/tslib/tslib.es6.js', + }, + transformIgnorePatterns: [ + 'node_modules/(?!tslib)', + ], + } + }, +} diff --git a/scripts/e2e.js b/scripts/e2e.js index 028a7ce85e..265bcf923d 100755 --- a/scripts/e2e.js +++ b/scripts/e2e.js @@ -43,6 +43,7 @@ const executeTest = (projectRealPath) => { logger.log(); copySync(join(cwd, 'jest-preset.js'), `${presetDir}/jest-preset.js`); + copySync(join(cwd, 'presets'), `${presetDir}/presets`); copySync(join(cwd, 'ngcc-jest-processor.js'), `${presetDir}/ngcc-jest-processor.js`); copySync(join(cwd, 'setup-jest.js'), `${presetDir}/setup-jest.js`); copySync(join(cwd, 'package.json'), `${presetDir}/package.json`); diff --git a/src/__tests__/__snapshots__/jest-preset.spec.ts.snap b/src/__tests__/__snapshots__/jest-preset.spec.ts.snap new file mode 100644 index 0000000000..dd31f9c01e --- /dev/null +++ b/src/__tests__/__snapshots__/jest-preset.spec.ts.snap @@ -0,0 +1,73 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Jest presets should return the correct jest config 1`] = ` +Object { + "globals": Object { + "ts-jest": Object { + "stringifyContentPathRegex": "\\\\.html$", + "tsconfig": "/tsconfig.spec.json", + }, + }, + "moduleFileExtensions": Array [ + "ts", + "html", + "js", + "json", + ], + "moduleNameMapper": Object { + "^app/(.*)$": "/src/app/$1", + "^assets/(.*)$": "/src/assets/$1", + "^environments/(.*)$": "/src/environments/$1", + "^src/(.*)$": "/src/$1", + }, + "snapshotSerializers": Array [ + "jest-preset-angular/build/serializers/html-comment", + "jest-preset-angular/build/serializers/ng-snapshot", + "jest-preset-angular/build/serializers/no-ng-attributes", + ], + "testEnvironment": "jsdom", + "transform": Object { + "^.+\\\\.(ts|js|html)$": "jest-preset-angular", + }, +} +`; + +exports[`Jest presets should return the correct jest config 2`] = ` +Object { + "extensionsToTreatAsEsm": Array [ + ".ts", + ], + "globals": Object { + "ts-jest": Object { + "stringifyContentPathRegex": "\\\\.html$", + "tsconfig": "/tsconfig.spec.json", + "useESM": true, + }, + }, + "moduleFileExtensions": Array [ + "ts", + "html", + "js", + "json", + ], + "moduleNameMapper": Object { + "^app/(.*)$": "/src/app/$1", + "^assets/(.*)$": "/src/assets/$1", + "^environments/(.*)$": "/src/environments/$1", + "^src/(.*)$": "/src/$1", + "tslib": "/node_modules/tslib/tslib.es6.js", + }, + "snapshotSerializers": Array [ + "jest-preset-angular/build/serializers/html-comment", + "jest-preset-angular/build/serializers/ng-snapshot", + "jest-preset-angular/build/serializers/no-ng-attributes", + ], + "testEnvironment": "jsdom", + "transform": Object { + "^.+\\\\.(ts|js|html)$": "jest-preset-angular", + }, + "transformIgnorePatterns": Array [ + "node_modules/(?!tslib)", + ], +} +`; diff --git a/src/__tests__/jest-preset.spec.ts b/src/__tests__/jest-preset.spec.ts new file mode 100644 index 0000000000..b9713ac81e --- /dev/null +++ b/src/__tests__/jest-preset.spec.ts @@ -0,0 +1,10 @@ +// @ts-expect-error for now we don't ship assets with .d.ts +import defaultsESMPreset from '../../presets/defaults-esm/jest-preset'; +// @ts-expect-error for now we don't ship assets with .d.ts +import defaultsPreset from '../../presets/defaults/jest-preset'; + +describe('Jest presets', () => { + test.each([defaultsPreset, defaultsESMPreset])('should return the correct jest config', (preset) => { + expect(preset).toMatchSnapshot(); + }); +});