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:
parent
9a5650136c
commit
12a407f8cb
@ -2,7 +2,4 @@ export {
|
||||
addPropertyToJestConfig,
|
||||
removePropertyFromJestConfig,
|
||||
} from './src/utils/config/update-config';
|
||||
export {
|
||||
jestConfigObjectAst,
|
||||
jestConfigObject,
|
||||
} from './src/utils/config/functions';
|
||||
export { jestConfigObjectAst } from './src/utils/config/functions';
|
||||
|
||||
@ -0,0 +1,4 @@
|
||||
// export so that we can mock this return value
|
||||
export function getJestObject(path: string) {
|
||||
return require(path);
|
||||
}
|
||||
@ -3,45 +3,60 @@ import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
|
||||
import { createEmptyWorkspace } from '@nrwl/workspace/testing';
|
||||
import * as path from 'path';
|
||||
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', () => {
|
||||
let initialTree: Tree;
|
||||
let schematicRunner: SchematicTestRunner;
|
||||
|
||||
const jestConfig = String.raw`
|
||||
module.exports = {
|
||||
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'
|
||||
]
|
||||
}
|
||||
module.exports = ${JSON.stringify(ngJestObject)}
|
||||
`;
|
||||
|
||||
const jestConfigReact = String.raw`
|
||||
module.exports = {
|
||||
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'
|
||||
}
|
||||
module.exports = ${JSON.stringify(reactJestObject)}
|
||||
`;
|
||||
|
||||
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.create('apps/products/jest.config.js', jestConfig);
|
||||
|
||||
@ -11,7 +11,9 @@ import {
|
||||
serializeJson,
|
||||
updateWorkspace,
|
||||
} 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 {
|
||||
return object !== null && object !== undefined;
|
||||
@ -26,10 +28,6 @@ function modifyJestConfig(
|
||||
tsConfig: string,
|
||||
isAngular: boolean
|
||||
) {
|
||||
if (setupFile === '') {
|
||||
return;
|
||||
}
|
||||
|
||||
let globalTsJest: any = {
|
||||
tsConfig,
|
||||
};
|
||||
@ -46,24 +44,26 @@ function modifyJestConfig(
|
||||
}
|
||||
|
||||
try {
|
||||
const jestObject = jestConfigObject(host, jestConfig);
|
||||
const jestObject = getJestObject(`${appRootPath}/${jestConfig}`);
|
||||
|
||||
// add set up env file
|
||||
// setupFilesAfterEnv
|
||||
const existingSetupFiles = jestObject.setupFilesAfterEnv;
|
||||
if (setupFile !== '') {
|
||||
// add set up env file
|
||||
// setupFilesAfterEnv
|
||||
const existingSetupFiles = jestObject.setupFilesAfterEnv;
|
||||
|
||||
let setupFilesAfterEnv: string | string[] = [setupFile];
|
||||
if (Array.isArray(existingSetupFiles)) {
|
||||
setupFilesAfterEnv = setupFile;
|
||||
let setupFilesAfterEnv: string | string[] = [setupFile];
|
||||
if (Array.isArray(existingSetupFiles)) {
|
||||
setupFilesAfterEnv = setupFile;
|
||||
}
|
||||
|
||||
addPropertyToJestConfig(
|
||||
host,
|
||||
jestConfig,
|
||||
'setupFilesAfterEnv',
|
||||
setupFilesAfterEnv
|
||||
);
|
||||
}
|
||||
|
||||
addPropertyToJestConfig(
|
||||
host,
|
||||
jestConfig,
|
||||
'setupFilesAfterEnv',
|
||||
setupFilesAfterEnv
|
||||
);
|
||||
|
||||
// check if jest config has babel transform
|
||||
const transformProperty = jestObject.transform;
|
||||
|
||||
|
||||
@ -1,8 +1,7 @@
|
||||
module.exports = {
|
||||
name: '<%= project %>',
|
||||
preset: '<%= offsetFromRoot %>jest.config.js',<% if(setupFile !== 'none') { %>
|
||||
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
|
||||
<% } %><% if (transformer === 'ts-jest') { %>
|
||||
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],<% } %><% if (transformer === 'ts-jest') { %>
|
||||
globals: {
|
||||
'ts-jest': {
|
||||
tsConfig: '<rootDir>/tsconfig.spec.json',<%if (setupFile === 'angular') { %>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user