Skip to content

Commit

Permalink
feat(testing): move test config more into nx.json targetDefaults
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz committed Oct 31, 2023
1 parent eed2cc5 commit e607087
Show file tree
Hide file tree
Showing 21 changed files with 49 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1624,7 +1624,6 @@ describe('app migrator', () => {
).toStrictEqual([
'build',
'lint',
'test',
'e2e',
'myCustomTest',
'myCustomLint',
Expand Down Expand Up @@ -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']);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -1280,7 +1280,6 @@ describe('lib migrator', () => {
).toStrictEqual([
'build',
'lint',
'test',
'e2e',
'myCustomBuild',
'myCustomTest',
Expand All @@ -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']);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,6 @@ export function updateWorkspace(
normalizePath(projectConfig.root),
`jest.config.${options.js ? 'js' : 'ts'}`
),
passWithNoTests: true,
},
configurations: {
ci: {
ci: true,
codeCoverage: true,
},
},
};

Expand Down
15 changes: 12 additions & 3 deletions packages/jest/src/generators/init/init.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
});
});

Expand Down
34 changes: 28 additions & 6 deletions packages/jest/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
addDependenciesToPackageJson,
GeneratorCallback,
getProjects,
joinPathFragments,
readNxJson,
removeDependenciesFromPackageJson,
runTasksInSerial,
Expand All @@ -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> {}

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down
7 changes: 0 additions & 7 deletions packages/nest/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
});
});
Expand Down
7 changes: 0 additions & 7 deletions packages/node/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ function addCommonFiles(tree: Tree, addAppsAndLibsFolders: boolean): Tree {
lint: {
cache: true,
},
test: {
cache: true,
},
e2e: {
cache: true,
},
Expand Down
7 changes: 0 additions & 7 deletions packages/plugin/src/generators/e2e-project/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
1 change: 0 additions & 1 deletion packages/plugin/src/generators/e2e-project/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ async function addJest(host: Tree, options: NormalizedSchema) {
...testTarget.options,
runInBand: true,
},
configurations: testTarget.configurations,
};

// remove the jest build target
Expand Down
7 changes: 0 additions & 7 deletions packages/plugin/src/generators/plugin/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
});
});
Expand Down
7 changes: 0 additions & 7 deletions packages/react-native/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,6 @@ exports[`Ignore @nx/react/plugins/storybook in Storybook eslint plugin should up
"lint": {
"cache": true,
},
"test": {
"cache": true,
},
},
}
`;
Expand Down
6 changes: 4 additions & 2 deletions packages/vite/src/generators/init/init.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,17 @@ 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)'
);
expect(productionNamedInputs).toContain(
'!{projectRoot}/tsconfig.spec.json'
);
expect(testDefaults).toEqual({
expect(vitestDefaults).toEqual({
cache: true,
inputs: ['default', '^production'],
});
Expand Down
5 changes: 3 additions & 2 deletions packages/vite/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,6 @@ describe('@nx/workspace:generateWorkspaceFiles', () => {
"e2e": {
"cache": true,
},
"lint": {
"cache": true,
},
"test": {
"cache": true,
},
Expand Down Expand Up @@ -163,9 +160,6 @@ describe('@nx/workspace:generateWorkspaceFiles', () => {
"e2e": {
"cache": true,
},
"lint": {
"cache": true,
},
"test": {
"cache": true,
},
Expand Down Expand Up @@ -231,9 +225,6 @@ describe('@nx/workspace:generateWorkspaceFiles', () => {
"e2e": {
"cache": true,
},
"lint": {
"cache": true,
},
"test": {
"cache": true,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ function createNxJson(
cache: true,
dependsOn: ['^build'],
},
lint: {
cache: true,
},
test: {
cache: true,
},
Expand Down

0 comments on commit e607087

Please sign in to comment.