From e60708796a9802426fe8b0fbdf701161f214ad94 Mon Sep 17 00:00:00 2001 From: FrozenPandaz <jasonjean1993@gmail.com> Date: Tue, 31 Oct 2023 16:43:55 -0400 Subject: [PATCH] feat(testing): move test config more into nx.json targetDefaults --- .../migrators/projects/app.migrator.spec.ts | 3 +- .../migrators/projects/lib.migrator.spec.ts | 3 +- .../workspace-rules-project.spec.ts | 7 ---- .../__snapshots__/configuration.spec.ts.snap | 7 ---- .../configuration/configuration.spec.ts | 7 ---- .../configuration/lib/update-workspace.ts | 7 ---- .../jest/src/generators/init/init.spec.ts | 15 ++++++-- packages/jest/src/generators/init/init.ts | 34 +++++++++++++++---- .../src/generators/library/library.spec.ts | 7 ---- .../src/generators/library/library.spec.ts | 7 ---- .../create-tree-with-empty-workspace.ts | 3 -- .../src/generators/e2e-project/e2e.spec.ts | 7 ---- .../plugin/src/generators/e2e-project/e2e.ts | 1 - .../src/generators/plugin/plugin.spec.ts | 7 ---- .../src/generators/library/library.spec.ts | 7 ---- .../__snapshots__/configuration.spec.ts.snap | 7 ---- .../move-storybook-tsconfig.spec.ts.snap | 3 -- .../vite/src/generators/init/init.spec.ts | 6 ++-- packages/vite/src/generators/init/init.ts | 5 +-- .../new/generate-workspace-files.spec.ts | 9 ----- .../new/generate-workspace-files.ts | 3 -- 21 files changed, 49 insertions(+), 106 deletions(-) diff --git a/packages/angular/src/generators/ng-add/migrators/projects/app.migrator.spec.ts b/packages/angular/src/generators/ng-add/migrators/projects/app.migrator.spec.ts index 93c0d0deedc41..8e58b23884017 100644 --- a/packages/angular/src/generators/ng-add/migrators/projects/app.migrator.spec.ts +++ b/packages/angular/src/generators/ng-add/migrators/projects/app.migrator.spec.ts @@ -1624,7 +1624,6 @@ describe('app migrator', () => { ).toStrictEqual([ 'build', 'lint', - 'test', 'e2e', 'myCustomTest', 'myCustomLint', @@ -1657,7 +1656,7 @@ describe('app migrator', () => { const { targetDefaults } = readNxJson(tree); expect( Object.keys(targetDefaults).filter((f) => targetDefaults[f].cache) - ).toStrictEqual(['build', 'lint', 'test', 'e2e', 'myCustomTest']); + ).toStrictEqual(['build', 'lint', 'e2e', 'myCustomTest']); }); }); }); diff --git a/packages/angular/src/generators/ng-add/migrators/projects/lib.migrator.spec.ts b/packages/angular/src/generators/ng-add/migrators/projects/lib.migrator.spec.ts index 302520deb51ad..68d1ebf7c952c 100644 --- a/packages/angular/src/generators/ng-add/migrators/projects/lib.migrator.spec.ts +++ b/packages/angular/src/generators/ng-add/migrators/projects/lib.migrator.spec.ts @@ -1280,7 +1280,6 @@ describe('lib migrator', () => { ).toStrictEqual([ 'build', 'lint', - 'test', 'e2e', 'myCustomBuild', 'myCustomTest', @@ -1307,7 +1306,7 @@ describe('lib migrator', () => { const { targetDefaults } = readNxJson(tree); expect( Object.keys(targetDefaults).filter((f) => targetDefaults[f].cache) - ).toStrictEqual(['build', 'lint', 'test', 'e2e', 'myCustomTest']); + ).toStrictEqual(['build', 'lint', 'e2e', 'myCustomTest']); }); }); }); diff --git a/packages/eslint/src/generators/workspace-rules-project/workspace-rules-project.spec.ts b/packages/eslint/src/generators/workspace-rules-project/workspace-rules-project.spec.ts index 9368d0e328910..28ca0af39efc6 100644 --- a/packages/eslint/src/generators/workspace-rules-project/workspace-rules-project.spec.ts +++ b/packages/eslint/src/generators/workspace-rules-project/workspace-rules-project.spec.ts @@ -81,16 +81,9 @@ describe('@nx/eslint:workspace-rules-project', () => { "sourceRoot": "tools/eslint-rules", "targets": { "test": { - "configurations": { - "ci": { - "ci": true, - "codeCoverage": true, - }, - }, "executor": "@nx/jest:jest", "options": { "jestConfig": "tools/eslint-rules/jest.config.ts", - "passWithNoTests": true, }, "outputs": [ "{workspaceRoot}/coverage/{projectRoot}", diff --git a/packages/jest/src/generators/configuration/__snapshots__/configuration.spec.ts.snap b/packages/jest/src/generators/configuration/__snapshots__/configuration.spec.ts.snap index baf985127967c..efae77a353176 100644 --- a/packages/jest/src/generators/configuration/__snapshots__/configuration.spec.ts.snap +++ b/packages/jest/src/generators/configuration/__snapshots__/configuration.spec.ts.snap @@ -114,16 +114,9 @@ export default { exports[`jestProject should use jest.config.js in project config with --js flag 1`] = ` { - "configurations": { - "ci": { - "ci": true, - "codeCoverage": true, - }, - }, "executor": "@nx/jest:jest", "options": { "jestConfig": "libs/lib1/jest.config.js", - "passWithNoTests": true, }, "outputs": [ "{workspaceRoot}/coverage/{projectRoot}", diff --git a/packages/jest/src/generators/configuration/configuration.spec.ts b/packages/jest/src/generators/configuration/configuration.spec.ts index 95f2f048498b5..2af6696f8a8b8 100644 --- a/packages/jest/src/generators/configuration/configuration.spec.ts +++ b/packages/jest/src/generators/configuration/configuration.spec.ts @@ -84,13 +84,6 @@ describe('jestProject', () => { outputs: ['{workspaceRoot}/coverage/{projectRoot}'], options: { jestConfig: 'libs/lib1/jest.config.ts', - passWithNoTests: true, - }, - configurations: { - ci: { - ci: true, - codeCoverage: true, - }, }, }); }); diff --git a/packages/jest/src/generators/configuration/lib/update-workspace.ts b/packages/jest/src/generators/configuration/lib/update-workspace.ts index 5f72beba39321..8e1acc8a8a3da 100644 --- a/packages/jest/src/generators/configuration/lib/update-workspace.ts +++ b/packages/jest/src/generators/configuration/lib/update-workspace.ts @@ -28,13 +28,6 @@ export function updateWorkspace( normalizePath(projectConfig.root), `jest.config.${options.js ? 'js' : 'ts'}` ), - passWithNoTests: true, - }, - configurations: { - ci: { - ci: true, - codeCoverage: true, - }, }, }; diff --git a/packages/jest/src/generators/init/init.spec.ts b/packages/jest/src/generators/init/init.spec.ts index 253b0024ccf71..e2eb9f96e03b2 100644 --- a/packages/jest/src/generators/init/init.spec.ts +++ b/packages/jest/src/generators/init/init.spec.ts @@ -79,17 +79,26 @@ export default { const productionFileSet = readJson<NxJsonConfiguration>(tree, 'nx.json') .namedInputs.production; - const testDefaults = readJson<NxJsonConfiguration>(tree, 'nx.json') - .targetDefaults.test; + const jestDefaults = readJson<NxJsonConfiguration>(tree, 'nx.json') + .targetDefaults['@nx/jest:jest']; expect(productionFileSet).toContain( '!{projectRoot}/**/?(*.)+(spec|test).[jt]s?(x)?(.snap)' ); expect(productionFileSet).toContain('!{projectRoot}/tsconfig.spec.json'); expect(productionFileSet).toContain('!{projectRoot}/jest.config.[jt]s'); expect(productionFileSet).toContain('!{projectRoot}/src/test-setup.[jt]s'); - expect(testDefaults).toEqual({ + expect(jestDefaults).toEqual({ cache: true, inputs: ['default', '^production', '{workspaceRoot}/jest.preset.js'], + options: { + passWithNoTests: true, + }, + configurations: { + ci: { + ci: true, + codeCoverage: true, + }, + }, }); }); diff --git a/packages/jest/src/generators/init/init.ts b/packages/jest/src/generators/init/init.ts index 6ad8d03a6655c..64316f6e92cf8 100644 --- a/packages/jest/src/generators/init/init.ts +++ b/packages/jest/src/generators/init/init.ts @@ -2,6 +2,7 @@ import { addDependenciesToPackageJson, GeneratorCallback, getProjects, + joinPathFragments, readNxJson, removeDependenciesFromPackageJson, runTasksInSerial, @@ -26,6 +27,7 @@ import { typesNodeVersion, } from '../../utils/versions'; import { JestInitSchema } from './schema'; +import { JestExecutorOptions } from '../../executors/jest/schema'; interface NormalizedSchema extends ReturnType<typeof normalizeOptions> {} @@ -64,7 +66,8 @@ function createJestConfig(tree: Tree, options: NormalizedSchema) { module.exports = { ...nxPreset }` ); - addTestInputs(tree); + updateProductionFileSet(tree); + addJestTargetDefaults(tree); } if (options.rootProject) { // we don't want any config to be made because the `configurationGenerator` will do it. @@ -113,7 +116,7 @@ function createJestConfig(tree: Tree, options: NormalizedSchema) { } } -function addTestInputs(tree: Tree) { +function updateProductionFileSet(tree: Tree) { const nxJson = readNxJson(tree); const productionFileSet = nxJson.namedInputs?.production; @@ -135,14 +138,33 @@ function addTestInputs(tree: Tree) { nxJson.namedInputs.production = Array.from(new Set(productionFileSet)); } - // Test targets depend on all their project's sources + production sources of dependencies + updateNxJson(tree, nxJson); +} + +function addJestTargetDefaults(tree: Tree) { + const nxJson = readNxJson(tree); + const productionFileSet = nxJson.namedInputs?.production; + nxJson.targetDefaults ??= {}; - nxJson.targetDefaults.test ??= {}; - nxJson.targetDefaults.test.inputs ??= [ + nxJson.targetDefaults['@nx/jest:jest'] ??= {}; + nxJson.targetDefaults['@nx/jest:jest'].cache ??= true; + + // Test targets depend on all their project's sources + production sources of dependencies + nxJson.targetDefaults['@nx/jest:jest'].inputs ??= [ 'default', productionFileSet ? '^production' : '^default', + '{workspaceRoot}/jest.preset.js', ]; - nxJson.targetDefaults.test.inputs.push('{workspaceRoot}/jest.preset.js'); + + nxJson.targetDefaults['@nx/jest:jest'].options ??= { + passWithNoTests: true, + }; + nxJson.targetDefaults['@nx/jest:jest'].configurations ??= { + ci: { + ci: true, + codeCoverage: true, + }, + }; updateNxJson(tree, nxJson); } diff --git a/packages/nest/src/generators/library/library.spec.ts b/packages/nest/src/generators/library/library.spec.ts index 99cc8ebcb0531..a0e043056f2c5 100644 --- a/packages/nest/src/generators/library/library.spec.ts +++ b/packages/nest/src/generators/library/library.spec.ts @@ -34,13 +34,6 @@ describe('lib', () => { outputs: [`{workspaceRoot}/coverage/{projectRoot}`], options: { jestConfig: `my-lib/jest.config.ts`, - passWithNoTests: true, - }, - configurations: { - ci: { - ci: true, - codeCoverage: true, - }, }, }); }); diff --git a/packages/node/src/generators/library/library.spec.ts b/packages/node/src/generators/library/library.spec.ts index 12966b6ed4449..632676c868549 100644 --- a/packages/node/src/generators/library/library.spec.ts +++ b/packages/node/src/generators/library/library.spec.ts @@ -40,13 +40,6 @@ describe('lib', () => { outputs: ['{workspaceRoot}/coverage/{projectRoot}'], options: { jestConfig: 'my-lib/jest.config.ts', - passWithNoTests: true, - }, - configurations: { - ci: { - ci: true, - codeCoverage: true, - }, }, }); expect( diff --git a/packages/nx/src/generators/testing-utils/create-tree-with-empty-workspace.ts b/packages/nx/src/generators/testing-utils/create-tree-with-empty-workspace.ts index f945aae2548d0..f6344adeff5e6 100644 --- a/packages/nx/src/generators/testing-utils/create-tree-with-empty-workspace.ts +++ b/packages/nx/src/generators/testing-utils/create-tree-with-empty-workspace.ts @@ -43,9 +43,6 @@ function addCommonFiles(tree: Tree, addAppsAndLibsFolders: boolean): Tree { lint: { cache: true, }, - test: { - cache: true, - }, e2e: { cache: true, }, diff --git a/packages/plugin/src/generators/e2e-project/e2e.spec.ts b/packages/plugin/src/generators/e2e-project/e2e.spec.ts index 6e8e0a1ec366c..8f0bcefacef85 100644 --- a/packages/plugin/src/generators/e2e-project/e2e.spec.ts +++ b/packages/plugin/src/generators/e2e-project/e2e.spec.ts @@ -119,19 +119,12 @@ describe('NxPlugin e2e-project Generator', () => { expect(project.targets.e2e).toBeTruthy(); expect(project.targets.e2e).toMatchInlineSnapshot(` { - "configurations": { - "ci": { - "ci": true, - "codeCoverage": true, - }, - }, "dependsOn": [ "^build", ], "executor": "@nx/jest:jest", "options": { "jestConfig": "apps/my-plugin-e2e/jest.config.ts", - "passWithNoTests": true, "runInBand": true, }, "outputs": [ diff --git a/packages/plugin/src/generators/e2e-project/e2e.ts b/packages/plugin/src/generators/e2e-project/e2e.ts index b4a50e7809527..72c5fc2008b7d 100644 --- a/packages/plugin/src/generators/e2e-project/e2e.ts +++ b/packages/plugin/src/generators/e2e-project/e2e.ts @@ -148,7 +148,6 @@ async function addJest(host: Tree, options: NormalizedSchema) { ...testTarget.options, runInBand: true, }, - configurations: testTarget.configurations, }; // remove the jest build target diff --git a/packages/plugin/src/generators/plugin/plugin.spec.ts b/packages/plugin/src/generators/plugin/plugin.spec.ts index 0fa3c8f1e092f..a6d6c00fe3455 100644 --- a/packages/plugin/src/generators/plugin/plugin.spec.ts +++ b/packages/plugin/src/generators/plugin/plugin.spec.ts @@ -83,13 +83,6 @@ describe('NxPlugin Plugin Generator', () => { outputs: ['{workspaceRoot}/coverage/{projectRoot}'], options: { jestConfig: 'libs/my-plugin/jest.config.ts', - passWithNoTests: true, - }, - configurations: { - ci: { - ci: true, - codeCoverage: true, - }, }, }); }); diff --git a/packages/react-native/src/generators/library/library.spec.ts b/packages/react-native/src/generators/library/library.spec.ts index 261809755e6b9..8387f6001172a 100644 --- a/packages/react-native/src/generators/library/library.spec.ts +++ b/packages/react-native/src/generators/library/library.spec.ts @@ -296,16 +296,9 @@ describe('lib', () => { const projectConfiguration = readProjectConfiguration(appTree, 'my-lib'); expect(projectConfiguration.targets.test).toMatchInlineSnapshot(` { - "configurations": { - "ci": { - "ci": true, - "codeCoverage": true, - }, - }, "executor": "@nx/jest:jest", "options": { "jestConfig": "my-lib/jest.config.ts", - "passWithNoTests": true, }, "outputs": [ "{workspaceRoot}/coverage/{projectRoot}", diff --git a/packages/storybook/src/generators/configuration/__snapshots__/configuration.spec.ts.snap b/packages/storybook/src/generators/configuration/__snapshots__/configuration.spec.ts.snap index c13046d0d5984..8174227b5c3e7 100644 --- a/packages/storybook/src/generators/configuration/__snapshots__/configuration.spec.ts.snap +++ b/packages/storybook/src/generators/configuration/__snapshots__/configuration.spec.ts.snap @@ -72,16 +72,9 @@ exports[`@nx/storybook:configuration for Storybook v7 basic functionalities shou }, }, "test": { - "configurations": { - "ci": { - "ci": true, - "codeCoverage": true, - }, - }, "executor": "@nx/jest:jest", "options": { "jestConfig": "test-ui-lib/jest.config.ts", - "passWithNoTests": true, }, "outputs": [ "{workspaceRoot}/coverage/{projectRoot}", diff --git a/packages/storybook/src/migrations/update-16-5-0/__snapshots__/move-storybook-tsconfig.spec.ts.snap b/packages/storybook/src/migrations/update-16-5-0/__snapshots__/move-storybook-tsconfig.spec.ts.snap index 09131788e0d6c..8efb7dbd3f0f0 100644 --- a/packages/storybook/src/migrations/update-16-5-0/__snapshots__/move-storybook-tsconfig.spec.ts.snap +++ b/packages/storybook/src/migrations/update-16-5-0/__snapshots__/move-storybook-tsconfig.spec.ts.snap @@ -121,9 +121,6 @@ exports[`Ignore @nx/react/plugins/storybook in Storybook eslint plugin should up "lint": { "cache": true, }, - "test": { - "cache": true, - }, }, } `; diff --git a/packages/vite/src/generators/init/init.spec.ts b/packages/vite/src/generators/init/init.spec.ts index 32152f5e5201e..0d9ea3359f3f4 100644 --- a/packages/vite/src/generators/init/init.spec.ts +++ b/packages/vite/src/generators/init/init.spec.ts @@ -80,7 +80,9 @@ describe('@nx/vite:init', () => { const productionNamedInputs = readJson(tree, 'nx.json').namedInputs .production; - const testDefaults = readJson(tree, 'nx.json').targetDefaults.test; + const vitestDefaults = readJson(tree, 'nx.json').targetDefaults[ + '@nx/vite:vitest' + ]; expect(productionNamedInputs).toContain( '!{projectRoot}/**/?(*.)+(spec|test).[jt]s?(x)?(.snap)' @@ -88,7 +90,7 @@ describe('@nx/vite:init', () => { expect(productionNamedInputs).toContain( '!{projectRoot}/tsconfig.spec.json' ); - expect(testDefaults).toEqual({ + expect(vitestDefaults).toEqual({ cache: true, inputs: ['default', '^production'], }); diff --git a/packages/vite/src/generators/init/init.ts b/packages/vite/src/generators/init/init.ts index dccdcd1bb020c..999ce5bd6246f 100644 --- a/packages/vite/src/generators/init/init.ts +++ b/packages/vite/src/generators/init/init.ts @@ -93,8 +93,9 @@ export function createVitestConfig(tree: Tree) { } nxJson.targetDefaults ??= {}; - nxJson.targetDefaults.test ??= {}; - nxJson.targetDefaults.test.inputs ??= [ + nxJson.targetDefaults['@nx/vite:vitest'] ??= {}; + nxJson.targetDefaults['@nx/vite:vitest'].cache ??= true; + nxJson.targetDefaults['@nx/vite:vitest'].inputs ??= [ 'default', productionFileSet ? '^production' : '^default', ]; diff --git a/packages/workspace/src/generators/new/generate-workspace-files.spec.ts b/packages/workspace/src/generators/new/generate-workspace-files.spec.ts index a18db7f54b2cc..a1445f03b3201 100644 --- a/packages/workspace/src/generators/new/generate-workspace-files.spec.ts +++ b/packages/workspace/src/generators/new/generate-workspace-files.spec.ts @@ -114,9 +114,6 @@ describe('@nx/workspace:generateWorkspaceFiles', () => { "e2e": { "cache": true, }, - "lint": { - "cache": true, - }, "test": { "cache": true, }, @@ -163,9 +160,6 @@ describe('@nx/workspace:generateWorkspaceFiles', () => { "e2e": { "cache": true, }, - "lint": { - "cache": true, - }, "test": { "cache": true, }, @@ -231,9 +225,6 @@ describe('@nx/workspace:generateWorkspaceFiles', () => { "e2e": { "cache": true, }, - "lint": { - "cache": true, - }, "test": { "cache": true, }, diff --git a/packages/workspace/src/generators/new/generate-workspace-files.ts b/packages/workspace/src/generators/new/generate-workspace-files.ts index 9d6d16168190f..d08ccc16b819a 100644 --- a/packages/workspace/src/generators/new/generate-workspace-files.ts +++ b/packages/workspace/src/generators/new/generate-workspace-files.ts @@ -72,9 +72,6 @@ function createNxJson( cache: true, dependsOn: ['^build'], }, - lint: { - cache: true, - }, test: { cache: true, },