Skip to content

Commit

Permalink
feat(testing): move jest config to .ts
Browse files Browse the repository at this point in the history
move jest config and preset to ts files

ISSUES CLOSED: #8344
  • Loading branch information
barbados-clemens committed Apr 15, 2022
1 parent 4af1179 commit ea496f3
Show file tree
Hide file tree
Showing 13 changed files with 191 additions and 20 deletions.
6 changes: 6 additions & 0 deletions packages/jest/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
"cli": "nx",
"description": "Create a root babel config file if it doesn't exist and using babel-jest in jest.config.js and add @nrwl/web as needed",
"factory": "./src/migrations/update-13-4-4/add-missing-root-babel-config"
},
"update-jest-config-extensions": {
"version": "14.0.0-beta.0",
"cli": "nx",
"description": "Update move jest config files to .ts files.",
"factory": "./src/migrations/update-14-0-0/update-jest-config-ext"
}
},
"packageJsonUpdates": {
Expand Down
8 changes: 4 additions & 4 deletions packages/jest/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ const schemaDefaults = {
} as const;

function createJestConfig(host: Tree) {
if (!host.exists('jest.config.js')) {
if (!host.exists('jest.config.ts')) {
host.write(
'jest.config.js',
'jest.config.ts',
stripIndents`
const { getJestProjects } = require('@nrwl/jest');
Expand All @@ -37,9 +37,9 @@ function createJestConfig(host: Tree) {
);
}

if (!host.exists('jest.preset.js')) {
if (!host.exists('jest.preset.ts')) {
host.write(
'jest.preset.js',
'jest.preset.ts',
`
const nxPreset = require('@nrwl/jest/preset');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
displayName: '<%= project %>',
preset: '<%= offsetFromRoot %>jest.preset.js',
preset: '<%= offsetFromRoot %>jest.preset.ts',
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
globals: {
'ts-jest': {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
displayName: '<%= project %>',
preset: '<%= offsetFromRoot %>jest.preset.js',<% if(setupFile !== 'none') { %>
preset: '<%= offsetFromRoot %>jest.preset.ts',<% if(setupFile !== 'none') { %>
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],<% } %><% if (transformer === 'ts-jest') { %>
globals: {
'ts-jest': {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { addPropertyToJestConfig } from '../../../utils/config/update-config';
import { readProjectConfiguration, Tree } from '@nrwl/devkit';

function isUsingUtilityFunction(host: Tree) {
return host.read('jest.config.js').toString().includes('getJestProjects()');
return host.read('jest.config.ts').toString().includes('getJestProjects()');
}

export function updateJestConfig(host: Tree, options: JestProjectSchema) {
Expand All @@ -13,7 +13,7 @@ export function updateJestConfig(host: Tree, options: JestProjectSchema) {
const project = readProjectConfiguration(host, options.project);
addPropertyToJestConfig(
host,
'jest.config.js',
'jest.config.ts',
'projects',
`<rootDir>/${project.root}`
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function updateWorkspace(tree: Tree, options: JestProjectSchema) {
options: {
jestConfig: joinPathFragments(
normalizePath(projectConfig.root),
'jest.config.js'
'jest.config.ts'
),
passWithNoTests: true,
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Jest Migration (v14.0.0) should rename project jest.config.js to jest.config.ts 1`] = `
"module.exports = {
transform: {
'^.+\\\\\\\\\\\\\\\\.[tj]sx?$': 'babel-jest',
},\\"preset\\": \\"../../../jest.preset.ts\\"
}"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { addProjectConfiguration, Tree } from '@nrwl/devkit';
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import { updateJestConfigExt } from './update-jest-config-ext';

describe('Jest Migration (v14.0.0)', () => {
let tree: Tree;
beforeEach(() => {
tree = createTreeWithEmptyWorkspace();
tree.write(
'jest.config.js',
String.raw`
const { getJestProjects } = require('@nrwl/jest');
module.exports = {
projects: getJestProjects(),
};
`
);

tree.write(
'jest.preset.js',
String.raw`
const nxPreset = require('@nrwl/jest/preset');
module.exports = { ...nxPreset };
`
);
addProjectConfiguration(tree, 'lib-one', {
root: 'libs/lib-one',
sourceRoot: 'libs/lib-one/src',
targets: {
test: {
executor: '@nrwl/jest:jest',
options: {
jestConfig: 'libs/lib-one/jest.config.js',
passWithNoTests: true,
},
},
},
});
tree.write(
'libs/lib-one/jest.config.js',
String.raw`module.exports = {
preset: '../../jest.preset.js',
transform: {
'^.+\\\\.[tj]sx?$': 'babel-jest',
}
}`
);

addProjectConfiguration(tree, 'lib-two', {
root: 'libs/lib-two',
sourceRoot: 'libs/lib-two/src',
targets: {
test: {
executor: '@nrwl/jest:jest',
options: {
jestConfig: 'libs/lib-two/jest.config.ts',
passWithNoTests: true,
},
},
},
});

tree.write(
'libs/lib-two/jest.config.ts',
String.raw`module.exports = {
preset: '../../jest.preset.ts',
transform: {
'^.+\\\\.[tj]sx?$': 'babel-jest',
}
}`
);

addProjectConfiguration(tree, 'lib-three', {
root: 'libs/lib-three',
sourceRoot: 'libs/lib-three/src',
targets: {
test: {
executor: '@nrwl/jest:jest',
options: {
jestConfig: 'libs/lib-three/jest.config.ts',
passWithNoTests: true,
},
},
},
});
});

it('should rename project jest.config.js to jest.config.ts', async () => {
await updateJestConfigExt(tree);
expect(tree.exists('libs/lib-one/jest.config.ts')).toBeTruthy();
expect(tree.read('libs/lib-one/jest.config.ts', 'utf-8')).toMatchSnapshot();
});

it('should rename root jest files', async () => {
await updateJestConfigExt(tree);
expect(tree.exists('jest.config.ts')).toBeTruthy();
expect(tree.exists('jest.preset.ts')).toBeTruthy();
});

it('should only rename files that end in .js', async () => {
expect(tree.exists('libs/lib-two/jest.config.ts')).toBeTruthy();
await updateJestConfigExt(tree);
expect(tree.exists('libs/lib-two/jest.config.ts')).toBeTruthy();
});

it('should not throw error if file does not exit', async () => {
await updateJestConfigExt(tree);
expect(tree.exists('libs/lib-three/jest.config.ts')).toBeFalsy();
expect(tree.exists('libs/lib-three/jest.config.js')).toBeFalsy();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { formatFiles, offsetFromRoot, Tree } from '@nrwl/devkit';
import { join } from 'path';
import {
removePropertyFromJestConfig,
addPropertyToJestConfig,
} from '../../utils/config/update-config';
import { JestExecutorOptions } from '../../executors/jest/schema';
import { forEachExecutorOptions } from '@nrwl/workspace/src/utilities/executor-options-utils';

export async function updateJestConfigExt(tree: Tree) {
if (tree.exists('jest.config.js')) {
tree.rename('jest.config.js', 'jest.config.ts');
}

if (tree.exists('jest.preset.js')) {
tree.rename('jest.preset.js', 'jest.preset.ts');
}

forEachExecutorOptions<JestExecutorOptions>(
tree,
'@nrwl/jest:jest',
(options, projectName) => {
if (
!tree.exists(options.jestConfig) ||
!options.jestConfig.endsWith('.js')
) {
return;
}
removePropertyFromJestConfig(tree, options.jestConfig, 'preset');
addPropertyToJestConfig(
tree,
options.jestConfig,
'preset',
join(offsetFromRoot(options.jestConfig), 'jest.preset.ts'),
{ valueAsString: false }
);

tree.rename(options.jestConfig, options.jestConfig.replace('.js', '.ts'));
}
);
await formatFiles(tree);
}

export default updateJestConfigExt;
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function updateJestConfig(host: Tree, options: NormalizedSchema) {
return;
}

const configPath = `${options.appProjectRoot}/jest.config.js`;
const configPath = `${options.appProjectRoot}/jest.config.ts`;
const originalContent = host.read(configPath, 'utf-8');
const content = originalContent
.replace(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function updateJestConfig(host: Tree, options: NormalizedSchema) {
return json;
});

const configPath = `${options.appProjectRoot}/jest.config.js`;
const configPath = `${options.appProjectRoot}/jest.config.ts`;
const originalContent = host.read(configPath, 'utf-8');
const content = updateJestConfigContent(originalContent);
host.write(configPath, content);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function updateJestConfig(
) {
const jestConfigPath = path.join(
schema.relativeToRootDestination,
'jest.config.js'
'jest.config.ts'
);

if (tree.exists(jestConfigPath)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
import { join } from 'path';

function isUsingUtilityFunction(host: Tree) {
return host.read('jest.config.js').toString().includes('getJestProjects()');
return host.read('jest.config.ts').toString().includes('getJestProjects()');
}

/**
Expand All @@ -34,16 +34,16 @@ export function updateJestConfig(
const projectToRemove = schema.projectName;

if (
!tree.exists('jest.config.js') ||
!tree.exists(join(projectConfig.root, 'jest.config.js')) ||
!tree.exists('jest.config.ts') ||
!tree.exists(join(projectConfig.root, 'jest.config.ts')) ||
isUsingUtilityFunction(tree)
) {
return;
}

const contents = tree.read('jest.config.js', 'utf-8');
const contents = tree.read('jest.config.ts', 'utf-8');
const sourceFile = createSourceFile(
'jest.config.js',
'jest.config.ts',
contents,
ScriptTarget.Latest
);
Expand All @@ -59,7 +59,7 @@ export function updateJestConfig(

if (!projectsAssignment) {
throw Error(
`Could not remove ${projectToRemove} from projects in /jest.config.js. Please remove ${projectToRemove} from your projects.`
`Could not remove ${projectToRemove} from projects in /jest.config.ts. Please remove ${projectToRemove} from your projects.`
);
}
const projectsArray =
Expand All @@ -73,7 +73,7 @@ export function updateJestConfig(

if (!project) {
console.warn(
`Could not find ${projectToRemove} in projects in /jest.config.js.`
`Could not find ${projectToRemove} in projects in /jest.config.ts.`
);
return;
}
Expand All @@ -86,7 +86,7 @@ export function updateJestConfig(
: project.getStart(sourceFile);

tree.write(
'jest.config.js',
'jest.config.ts',
applyChangesToString(contents, [
{
type: ChangeType.Delete,
Expand Down

0 comments on commit ea496f3

Please sign in to comment.