diff --git a/packages/jest/src/migrations/update-15-8-0/__snapshots__/update-configs-jest-29.spec.ts.snap b/packages/jest/src/migrations/update-15-8-0/__snapshots__/update-configs-jest-29.spec.ts.snap index 42c80fd9c667c..903ff8202052b 100644 --- a/packages/jest/src/migrations/update-15-8-0/__snapshots__/update-configs-jest-29.spec.ts.snap +++ b/packages/jest/src/migrations/update-15-8-0/__snapshots__/update-configs-jest-29.spec.ts.snap @@ -1,6 +1,31 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`Jest Migration - jest 29 update configs should be idempotent 1`] = ` +exports[`Jest Migration - jest 29 update configs should NOT update ts-jest with no globals are preset 1`] = ` +"const nxPreset = require('@nrwl/jest/preset').default; +module.exports = { +...nxPreset, +testMatch: ['**/+(*.)+(spec|test).+(ts|js)?(x)'], +transform: { + '^.+\\\\.(ts|js|html)$': 'ts-jest', + }, +resolver: '@nrwl/jest/plugins/resolver', +moduleFileExtensions: ['ts', 'js', 'html'], +coverageReporters: ['html'], +/* TODO: Update to latest Jest snapshotFormat + * By default Nx has kept the older style of Jest Snapshot formats + * to prevent breaking of any existing tests with snapshots. + * It's recommend you update to the latest format. + * You can do this by removing snapshotFormat property + * and running tests with --update-snapshot flag. + * Example: \\"nx affected --targets=test --update-snapshot\\" + * More info: https://jestjs.io/docs/upgrading-to-jest29#snapshot-format + */ +snapshotFormat: { escapeString: true, printBasicPrototype: true } +}; +" +`; + +exports[`Jest Migration - jest 29 update configs should add snapshot config with no root preset 1`] = ` "/* eslint-disable */ export default { displayName: 'my-lib', @@ -19,7 +44,7 @@ coverageDirectory: '../../coverage/libs/my-lib', * It's recommend you update to the latest format. * You can do this by removing snapshotFormat property * and running tests with --update-snapshot flag. - * Example: \\"nx test my-lib --update-snapshot\\" + * Example: From within the project directory, run \\"nx test --update-snapshot\\" * More info: https://jestjs.io/docs/upgrading-to-jest29#snapshot-format */ snapshotFormat: { escapeString: true, printBasicPrototype: true } @@ -27,7 +52,7 @@ snapshotFormat: { escapeString: true, printBasicPrototype: true } " `; -exports[`Jest Migration - jest 29 update configs should be idempotent 2`] = ` +exports[`Jest Migration - jest 29 update configs should add snapshot config with no root preset 2`] = ` "module.exports = { transform: { '^.+\\\\\\\\.[tj]sx?$': ['ts-jest', { @@ -50,7 +75,7 @@ preset: '../../jest.preset.js', * It's recommend you update to the latest format. * You can do this by removing snapshotFormat property * and running tests with --update-snapshot flag. - * Example: \\"nx test my-lib --update-snapshot\\" + * Example: From within the project directory, run \\"nx test --update-snapshot\\" * More info: https://jestjs.io/docs/upgrading-to-jest29#snapshot-format */ snapshotFormat: { escapeString: true, printBasicPrototype: true } @@ -58,6 +83,44 @@ snapshotFormat: { escapeString: true, printBasicPrototype: true } " `; +exports[`Jest Migration - jest 29 update configs should be idempotent 1`] = ` +"/* eslint-disable */ +export default { + displayName: 'my-lib', + preset: '../../jest.preset.js', + globals: { }, + transform: { + '^.+\\\\\\\\.[tj]sx?$': ['ts-jest', { + tsconfig: '/tsconfig.spec.json', + }] + }, + moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'], + coverageDirectory: '../../coverage/libs/my-lib' +}; +" +`; + +exports[`Jest Migration - jest 29 update configs should be idempotent 2`] = ` +"module.exports = { +transform: { + '^.+\\\\\\\\.[tj]sx?$': ['ts-jest', { + tsconfig: '/tsconfig.spec.json' + }] +}, +// I am a comment and shouldn't be removed +moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'], +globals: { something: 'else', +abc: [1234, true, {abc: 'yes'}] }, +/** + * Multi-line comment shouldn't be removed + */ +displayName: 'jest', +testEnvironment: 'node', +preset: '../../jest.preset.js' +}; +" +`; + exports[`Jest Migration - jest 29 update configs should update globalThis.ngJest.teardown to testEnvironmentOptions 1`] = ` "globalThis.ngJest = { @@ -75,16 +138,6 @@ moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'], displayName: 'jest', testEnvironment: 'node', preset: '../../jest.preset.js', -/* TODO: Update to latest Jest snapshotFormat - * By default Nx has kept the older style of Jest Snapshot formats - * to prevent breaking of any existing tests with snapshots. - * It's recommend you update to the latest format. - * You can do this by removing snapshotFormat property - * and running tests with --update-snapshot flag. - * Example: \\"nx test jest-preset-angular --update-snapshot\\" - * More info: https://jestjs.io/docs/upgrading-to-jest29#snapshot-format - */ -snapshotFormat: { escapeString: true, printBasicPrototype: true }, testEnvironmentOptions: { teardown: true }, };" `; @@ -97,57 +150,37 @@ globalThis.ngJest = { } module.exports = { -globals: { }, -transform: { + globals: { }, + transform: { '^.+.(ts|mjs|js|html)$': ['jest-preset-angular', { tsconfig: '/tsconfig.spec.json', stringifyContentPathRegex: '\\\\.(html|svg)$', }], }, -moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'], -testEnvironmentOptions: { + moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'], + testEnvironmentOptions: { blah: 123, teardown: false }, -displayName: 'jest', -testEnvironment: 'node', -preset: '../../jest.preset.js', -/* TODO: Update to latest Jest snapshotFormat - * By default Nx has kept the older style of Jest Snapshot formats - * to prevent breaking of any existing tests with snapshots. - * It's recommend you update to the latest format. - * You can do this by removing snapshotFormat property - * and running tests with --update-snapshot flag. - * Example: \\"nx test jest-preset-angular --update-snapshot\\" - * More info: https://jestjs.io/docs/upgrading-to-jest29#snapshot-format - */ -snapshotFormat: { escapeString: true, printBasicPrototype: true } + displayName: 'jest', + testEnvironment: 'node', + preset: '../../jest.preset.js', };" `; exports[`Jest Migration - jest 29 update configs should update jest.config.ts 1`] = ` "/* eslint-disable */ export default { -displayName: 'my-lib', -preset: '../../jest.preset.js', -globals: { }, -transform: { + displayName: 'my-lib', + preset: '../../jest.preset.js', + globals: { }, + transform: { '^.+\\\\\\\\.[tj]sx?$': ['ts-jest', { tsconfig: '/tsconfig.spec.json', }] }, -moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'], -coverageDirectory: '../../coverage/libs/my-lib', -/* TODO: Update to latest Jest snapshotFormat - * By default Nx has kept the older style of Jest Snapshot formats - * to prevent breaking of any existing tests with snapshots. - * It's recommend you update to the latest format. - * You can do this by removing snapshotFormat property - * and running tests with --update-snapshot flag. - * Example: \\"nx test my-lib --update-snapshot\\" - * More info: https://jestjs.io/docs/upgrading-to-jest29#snapshot-format - */ -snapshotFormat: { escapeString: true, printBasicPrototype: true } + moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'], + coverageDirectory: '../../coverage/libs/my-lib' }; " `; @@ -168,62 +201,118 @@ abc: [1234, true, {abc: 'yes'}] }, */ displayName: 'jest', testEnvironment: 'node', -preset: '../../jest.preset.js', +preset: '../../jest.preset.js' +}; +" +`; + +exports[`Jest Migration - jest 29 update configs should update root preset 1`] = ` +" + const nxPreset = require('@nrwl/jest/preset').default; + + module.exports = { +...nxPreset, /* TODO: Update to latest Jest snapshotFormat * By default Nx has kept the older style of Jest Snapshot formats * to prevent breaking of any existing tests with snapshots. * It's recommend you update to the latest format. * You can do this by removing snapshotFormat property * and running tests with --update-snapshot flag. - * Example: \\"nx test my-lib --update-snapshot\\" + * Example: \\"nx affected --targets=test --update-snapshot\\" * More info: https://jestjs.io/docs/upgrading-to-jest29#snapshot-format */ snapshotFormat: { escapeString: true, printBasicPrototype: true } +}" +`; + +exports[`Jest Migration - jest 29 update configs should update root preset 2`] = ` +"/* eslint-disable */ +export default { + displayName: 'my-lib', + preset: '../../jest.preset.js', + globals: { }, + transform: { + '^.+\\\\\\\\.[tj]sx?$': ['ts-jest', { + tsconfig: '/tsconfig.spec.json', + }] + }, + moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'], + coverageDirectory: '../../coverage/libs/my-lib' }; " `; -exports[`Jest Migration - jest 29 update configs should work if not using ts-jest transformer 1`] = ` -"export default { +exports[`Jest Migration - jest 29 update configs should update root preset 3`] = ` +"module.exports = { transform: { - '^.+\\\\\\\\.[tj]sx?$': 'babel-jest', - }, + '^.+\\\\\\\\.[tj]sx?$': ['ts-jest', { + tsconfig: '/tsconfig.spec.json' + }] +}, +// I am a comment and shouldn't be removed moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'], +globals: { something: 'else', +abc: [1234, true, {abc: 'yes'}] }, +/** + * Multi-line comment shouldn't be removed + */ displayName: 'jest', testEnvironment: 'node', -preset: '../../jest.preset.js', +preset: '../../jest.preset.js' +}; +" +`; + +exports[`Jest Migration - jest 29 update configs should update root preset if ts-jest is preset 1`] = ` +"const nxPreset = require('@nrwl/jest/preset').default; +module.exports = { +...nxPreset, +testMatch: ['**/+(*.)+(spec|test).+(ts|js)?(x)'], +globals: { something: 'else', +abc: [1234, true, {abc: 'yes'}] }, +transform: { + '^.+\\\\.(ts|js|html)$': ['ts-jest', { + tsconfig: '/tsconfig.spec.json' + }], + }, +resolver: '@nrwl/jest/plugins/resolver', +moduleFileExtensions: ['ts', 'js', 'html'], +coverageReporters: ['html'], /* TODO: Update to latest Jest snapshotFormat * By default Nx has kept the older style of Jest Snapshot formats * to prevent breaking of any existing tests with snapshots. * It's recommend you update to the latest format. * You can do this by removing snapshotFormat property * and running tests with --update-snapshot flag. - * Example: \\"nx test no-ts-jest --update-snapshot\\" + * Example: \\"nx affected --targets=test --update-snapshot\\" * More info: https://jestjs.io/docs/upgrading-to-jest29#snapshot-format */ snapshotFormat: { escapeString: true, printBasicPrototype: true } +}; +" +`; + +exports[`Jest Migration - jest 29 update configs should work if not using ts-jest transformer 1`] = ` +"export default { + transform: { + '^.+\\\\\\\\.[tj]sx?$': 'babel-jest', + }, + moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'] + displayName: 'jest', + testEnvironment: 'node', + preset: '../../jest.preset.js', };" `; exports[`Jest Migration - jest 29 update configs should work if not using ts-jest transformer 2`] = ` "module.exports = { -transform: { + transform: { '^.+\\\\\\\\.[tj]sx?$': 'babel-jest', }, -moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'], -displayName: 'jest', -testEnvironment: 'node', -preset: '../../jest.preset.js', -/* TODO: Update to latest Jest snapshotFormat - * By default Nx has kept the older style of Jest Snapshot formats - * to prevent breaking of any existing tests with snapshots. - * It's recommend you update to the latest format. - * You can do this by removing snapshotFormat property - * and running tests with --update-snapshot flag. - * Example: \\"nx test no-ts-jest --update-snapshot\\" - * More info: https://jestjs.io/docs/upgrading-to-jest29#snapshot-format - */ -snapshotFormat: { escapeString: true, printBasicPrototype: true } + moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'] + displayName: 'jest', + testEnvironment: 'node', + preset: '../../jest.preset.js', };" `; @@ -261,79 +350,49 @@ abc: [1234, true, {abc: 'yes'}] }, exports[`Jest Migration - jest 29 update configs should work with jest-preset-angular 1`] = ` "export default { -globals: { }, -transform: { + globals: { }, + transform: { '^.+.(ts|mjs|js|html)$': ['jest-preset-angular', { tsconfig: '/tsconfig.spec.json', stringifyContentPathRegex: '\\\\.(html|svg)$', }], }, -moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'], -displayName: 'jest', -testEnvironment: 'node', -preset: '../../jest.preset.js', -/* TODO: Update to latest Jest snapshotFormat - * By default Nx has kept the older style of Jest Snapshot formats - * to prevent breaking of any existing tests with snapshots. - * It's recommend you update to the latest format. - * You can do this by removing snapshotFormat property - * and running tests with --update-snapshot flag. - * Example: \\"nx test jest-preset-angular --update-snapshot\\" - * More info: https://jestjs.io/docs/upgrading-to-jest29#snapshot-format - */ -snapshotFormat: { escapeString: true, printBasicPrototype: true } + moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'] + displayName: 'jest', + testEnvironment: 'node', + preset: '../../jest.preset.js', };" `; exports[`Jest Migration - jest 29 update configs should work with jest-preset-angular 2`] = ` "module.exports = { -globals: { }, -transform: { + globals: { }, + transform: { '^.+.(ts|mjs|js|html)$': ['jest-preset-angular', { tsconfig: '/tsconfig.spec.json', stringifyContentPathRegex: '\\\\.(html|svg)$', }], }, -moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'], -displayName: 'jest', -testEnvironment: 'node', -preset: '../../jest.preset.js', -/* TODO: Update to latest Jest snapshotFormat - * By default Nx has kept the older style of Jest Snapshot formats - * to prevent breaking of any existing tests with snapshots. - * It's recommend you update to the latest format. - * You can do this by removing snapshotFormat property - * and running tests with --update-snapshot flag. - * Example: \\"nx test jest-preset-angular --update-snapshot\\" - * More info: https://jestjs.io/docs/upgrading-to-jest29#snapshot-format - */ -snapshotFormat: { escapeString: true, printBasicPrototype: true } + moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'] + displayName: 'jest', + testEnvironment: 'node', + preset: '../../jest.preset.js', };" `; exports[`Jest Migration - jest 29 update configs should work with multiple projects + configs 1`] = ` "/* eslint-disable */ export default { -displayName: 'my-lib', -preset: '../../jest.preset.js', -globals: { }, -transform: { + displayName: 'my-lib', + preset: '../../jest.preset.js', + globals: { }, + transform: { '^.+\\\\\\\\.[tj]sx?$': ['ts-jest', { tsconfig: '/tsconfig.spec.json', }] }, -moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'], -coverageDirectory: '../../coverage/libs/my-lib', -/* TODO: Update to latest Jest snapshotFormat - * By default Nx has kept the older style of Jest Snapshot formats - * to prevent breaking of any existing tests with snapshots. - * It's recommend you update to the latest format. - * You can do this by removing snapshotFormat property - * and running tests with --update-snapshot flag. - * Example: \\"nx test my-lib --update-snapshot\\" - * More info: https://jestjs.io/docs/upgrading-to-jest29#snapshot-format - */ -snapshotFormat: { escapeString: true, printBasicPrototype: true } + moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'], + coverageDirectory: '../../coverage/libs/my-lib' }; " `; @@ -354,17 +413,7 @@ abc: [1234, true, {abc: 'yes'}] }, */ displayName: 'jest', testEnvironment: 'node', -preset: '../../jest.preset.js', -/* TODO: Update to latest Jest snapshotFormat - * By default Nx has kept the older style of Jest Snapshot formats - * to prevent breaking of any existing tests with snapshots. - * It's recommend you update to the latest format. - * You can do this by removing snapshotFormat property - * and running tests with --update-snapshot flag. - * Example: \\"nx test my-lib --update-snapshot\\" - * More info: https://jestjs.io/docs/upgrading-to-jest29#snapshot-format - */ -snapshotFormat: { escapeString: true, printBasicPrototype: true } +preset: '../../jest.preset.js' }; " `; @@ -372,26 +421,16 @@ snapshotFormat: { escapeString: true, printBasicPrototype: true } exports[`Jest Migration - jest 29 update configs should work with multiple projects + configs 3`] = ` "/* eslint-disable */ export default { -displayName: 'another-lib', -preset: '../../jest.preset.js', -globals: { }, -transform: { + displayName: 'another-lib', + preset: '../../jest.preset.js', + globals: { }, + transform: { '^.+\\\\\\\\.[tj]sx?$': ['ts-jest', { tsconfig: '/tsconfig.spec.json', }] }, -moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'], -coverageDirectory: '../../coverage/libs/another-lib', -/* TODO: Update to latest Jest snapshotFormat - * By default Nx has kept the older style of Jest Snapshot formats - * to prevent breaking of any existing tests with snapshots. - * It's recommend you update to the latest format. - * You can do this by removing snapshotFormat property - * and running tests with --update-snapshot flag. - * Example: \\"nx test another-lib --update-snapshot\\" - * More info: https://jestjs.io/docs/upgrading-to-jest29#snapshot-format - */ -snapshotFormat: { escapeString: true, printBasicPrototype: true } + moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'], + coverageDirectory: '../../coverage/libs/another-lib' }; " `; @@ -412,17 +451,7 @@ abc: [1234, true, {abc: 'yes'}] }, */ displayName: 'jest', testEnvironment: 'node', -preset: '../../jest.preset.js', -/* TODO: Update to latest Jest snapshotFormat - * By default Nx has kept the older style of Jest Snapshot formats - * to prevent breaking of any existing tests with snapshots. - * It's recommend you update to the latest format. - * You can do this by removing snapshotFormat property - * and running tests with --update-snapshot flag. - * Example: \\"nx test another-lib --update-snapshot\\" - * More info: https://jestjs.io/docs/upgrading-to-jest29#snapshot-format - */ -snapshotFormat: { escapeString: true, printBasicPrototype: true } +preset: '../../jest.preset.js' }; " `; diff --git a/packages/jest/src/migrations/update-15-8-0/update-configs-jest-29.spec.ts b/packages/jest/src/migrations/update-15-8-0/update-configs-jest-29.spec.ts index f006df5eb3a6f..9bea2ef8bce04 100644 --- a/packages/jest/src/migrations/update-15-8-0/update-configs-jest-29.spec.ts +++ b/packages/jest/src/migrations/update-15-8-0/update-configs-jest-29.spec.ts @@ -35,6 +35,86 @@ describe('Jest Migration - jest 29 update configs', () => { expect(actualJestConfigJs).toMatchSnapshot(); }); + it('should update root preset', async () => { + await setup(tree, 'my-lib'); + await updateConfigsJest29(tree); + + const actualPreset = tree.read('jest.preset.js', 'utf-8'); + expect(actualPreset).toMatchSnapshot(); + const actualJestConfigTs = tree.read('libs/my-lib/jest.config.ts', 'utf-8'); + expect(actualJestConfigTs).toMatchSnapshot(); + const actualJestConfigJs = tree.read('libs/my-lib/jest.config.js', 'utf-8'); + expect(actualJestConfigJs).toMatchSnapshot(); + }); + + it('should update root preset if ts-jest is preset', async () => { + await setup(tree, 'my-lib'); + tree.write( + 'jest.preset.js', + `const nxPreset = require('@nrwl/jest/preset').default; +module.exports = { + ...nxPreset, + testMatch: ['**/+(*.)+(spec|test).+(ts|js)?(x)'], + globals: { + 'ts-jest': { + tsconfig: '/tsconfig.spec.json' + }, + something: 'else', + abc: [1234, true, {abc: 'yes'}] + }, + transform: { + '^.+\\.(ts|js|html)$': 'ts-jest', + }, + resolver: '@nrwl/jest/plugins/resolver', + moduleFileExtensions: ['ts', 'js', 'html'], + coverageReporters: ['html'], +}; +` + ); + + await updateConfigsJest29(tree); + + const actualPreset = tree.read('jest.preset.js', 'utf-8'); + expect(actualPreset).toMatchSnapshot(); + }); + + it('should NOT update ts-jest with no globals are preset', async () => { + await setup(tree, 'my-lib'); + tree.write( + 'jest.preset.js', + `const nxPreset = require('@nrwl/jest/preset').default; +module.exports = { + ...nxPreset, + testMatch: ['**/+(*.)+(spec|test).+(ts|js)?(x)'], + transform: { + '^.+\\.(ts|js|html)$': 'ts-jest', + }, + resolver: '@nrwl/jest/plugins/resolver', + moduleFileExtensions: ['ts', 'js', 'html'], + coverageReporters: ['html'], +}; +` + ); + + await updateConfigsJest29(tree); + + const actualPreset = tree.read('jest.preset.js', 'utf-8'); + expect(actualPreset).toMatchSnapshot(); + }); + + it('should add snapshot config with no root preset', async () => { + await setup(tree, 'my-lib'); + + tree.delete('jest.preset.js'); + + await updateConfigsJest29(tree); + + const actualJestConfigTs = tree.read('libs/my-lib/jest.config.ts', 'utf-8'); + expect(actualJestConfigTs).toMatchSnapshot(); + const actualJestConfigJs = tree.read('libs/my-lib/jest.config.js', 'utf-8'); + expect(actualJestConfigJs).toMatchSnapshot(); + }); + it('should work with multiple projects + configs', async () => { await setup(tree, 'my-lib'); await setup(tree, 'another-lib', projectGraph); diff --git a/packages/jest/src/migrations/update-15-8-0/update-configs-jest-29.ts b/packages/jest/src/migrations/update-15-8-0/update-configs-jest-29.ts index 316cd0397a2c6..369750da33bfd 100644 --- a/packages/jest/src/migrations/update-15-8-0/update-configs-jest-29.ts +++ b/packages/jest/src/migrations/update-15-8-0/update-configs-jest-29.ts @@ -10,8 +10,11 @@ import { tsquery } from '@phenomnomnominal/tsquery'; import * as ts from 'typescript'; import { JestExecutorOptions } from '../../executors/jest/schema'; import { forEachExecutorOptionsInGraph } from '@nrwl/workspace/src/utilities/executor-options-utils'; +import { findRootJestPreset } from '../../utils/config/find-root-jest-files'; export async function updateConfigsJest29(tree: Tree) { + const rootPreset = findRootJestPreset(tree); + const targetsWithJest = new Set(); // have to use graph so the negative configuration targets are expanded const graph = await createProjectGraphAsync(); forEachExecutorOptionsInGraph( @@ -19,18 +22,31 @@ export async function updateConfigsJest29(tree: Tree) { '@nrwl/jest:jest', (options, projectName, targetName) => { if (options.jestConfig && tree.exists(options.jestConfig)) { - addSnapshotOptionsToConfig( - tree, - options.jestConfig, - projectName, - targetName - ); + targetsWithJest.add(targetName); + // if the default root preset exists or if the project doesn't have a 'preset' configured + // -> update snapshot config + if (!rootPreset || !hasPresetConfigured(tree, options.jestConfig)) { + addSnapshotOptionsToConfig( + tree, + options.jestConfig, + `From within the project directory, run "nx test --update-snapshot"` + ); + } updateTsJestOptions(tree, options.jestConfig); updateNgJestOptions(tree, options.jestConfig); } } ); + if (rootPreset && tree.exists(rootPreset)) { + const cmd = `"nx affected --targets=${Array.from(targetsWithJest).join( + ',' + )} --update-snapshot"`; + addSnapshotOptionsToConfig(tree, rootPreset, cmd); + updateTsJestOptions(tree, rootPreset); + updateNgJestOptions(tree, rootPreset); + } + await formatFiles(tree); logger.info(stripIndents`NX Jest Snapshot format changed in v29. By default Nx kept the older style to prevent breaking of existing tests with snapshots. @@ -43,8 +59,7 @@ More info: https://jestjs.io/docs/upgrading-to-jest29#snapshot-format`); function addSnapshotOptionsToConfig( tree: Tree, configPath: string, - projectName: string, - targetName: string + updateSnapshotExample: string ) { const config = tree.read(configPath, 'utf-8'); const hasSnapshotOptions = tsquery.query( @@ -66,7 +81,7 @@ ${node.properties.map((p) => getNodeWithComments(config, p)).join(',\n')}, * It's recommend you update to the latest format. * You can do this by removing snapshotFormat property * and running tests with --update-snapshot flag. - * Example: "nx ${targetName} ${projectName} --update-snapshot" + * Example: ${updateSnapshotExample} * More info: https://jestjs.io/docs/upgrading-to-jest29#snapshot-format */ snapshotFormat: { escapeString: true, printBasicPrototype: true } @@ -78,6 +93,17 @@ snapshotFormat: { escapeString: true, printBasicPrototype: true } tree.write(configPath, updatedConfig); } +function hasPresetConfigured(tree: Tree, configPath: string): boolean { + const contents = tree.read(configPath, 'utf-8'); + + return ( + tsquery.query( + contents, + `${TS_QUERY_JEST_CONFIG_PREFIX} > ObjectLiteralExpression PropertyAssignment:has(Identifier[name="preset"])` + )?.length > 0 + ); +} + function updateTsJestOptions(tree: Tree, configPath: string) { // query for the globals property, if they don't have one then there's nothing to modify. const contents = tree.read(configPath, 'utf-8');