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 May 4, 2022
1 parent cb81d27 commit fad1c30
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// 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 = {
exports[`Jest Migration (v14.0.0) should NOT update jest.config.ts preset 1`] = `
"/* eslint-disable */
module.exports = {
displayName: 'lib-one',
preset: '../../jest.preset.js',
globals: {
Expand All @@ -18,8 +20,10 @@ exports[`Jest Migration (v14.0.0) should rename project jest.config.js to jest.c
"
`;

exports[`Jest Migration (v14.0.0) should update jest.config.ts preset to use the jest.preset.ts 1`] = `
"module.exports = {
exports[`Jest Migration (v14.0.0) should rename project jest.config.js to jest.config.ts 1`] = `
"/* eslint-disable */
module.exports = {
displayName: 'lib-one',
preset: '../../jest.preset.js',
globals: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ async function libSetUp(tree: Tree, options = setupDefaults) {
const config = tree.read(`libs/${options.libName}/jest.config.js`, 'utf-8');
tree.write(
`libs/${options.libName}/jest.config.js`,
config.replace(
/\/\/ eslint-disable-next-line @typescript-eslint\/naming-convention/g,
''
)
config
.replace(/\/\* eslint-disable \*\//g, '')
.replace(/export default/g, 'module.exports =')
);
updateProjectConfiguration(tree, options.libName, {
...readProjectConfiguration(tree, options.libName),
Expand Down Expand Up @@ -66,8 +65,9 @@ describe('Jest Migration (v14.0.0)', () => {
expect(tree.read('libs/lib-one/jest.config.ts', 'utf-8')).toMatchSnapshot();
});


it('should rename root jest.config.js', async () => {
await libSetUp(tree);

await updateJestConfigExt(tree);
expect(tree.exists('jest.config.ts')).toBeTruthy();
expect(tree.exists('jest.preset.js')).toBeTruthy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import {
updateJson,
updateProjectConfiguration,
} from '@nrwl/devkit';
import { forEachExecutorOptions } from '@nrwl/workspace/src/utilities/executor-options-utils';
import { extname } from 'path';
import { JestExecutorOptions } from '../../executors/jest/schema';
import { forEachExecutorOptions } from '@nrwl/workspace/src/utilities/executor-options-utils';

const allowedExt = ['.ts', '.js'];

Expand Down
14 changes: 7 additions & 7 deletions packages/jest/src/utils/config/functions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as ts from 'typescript';
import {applyChangesToString, ChangeType, Tree} from '@nrwl/devkit';
import {Config} from '@jest/types';
import {createContext, runInContext} from 'vm';
import {dirname, join} from 'path';
import { applyChangesToString, ChangeType, Tree } from '@nrwl/devkit';
import { Config } from '@jest/types';
import { createContext, runInContext } from 'vm';
import { dirname, join } from 'path';

function makeTextToInsert(
value: unknown,
Expand Down Expand Up @@ -93,7 +93,7 @@ export function addOrUpdateProperty(
const text = makeTextToInsert(
value,
arrayLiteral.elements.length !== 0 &&
!arrayLiteral.elements.hasTrailingComma
!arrayLiteral.elements.hasTrailingComma
);
const updatedContents = applyChangesToString(originalContents, [
{
Expand Down Expand Up @@ -150,7 +150,7 @@ export function removeProperty(
if (
properties.length > 0 &&
propertyAssignment.initializer.kind ===
ts.SyntaxKind.ObjectLiteralExpression
ts.SyntaxKind.ObjectLiteralExpression
) {
return removeProperty(
propertyAssignment.initializer as ts.ObjectLiteralExpression,
Expand Down Expand Up @@ -253,7 +253,7 @@ export function jestConfigObject(
): Partial<Config.InitialOptions> & { [index: string]: any } {
const __filename = join(host.root, path);
const contents = host.read(path, 'utf-8');
let module = {exports: {}};
let module = { exports: {} };

// transform the export default syntax to module.exports
// this will work for the default config, but will break if there are any other ts syntax
Expand Down

0 comments on commit fad1c30

Please sign in to comment.