Skip to content

Commit

Permalink
fix(testing): prevent renaming root jest preset
Browse files Browse the repository at this point in the history
fixes: #9973
  • Loading branch information
barbados-clemens committed Apr 29, 2022
1 parent c4c6420 commit 0c39a48
Showing 1 changed file with 2 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
formatFiles,
joinPathFragments,
logger,
offsetFromRoot,
ProjectConfiguration,
readJson,
readProjectConfiguration,
Expand All @@ -11,44 +10,13 @@ import {
updateJson,
updateProjectConfiguration,
} from '@nrwl/devkit';
import { jestConfigObject } from '../../utils/config/functions';
import { dirname, extname, join } from 'path';
import {
removePropertyFromJestConfig,
addPropertyToJestConfig,
} from '../../utils/config/update-config';
import { extname } from 'path';
import { JestExecutorOptions } from '../../executors/jest/schema';
import { forEachExecutorOptions } from '@nrwl/workspace/src/utilities/executor-options-utils';

const allowedExt = ['.ts', '.js'];
let isRootPresetUpdated = false;

function updateJestPreset(
tree: Tree,
options: JestExecutorOptions,
projectName: string
) {
const oldConfig = jestConfigObject(tree, options.jestConfig);
if (!oldConfig) {
return;
}
// if using the root preset and the root preset was updated to ts file.
// then update the jest config
if (isRootPresetUpdated && oldConfig?.preset?.endsWith('jest.preset.js')) {
removePropertyFromJestConfig(tree, options.jestConfig, 'preset');
addPropertyToJestConfig(
tree,
options.jestConfig,
'preset',
joinPathFragments(
offsetFromRoot(dirname(options.jestConfig)),
'jest.preset.ts'
),
{ valueAsString: false }
);
}
}

function updateTsConfig(tree: Tree, tsConfigPath: string) {
try {
updateJson(tree, tsConfigPath, (json) => {
Expand All @@ -69,7 +37,7 @@ function addEsLintIgnoreComments(tree: Tree, filePath: string) {
const contents = tree.read(filePath, 'utf-8');
tree.write(
filePath,
`/* eslint-disable @typescript-eslint/naming-convention */
`/* eslint-disable */
${contents}`
);
}
Expand Down Expand Up @@ -150,7 +118,6 @@ export async function updateJestConfigExt(tree: Tree) {
return;
}

updateJestPreset(tree, options, projectName);
addEsLintIgnoreComments(tree, options.jestConfig);

const newJestConfigPath = options.jestConfig.replace('.js', '.ts');
Expand Down

0 comments on commit 0c39a48

Please sign in to comment.