fix(testing): check if target exists before adding target for jest (#18327)

This commit is contained in:
Caleb Ukle 2023-08-02 08:49:41 -05:00 committed by GitHub
parent e5fd0d5581
commit 697754dfaa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 0 deletions

View File

@ -3,6 +3,7 @@ import {
readJson,
readProjectConfiguration,
Tree,
updateProjectConfiguration,
writeJson,
} from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
@ -60,6 +61,19 @@ describe('jestProject', () => {
expect(tree.read('libs/lib1/jest.config.ts', 'utf-8')).toMatchSnapshot();
});
it('should add target if there are no targets', async () => {
const pc = readProjectConfiguration(tree, 'lib1');
delete pc.targets;
updateProjectConfiguration(tree, 'lib1', pc);
expect(async () => {
await configurationGenerator(tree, {
...defaultOptions,
project: 'lib1',
setupFile: 'angular',
});
}).not.toThrow();
});
it('should alter project configuration', async () => {
await configurationGenerator(tree, {
...defaultOptions,

View File

@ -12,6 +12,10 @@ export function updateWorkspace(
options: NormalizedJestProjectSchema
) {
const projectConfig = readProjectConfiguration(tree, options.project);
if (!projectConfig.targets) {
projectConfig.targets = {};
}
projectConfig.targets.test = {
executor: '@nx/jest:jest',
outputs: [