Skip to content

Commit

Permalink
feat(presets): add ESM preset (#723)
Browse files Browse the repository at this point in the history
Related to #710
  • Loading branch information
ahnpnl authored Jan 11, 2021
1 parent a2166f8 commit b0073b0
Show file tree
Hide file tree
Showing 24 changed files with 157 additions and 92 deletions.
2 changes: 1 addition & 1 deletion e2e/test-app-v10/jest-cjs-iso.config.js
Original file line number Diff line number Diff line change
@@ -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: '<rootDir>/tsconfig-cjs.spec.json',
Expand Down
8 changes: 1 addition & 7 deletions e2e/test-app-v10/jest-cjs-uniso.config.js
Original file line number Diff line number Diff line change
@@ -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: '<rootDir>/tsconfig-cjs.spec.json',
stringifyContentPathRegex: '\\.html$',
}
},
preset: 'jest-preset-angular',
};
4 changes: 1 addition & 3 deletions e2e/test-app-v10/jest-esm-iso.config.js
Original file line number Diff line number Diff line change
@@ -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: '<rootDir>/tsconfig-esm.spec.json',
Expand All @@ -17,5 +16,4 @@ module.exports = {
...baseConfig.moduleNameMapper,
'tslib': '<rootDir>/node_modules/tslib/tslib.es6.js',
},
transformIgnorePatterns: ['node_modules/(?!tslib)'],
};
11 changes: 1 addition & 10 deletions e2e/test-app-v10/jest-esm-uniso.config.js
Original file line number Diff line number Diff line change
@@ -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: '<rootDir>/tsconfig-esm.spec.json',
stringifyContentPathRegex: '\\.html$',
useESM: true,
}
},
preset: 'jest-preset-angular/presets/defaults-esm',
moduleNameMapper: {
...baseConfig.moduleNameMapper,
'tslib': '<rootDir>/node_modules/tslib/tslib.es6.js',
},
transformIgnorePatterns: ['node_modules/(?!tslib)'],
};
3 changes: 2 additions & 1 deletion e2e/test-app-v10/jest.config.js
Original file line number Diff line number Diff line change
@@ -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)$': '<rootDir>/src/__tests__/__mocks__/image.js',
},
Expand Down
2 changes: 1 addition & 1 deletion e2e/test-app-v11/jest-cjs-iso.config.js
Original file line number Diff line number Diff line change
@@ -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: '<rootDir>/tsconfig-cjs.spec.json',
Expand Down
8 changes: 1 addition & 7 deletions e2e/test-app-v11/jest-cjs-uniso.config.js
Original file line number Diff line number Diff line change
@@ -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: '<rootDir>/tsconfig-cjs.spec.json',
stringifyContentPathRegex: '\\.html$',
}
},
preset: 'jest-preset-angular',
};
4 changes: 1 addition & 3 deletions e2e/test-app-v11/jest-esm-iso.config.js
Original file line number Diff line number Diff line change
@@ -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: '<rootDir>/tsconfig-esm.spec.json',
Expand All @@ -17,5 +16,4 @@ module.exports = {
...baseConfig.moduleNameMapper,
'tslib': '<rootDir>/node_modules/tslib/tslib.es6.js',
},
transformIgnorePatterns: ['node_modules/(?!tslib)'],
};
11 changes: 1 addition & 10 deletions e2e/test-app-v11/jest-esm-uniso.config.js
Original file line number Diff line number Diff line change
@@ -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: '<rootDir>/tsconfig-esm.spec.json',
stringifyContentPathRegex: '\\.html$',
useESM: true,
}
},
preset: 'jest-preset-angular/presets/defaults-esm',
moduleNameMapper: {
...baseConfig.moduleNameMapper,
'tslib': '<rootDir>/node_modules/tslib/tslib.es6.js',
},
transformIgnorePatterns: ['node_modules/(?!tslib)'],
};
3 changes: 2 additions & 1 deletion e2e/test-app-v11/jest.config.js
Original file line number Diff line number Diff line change
@@ -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)$': '<rootDir>/src/__tests__/__mocks__/image.js',
},
Expand Down
2 changes: 1 addition & 1 deletion e2e/test-app-v9/jest-cjs-iso.config.js
Original file line number Diff line number Diff line change
@@ -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: '<rootDir>/tsconfig-cjs.spec.json',
Expand Down
8 changes: 1 addition & 7 deletions e2e/test-app-v9/jest-cjs-uniso.config.js
Original file line number Diff line number Diff line change
@@ -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: '<rootDir>/tsconfig-cjs.spec.json',
stringifyContentPathRegex: '\\.html$',
}
},
preset: 'jest-preset-angular',
};
4 changes: 1 addition & 3 deletions e2e/test-app-v9/jest-esm-iso.config.js
Original file line number Diff line number Diff line change
@@ -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: '<rootDir>/tsconfig-esm.spec.json',
Expand All @@ -17,5 +16,4 @@ module.exports = {
...baseConfig.moduleNameMapper,
'tslib': '<rootDir>/node_modules/tslib/tslib.es6.js',
},
transformIgnorePatterns: ['node_modules/(?!tslib)'],
};
11 changes: 1 addition & 10 deletions e2e/test-app-v9/jest-esm-uniso.config.js
Original file line number Diff line number Diff line change
@@ -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: '<rootDir>/tsconfig-esm.spec.json',
stringifyContentPathRegex: '\\.html$',
useESM: true,
}
},
preset: 'jest-preset-angular/presets/defaults-esm',
moduleNameMapper: {
...baseConfig.moduleNameMapper,
'tslib': '<rootDir>/node_modules/tslib/tslib.es6.js',
},
transformIgnorePatterns: ['node_modules/(?!tslib)'],
};
3 changes: 2 additions & 1 deletion e2e/test-app-v9/jest.config.js
Original file line number Diff line number Diff line change
@@ -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)$': '<rootDir>/src/__tests__/__mocks__/image.js',
},
Expand Down
24 changes: 1 addition & 23 deletions jest-preset.js
Original file line number Diff line number Diff line change
@@ -1,23 +1 @@
const snapshotSerializers = require('./build/serializers');

module.exports = {
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
stringifyContentPathRegex: '\\.html$',
},
},
testEnvironment: 'jsdom',
transform: {
'^.+\\.(ts|js|html)$': 'jest-preset-angular',
},
moduleFileExtensions: ['ts', 'html', 'js', 'json'],
moduleNameMapper: {
'^src/(.*)$': '<rootDir>/src/$1',
'^app/(.*)$': '<rootDir>/src/app/$1',
'^assets/(.*)$': '<rootDir>/src/assets/$1',
'^environments/(.*)$': '<rootDir>/src/environments/$1',
},
transformIgnorePatterns: ['node_modules/(?!@ngrx)'],
snapshotSerializers,
};
module.exports = require('./presets').defaults;
2 changes: 2 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require('./ngcc-jest-processor');

/** @type {import('ts-jest/dist/types').ProjectConfigTsJest} */
module.exports = {
preset: 'ts-jest',
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
1 change: 1 addition & 0 deletions presets/defaults-esm/jest-preset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('..').defaultsESM;
1 change: 1 addition & 0 deletions presets/defaults/jest-preset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('..').defaults;
47 changes: 47 additions & 0 deletions presets/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const snapshotSerializers = require('../build/serializers');

const basePreset = {
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
stringifyContentPathRegex: '\\.html$',
},
},
testEnvironment: 'jsdom',
transform: {
'^.+\\.(ts|js|html)$': 'jest-preset-angular',
},
moduleFileExtensions: ['ts', 'html', 'js', 'json'],
moduleNameMapper: {
'^src/(.*)$': '<rootDir>/src/$1',
'^app/(.*)$': '<rootDir>/src/app/$1',
'^assets/(.*)$': '<rootDir>/src/assets/$1',
'^environments/(.*)$': '<rootDir>/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': '<rootDir>/node_modules/tslib/tslib.es6.js',
},
transformIgnorePatterns: [
'node_modules/(?!tslib)',
],
}
},
}
1 change: 1 addition & 0 deletions scripts/e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand Down
Loading

0 comments on commit b0073b0

Please sign in to comment.