Skip to content

Commit

Permalink
feat: add global-setup.mjs to use with Jest ESM mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ahnpnl committed Jun 26, 2022
1 parent 7c5ef39 commit 10a5c32
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 19 deletions.
2 changes: 1 addition & 1 deletion examples/example-app-monorepo/jest-esm-isolated.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ globalThis.ngJest = {

/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
const jestConfig = {
globalSetup: 'jest-preset-angular/global-setup',
globalSetup: 'jest-preset-angular/global-setup.mjs',
projects: ['<rootDir>/apps/app1/jest-esm-isolated.config.mjs', '<rootDir>/libs/user/jest-esm-isolated.config.mjs'],
};

Expand Down
2 changes: 1 addition & 1 deletion examples/example-app-monorepo/jest-esm.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ globalThis.ngJest = {

/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
const jestConfig = {
globalSetup: 'jest-preset-angular/global-setup',
globalSetup: 'jest-preset-angular/global-setup.mjs',
projects: ['<rootDir>/apps/app1/jest-esm.config.mjs', '<rootDir>/libs/user/jest-esm.config.mjs'],
};

Expand Down
2 changes: 1 addition & 1 deletion examples/example-app-v12/jest-esm.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const jestConfig = {
tsconfig: '<rootDir>/tsconfig-esm.spec.json',
},
},
globalSetup: 'jest-preset-angular/global-setup',
globalSetup: 'jest-preset-angular/global-setup.mjs',
setupFilesAfterEnv: ['<rootDir>/setup-jest-esm.ts'],
}

Expand Down
2 changes: 1 addition & 1 deletion examples/example-app-v13/jest-esm.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const jestConfig = {
tsconfig: '<rootDir>/tsconfig-esm.spec.json',
},
},
globalSetup: 'jest-preset-angular/global-setup',
globalSetup: 'jest-preset-angular/global-setup.mjs',
moduleNameMapper: {
tslib: 'tslib/tslib.es6.js',
rxjs: '<rootDir>/node_modules/rxjs/dist/bundles/rxjs.umd.js',
Expand Down
2 changes: 1 addition & 1 deletion examples/example-app-v14/jest-esm.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const jestConfig = {
tsconfig: '<rootDir>/tsconfig-esm.spec.json',
},
},
globalSetup: 'jest-preset-angular/global-setup',
globalSetup: 'jest-preset-angular/global-setup.mjs',
moduleNameMapper: {
tslib: 'tslib/tslib.es6.js',
rxjs: '<rootDir>/node_modules/rxjs/dist/bundles/rxjs.umd.js',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const jestConfig = {
tsconfig: '<rootDir>/tsconfig-esm.spec.json',
},
},
globalSetup: 'jest-preset-angular/global-setup',
globalSetup: 'jest-preset-angular/global-setup.mjs',
moduleNameMapper: {
tslib: 'tslib/tslib.es6.js',
rxjs: '<rootDir>/../../node_modules/rxjs/dist/bundles/rxjs.umd.js',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const jestConfig = {
tsconfig: '<rootDir>/tsconfig-esm.spec.json',
},
},
globalSetup: 'jest-preset-angular/global-setup',
globalSetup: 'jest-preset-angular/global-setup.mjs',
moduleNameMapper: {
tslib: 'tslib/tslib.es6.js',
rxjs: '<rootDir>/../../node_modules/rxjs/dist/bundles/rxjs.umd.js',
Expand Down
2 changes: 1 addition & 1 deletion global-setup.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('./build/config/global-setup');
module.exports = require('./build/config/global-setup').globalSetup;
3 changes: 3 additions & 0 deletions global-setup.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { globalSetup } from './build/config/global-setup';

export default globalSetup;
21 changes: 14 additions & 7 deletions scripts/test-examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,20 @@ const executeTest = (projectPath) => {
logger.log('installing bundled version of jest-preset-angular');
logger.log();

['build', 'presets', 'global-setup.js', 'jest-preset.js', 'package.json', 'setup-jest.js', 'setup-jest.mjs'].forEach(
(asset) => {
const assetToReplace = join(projectPath, 'node_modules', 'jest-preset-angular', asset);
const assetToCopy = join(rootDir, asset);
copySync(assetToCopy, assetToReplace, {});
}
);
[
'build',
'presets',
'global-setup.js',
'global-setup.mjs',
'jest-preset.js',
'package.json',
'setup-jest.js',
'setup-jest.mjs',
].forEach((asset) => {
const assetToReplace = join(projectPath, 'node_modules', 'jest-preset-angular', asset);
const assetToCopy = join(rootDir, asset);
copySync(assetToCopy, assetToReplace, {});
});

// then we can run the tests
const cmdLine = ['yarn', 'test'];
Expand Down
2 changes: 1 addition & 1 deletion src/config/global-setup.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import globalSetup from './global-setup';
import { globalSetup } from './global-setup';

jest.mock('../utils/ngcc-jest-processor', () => {
return {
Expand Down
4 changes: 1 addition & 3 deletions src/config/global-setup.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { runNgccJestProcessor } from '../utils/ngcc-jest-processor';

const globalSetup = async () => {
export const globalSetup = async () => {
const ngJestConfig = globalThis.ngJest;
const tsconfig = ngJestConfig?.tsconfig;
if (!ngJestConfig?.skipNgcc) {
runNgccJestProcessor(tsconfig);
}
};

export default globalSetup;

0 comments on commit 10a5c32

Please sign in to comment.