fix(testing): use root preset for snapshot config changes first (#15237)

This commit is contained in:
Caleb Ukle 2023-02-24 12:11:31 -06:00 committed by GitHub
parent 0a8c5db482
commit 8c7a27f3da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 309 additions and 174 deletions

View File

@ -1,6 +1,31 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP // 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 */ "/* eslint-disable */
export default { export default {
displayName: 'my-lib', displayName: 'my-lib',
@ -19,7 +44,7 @@ coverageDirectory: '../../coverage/libs/my-lib',
* It's recommend you update to the latest format. * It's recommend you update to the latest format.
* You can do this by removing snapshotFormat property * You can do this by removing snapshotFormat property
* and running tests with --update-snapshot flag. * 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 * More info: https://jestjs.io/docs/upgrading-to-jest29#snapshot-format
*/ */
snapshotFormat: { escapeString: true, printBasicPrototype: true } 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 = { "module.exports = {
transform: { transform: {
'^.+\\\\\\\\.[tj]sx?$': ['ts-jest', { '^.+\\\\\\\\.[tj]sx?$': ['ts-jest', {
@ -50,7 +75,7 @@ preset: '../../jest.preset.js',
* It's recommend you update to the latest format. * It's recommend you update to the latest format.
* You can do this by removing snapshotFormat property * You can do this by removing snapshotFormat property
* and running tests with --update-snapshot flag. * 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 * More info: https://jestjs.io/docs/upgrading-to-jest29#snapshot-format
*/ */
snapshotFormat: { escapeString: true, printBasicPrototype: true } 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: '<rootDir>/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: '<rootDir>/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`] = ` exports[`Jest Migration - jest 29 update configs should update globalThis.ngJest.teardown to testEnvironmentOptions 1`] = `
"globalThis.ngJest = { "globalThis.ngJest = {
@ -75,16 +138,6 @@ moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'],
displayName: 'jest', displayName: 'jest',
testEnvironment: 'node', testEnvironment: 'node',
preset: '../../jest.preset.js', 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 }, testEnvironmentOptions: { teardown: true },
};" };"
`; `;
@ -97,57 +150,37 @@ globalThis.ngJest = {
} }
module.exports = { module.exports = {
globals: { }, globals: { },
transform: { transform: {
'^.+.(ts|mjs|js|html)$': ['jest-preset-angular', { '^.+.(ts|mjs|js|html)$': ['jest-preset-angular', {
tsconfig: '<rootDir>/tsconfig.spec.json', tsconfig: '<rootDir>/tsconfig.spec.json',
stringifyContentPathRegex: '\\\\.(html|svg)$', stringifyContentPathRegex: '\\\\.(html|svg)$',
}], }],
}, },
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'], moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'],
testEnvironmentOptions: { testEnvironmentOptions: {
blah: 123, blah: 123,
teardown: false teardown: false
}, },
displayName: 'jest', displayName: 'jest',
testEnvironment: 'node', testEnvironment: 'node',
preset: '../../jest.preset.js', 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 }
};" };"
`; `;
exports[`Jest Migration - jest 29 update configs should update jest.config.ts 1`] = ` exports[`Jest Migration - jest 29 update configs should update jest.config.ts 1`] = `
"/* eslint-disable */ "/* eslint-disable */
export default { export default {
displayName: 'my-lib', displayName: 'my-lib',
preset: '../../jest.preset.js', preset: '../../jest.preset.js',
globals: { }, globals: { },
transform: { transform: {
'^.+\\\\\\\\.[tj]sx?$': ['ts-jest', { '^.+\\\\\\\\.[tj]sx?$': ['ts-jest', {
tsconfig: '<rootDir>/tsconfig.spec.json', tsconfig: '<rootDir>/tsconfig.spec.json',
}] }]
}, },
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'], moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '../../coverage/libs/my-lib', 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 }
}; };
" "
`; `;
@ -168,14 +201,90 @@ abc: [1234, true, {abc: 'yes'}] },
*/ */
displayName: 'jest', displayName: 'jest',
testEnvironment: 'node', 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 /* TODO: Update to latest Jest snapshotFormat
* By default Nx has kept the older style of Jest Snapshot formats * By default Nx has kept the older style of Jest Snapshot formats
* to prevent breaking of any existing tests with snapshots. * to prevent breaking of any existing tests with snapshots.
* It's recommend you update to the latest format. * It's recommend you update to the latest format.
* You can do this by removing snapshotFormat property * You can do this by removing snapshotFormat property
* and running tests with --update-snapshot flag. * 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: '<rootDir>/tsconfig.spec.json',
}]
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '../../coverage/libs/my-lib'
};
"
`;
exports[`Jest Migration - jest 29 update configs should update root preset 3`] = `
"module.exports = {
transform: {
'^.+\\\\\\\\.[tj]sx?$': ['ts-jest', {
tsconfig: '<rootDir>/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 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: '<rootDir>/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 affected --targets=test --update-snapshot\\"
* More info: https://jestjs.io/docs/upgrading-to-jest29#snapshot-format * More info: https://jestjs.io/docs/upgrading-to-jest29#snapshot-format
*/ */
snapshotFormat: { escapeString: true, printBasicPrototype: true } snapshotFormat: { escapeString: true, printBasicPrototype: true }
@ -185,45 +294,25 @@ snapshotFormat: { escapeString: true, printBasicPrototype: true }
exports[`Jest Migration - jest 29 update configs should work if not using ts-jest transformer 1`] = ` exports[`Jest Migration - jest 29 update configs should work if not using ts-jest transformer 1`] = `
"export default { "export default {
transform: { transform: {
'^.+\\\\\\\\.[tj]sx?$': 'babel-jest', '^.+\\\\\\\\.[tj]sx?$': 'babel-jest',
}, },
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'], moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html']
displayName: 'jest', displayName: 'jest',
testEnvironment: 'node', testEnvironment: 'node',
preset: '../../jest.preset.js', 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 }
};" };"
`; `;
exports[`Jest Migration - jest 29 update configs should work if not using ts-jest transformer 2`] = ` exports[`Jest Migration - jest 29 update configs should work if not using ts-jest transformer 2`] = `
"module.exports = { "module.exports = {
transform: { transform: {
'^.+\\\\\\\\.[tj]sx?$': 'babel-jest', '^.+\\\\\\\\.[tj]sx?$': 'babel-jest',
}, },
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'], moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html']
displayName: 'jest', displayName: 'jest',
testEnvironment: 'node', testEnvironment: 'node',
preset: '../../jest.preset.js', 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 }
};" };"
`; `;
@ -261,79 +350,49 @@ abc: [1234, true, {abc: 'yes'}] },
exports[`Jest Migration - jest 29 update configs should work with jest-preset-angular 1`] = ` exports[`Jest Migration - jest 29 update configs should work with jest-preset-angular 1`] = `
"export default { "export default {
globals: { }, globals: { },
transform: { transform: {
'^.+.(ts|mjs|js|html)$': ['jest-preset-angular', { '^.+.(ts|mjs|js|html)$': ['jest-preset-angular', {
tsconfig: '<rootDir>/tsconfig.spec.json', tsconfig: '<rootDir>/tsconfig.spec.json',
stringifyContentPathRegex: '\\\\.(html|svg)$', stringifyContentPathRegex: '\\\\.(html|svg)$',
}], }],
}, },
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'], moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html']
displayName: 'jest', displayName: 'jest',
testEnvironment: 'node', testEnvironment: 'node',
preset: '../../jest.preset.js', 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 }
};" };"
`; `;
exports[`Jest Migration - jest 29 update configs should work with jest-preset-angular 2`] = ` exports[`Jest Migration - jest 29 update configs should work with jest-preset-angular 2`] = `
"module.exports = { "module.exports = {
globals: { }, globals: { },
transform: { transform: {
'^.+.(ts|mjs|js|html)$': ['jest-preset-angular', { '^.+.(ts|mjs|js|html)$': ['jest-preset-angular', {
tsconfig: '<rootDir>/tsconfig.spec.json', tsconfig: '<rootDir>/tsconfig.spec.json',
stringifyContentPathRegex: '\\\\.(html|svg)$', stringifyContentPathRegex: '\\\\.(html|svg)$',
}], }],
}, },
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'], moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html']
displayName: 'jest', displayName: 'jest',
testEnvironment: 'node', testEnvironment: 'node',
preset: '../../jest.preset.js', 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 }
};" };"
`; `;
exports[`Jest Migration - jest 29 update configs should work with multiple projects + configs 1`] = ` exports[`Jest Migration - jest 29 update configs should work with multiple projects + configs 1`] = `
"/* eslint-disable */ "/* eslint-disable */
export default { export default {
displayName: 'my-lib', displayName: 'my-lib',
preset: '../../jest.preset.js', preset: '../../jest.preset.js',
globals: { }, globals: { },
transform: { transform: {
'^.+\\\\\\\\.[tj]sx?$': ['ts-jest', { '^.+\\\\\\\\.[tj]sx?$': ['ts-jest', {
tsconfig: '<rootDir>/tsconfig.spec.json', tsconfig: '<rootDir>/tsconfig.spec.json',
}] }]
}, },
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'], moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '../../coverage/libs/my-lib', 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 }
}; };
" "
`; `;
@ -354,17 +413,7 @@ abc: [1234, true, {abc: 'yes'}] },
*/ */
displayName: 'jest', displayName: 'jest',
testEnvironment: 'node', testEnvironment: 'node',
preset: '../../jest.preset.js', 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 }
}; };
" "
`; `;
@ -372,26 +421,16 @@ snapshotFormat: { escapeString: true, printBasicPrototype: true }
exports[`Jest Migration - jest 29 update configs should work with multiple projects + configs 3`] = ` exports[`Jest Migration - jest 29 update configs should work with multiple projects + configs 3`] = `
"/* eslint-disable */ "/* eslint-disable */
export default { export default {
displayName: 'another-lib', displayName: 'another-lib',
preset: '../../jest.preset.js', preset: '../../jest.preset.js',
globals: { }, globals: { },
transform: { transform: {
'^.+\\\\\\\\.[tj]sx?$': ['ts-jest', { '^.+\\\\\\\\.[tj]sx?$': ['ts-jest', {
tsconfig: '<rootDir>/tsconfig.spec.json', tsconfig: '<rootDir>/tsconfig.spec.json',
}] }]
}, },
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'], moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '../../coverage/libs/another-lib', 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 }
}; };
" "
`; `;
@ -412,17 +451,7 @@ abc: [1234, true, {abc: 'yes'}] },
*/ */
displayName: 'jest', displayName: 'jest',
testEnvironment: 'node', testEnvironment: 'node',
preset: '../../jest.preset.js', 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 }
}; };
" "
`; `;

View File

@ -35,6 +35,86 @@ describe('Jest Migration - jest 29 update configs', () => {
expect(actualJestConfigJs).toMatchSnapshot(); 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: '<rootDir>/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 () => { it('should work with multiple projects + configs', async () => {
await setup(tree, 'my-lib'); await setup(tree, 'my-lib');
await setup(tree, 'another-lib', projectGraph); await setup(tree, 'another-lib', projectGraph);

View File

@ -10,8 +10,11 @@ import { tsquery } from '@phenomnomnominal/tsquery';
import * as ts from 'typescript'; import * as ts from 'typescript';
import { JestExecutorOptions } from '../../executors/jest/schema'; import { JestExecutorOptions } from '../../executors/jest/schema';
import { forEachExecutorOptionsInGraph } from '@nrwl/workspace/src/utilities/executor-options-utils'; 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) { export async function updateConfigsJest29(tree: Tree) {
const rootPreset = findRootJestPreset(tree);
const targetsWithJest = new Set<string>();
// have to use graph so the negative configuration targets are expanded // have to use graph so the negative configuration targets are expanded
const graph = await createProjectGraphAsync(); const graph = await createProjectGraphAsync();
forEachExecutorOptionsInGraph<JestExecutorOptions>( forEachExecutorOptionsInGraph<JestExecutorOptions>(
@ -19,18 +22,31 @@ export async function updateConfigsJest29(tree: Tree) {
'@nrwl/jest:jest', '@nrwl/jest:jest',
(options, projectName, targetName) => { (options, projectName, targetName) => {
if (options.jestConfig && tree.exists(options.jestConfig)) { if (options.jestConfig && tree.exists(options.jestConfig)) {
addSnapshotOptionsToConfig( targetsWithJest.add(targetName);
tree, // if the default root preset exists or if the project doesn't have a 'preset' configured
options.jestConfig, // -> update snapshot config
projectName, if (!rootPreset || !hasPresetConfigured(tree, options.jestConfig)) {
targetName addSnapshotOptionsToConfig(
); tree,
options.jestConfig,
`From within the project directory, run "nx test --update-snapshot"`
);
}
updateTsJestOptions(tree, options.jestConfig); updateTsJestOptions(tree, options.jestConfig);
updateNgJestOptions(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); await formatFiles(tree);
logger.info(stripIndents`NX Jest Snapshot format changed in v29. 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. 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( function addSnapshotOptionsToConfig(
tree: Tree, tree: Tree,
configPath: string, configPath: string,
projectName: string, updateSnapshotExample: string
targetName: string
) { ) {
const config = tree.read(configPath, 'utf-8'); const config = tree.read(configPath, 'utf-8');
const hasSnapshotOptions = tsquery.query( 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. * It's recommend you update to the latest format.
* You can do this by removing snapshotFormat property * You can do this by removing snapshotFormat property
* and running tests with --update-snapshot flag. * 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 * More info: https://jestjs.io/docs/upgrading-to-jest29#snapshot-format
*/ */
snapshotFormat: { escapeString: true, printBasicPrototype: true } snapshotFormat: { escapeString: true, printBasicPrototype: true }
@ -78,6 +93,17 @@ snapshotFormat: { escapeString: true, printBasicPrototype: true }
tree.write(configPath, updatedConfig); 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) { function updateTsJestOptions(tree: Tree, configPath: string) {
// query for the globals property, if they don't have one then there's nothing to modify. // query for the globals property, if they don't have one then there's nothing to modify.
const contents = tree.read(configPath, 'utf-8'); const contents = tree.read(configPath, 'utf-8');