fix(testing): resolve jestconfig and add globals.ts-jest if needed for migration (#3336)

* fix(testing): require jest config to resolve it better during migration

* fix(testing): require with appRootPath

* chore(testing): remove uneeded exports and imports
This commit is contained in:
Jonathan Cammisuli 2020-07-14 12:59:38 -04:00 committed by GitHub
parent 9a5650136c
commit 12a407f8cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 68 additions and 53 deletions

View File

@ -2,7 +2,4 @@ export {
addPropertyToJestConfig, addPropertyToJestConfig,
removePropertyFromJestConfig, removePropertyFromJestConfig,
} from './src/utils/config/update-config'; } from './src/utils/config/update-config';
export { export { jestConfigObjectAst } from './src/utils/config/functions';
jestConfigObjectAst,
jestConfigObject,
} from './src/utils/config/functions';

View File

@ -0,0 +1,4 @@
// export so that we can mock this return value
export function getJestObject(path: string) {
return require(path);
}

View File

@ -3,45 +3,60 @@ import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
import { createEmptyWorkspace } from '@nrwl/workspace/testing'; import { createEmptyWorkspace } from '@nrwl/workspace/testing';
import * as path from 'path'; import * as path from 'path';
import { serializeJson } from '@nrwl/workspace'; import { serializeJson } from '@nrwl/workspace';
import { jestConfigObject } from '../../..'; import { jestConfigObject } from '../../utils/config/functions';
import { getJestObject } from './require-jest-config';
jest.mock('./require-jest-config');
const getJestObjectMock = getJestObject as jest.Mock<typeof getJestObject>;
const ngJestObject = {
name: 'test-jest',
preset: '../../jest.config.js',
coverageDirectory: '../../coverage/libs/test-jest',
globals: {
'existing-global': 'test',
},
snapshotSerializers: [
'jest-preset-angular/build/AngularNoNgAttributesSnapshotSerializer.js',
'jest-preset-angular/build/AngularSnapshotSerializer.js',
'jest-preset-angular/build/HTMLCommentSerializer.js',
],
};
const reactJestObject = {
name: 'my-react-app',
preset: '../../jest.config.js',
transform: {
'^(?!.*\\\\.(js|jsx|ts|tsx|css|json)$)': '@nrwl/react/plugins/jest',
'^.+\\\\.[tj]sx?$': [
'babel-jest',
{ cwd: __dirname, configFile: './babel-jest.config.json' },
],
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'],
coverageDirectory: '../../coverage/apps/my-react-app',
};
describe('update 10.0.0', () => { describe('update 10.0.0', () => {
let initialTree: Tree; let initialTree: Tree;
let schematicRunner: SchematicTestRunner; let schematicRunner: SchematicTestRunner;
const jestConfig = String.raw` const jestConfig = String.raw`
module.exports = { module.exports = ${JSON.stringify(ngJestObject)}
name: 'test-jest',
preset: '../../jest.config.js',
coverageDirectory: '../../coverage/libs/test-jest',
globals: {
"existing-global": "test"
},
snapshotSerializers: [
'jest-preset-angular/build/AngularNoNgAttributesSnapshotSerializer.js',
'jest-preset-angular/build/AngularSnapshotSerializer.js',
'jest-preset-angular/build/HTMLCommentSerializer.js'
]
}
`; `;
const jestConfigReact = String.raw` const jestConfigReact = String.raw`
module.exports = { module.exports = ${JSON.stringify(reactJestObject)}
name: 'my-react-app',
preset: '../../jest.config.js',
transform: {
'^(?!.*\\\\.(js|jsx|ts|tsx|css|json)$)': '@nrwl/react/plugins/jest',
'^.+\\\\.[tj]sx?$': [
'babel-jest',
{ cwd: __dirname, configFile: './babel-jest.config.json' }
]
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'],
coverageDirectory: '../../coverage/apps/my-react-app'
}
`; `;
beforeEach(() => { beforeEach(() => {
getJestObjectMock.mockImplementation((path: string): any => {
if (path.includes('apps/products')) {
return ngJestObject;
} else if (path.includes('apps/cart')) {
return reactJestObject;
}
});
initialTree = createEmptyWorkspace(Tree.empty()); initialTree = createEmptyWorkspace(Tree.empty());
initialTree.create('apps/products/jest.config.js', jestConfig); initialTree.create('apps/products/jest.config.js', jestConfig);

View File

@ -11,7 +11,9 @@ import {
serializeJson, serializeJson,
updateWorkspace, updateWorkspace,
} from '@nrwl/workspace'; } from '@nrwl/workspace';
import { addPropertyToJestConfig, jestConfigObject } from '../../..'; import { addPropertyToJestConfig } from '../../utils/config/update-config';
import { getJestObject } from './require-jest-config';
import { appRootPath } from '@nrwl/workspace/src/utils/app-root';
function checkJestPropertyObject(object: unknown): object is object { function checkJestPropertyObject(object: unknown): object is object {
return object !== null && object !== undefined; return object !== null && object !== undefined;
@ -26,10 +28,6 @@ function modifyJestConfig(
tsConfig: string, tsConfig: string,
isAngular: boolean isAngular: boolean
) { ) {
if (setupFile === '') {
return;
}
let globalTsJest: any = { let globalTsJest: any = {
tsConfig, tsConfig,
}; };
@ -46,24 +44,26 @@ function modifyJestConfig(
} }
try { try {
const jestObject = jestConfigObject(host, jestConfig); const jestObject = getJestObject(`${appRootPath}/${jestConfig}`);
// add set up env file if (setupFile !== '') {
// setupFilesAfterEnv // add set up env file
const existingSetupFiles = jestObject.setupFilesAfterEnv; // setupFilesAfterEnv
const existingSetupFiles = jestObject.setupFilesAfterEnv;
let setupFilesAfterEnv: string | string[] = [setupFile]; let setupFilesAfterEnv: string | string[] = [setupFile];
if (Array.isArray(existingSetupFiles)) { if (Array.isArray(existingSetupFiles)) {
setupFilesAfterEnv = setupFile; setupFilesAfterEnv = setupFile;
}
addPropertyToJestConfig(
host,
jestConfig,
'setupFilesAfterEnv',
setupFilesAfterEnv
);
} }
addPropertyToJestConfig(
host,
jestConfig,
'setupFilesAfterEnv',
setupFilesAfterEnv
);
// check if jest config has babel transform // check if jest config has babel transform
const transformProperty = jestObject.transform; const transformProperty = jestObject.transform;

View File

@ -1,8 +1,7 @@
module.exports = { module.exports = {
name: '<%= project %>', name: '<%= project %>',
preset: '<%= offsetFromRoot %>jest.config.js',<% if(setupFile !== 'none') { %> preset: '<%= offsetFromRoot %>jest.config.js',<% if(setupFile !== 'none') { %>
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'], setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],<% } %><% if (transformer === 'ts-jest') { %>
<% } %><% if (transformer === 'ts-jest') { %>
globals: { globals: {
'ts-jest': { 'ts-jest': {
tsConfig: '<rootDir>/tsconfig.spec.json',<%if (setupFile === 'angular') { %> tsConfig: '<rootDir>/tsconfig.spec.json',<%if (setupFile === 'angular') { %>