@nx/jest 17.3.0-beta.3 → 17.3.0-beta.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -4
- package/src/generators/configuration/configuration.js +13 -2
- package/src/generators/configuration/lib/create-jest-config.d.ts +3 -0
- package/src/generators/configuration/lib/create-jest-config.js +102 -0
- package/src/generators/configuration/lib/ensure-dependencies.d.ts +3 -0
- package/src/generators/configuration/lib/ensure-dependencies.js +34 -0
- package/src/generators/configuration/lib/update-vscode-recommended-extensions.d.ts +2 -0
- package/src/generators/configuration/lib/update-vscode-recommended-extensions.js +18 -0
- package/src/generators/init/init.d.ts +3 -3
- package/src/generators/init/init.js +26 -179
- package/src/generators/init/schema.d.ts +1 -8
- package/src/generators/init/schema.json +4 -23
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nx/jest",
|
|
3
|
-
"version": "17.3.0-beta.
|
|
3
|
+
"version": "17.3.0-beta.5",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "The Nx Plugin for Jest contains executors and generators allowing your workspace to use the powerful Jest testing capabilities.",
|
|
6
6
|
"repository": {
|
|
@@ -45,9 +45,9 @@
|
|
|
45
45
|
"minimatch": "3.0.5",
|
|
46
46
|
"resolve.exports": "1.1.0",
|
|
47
47
|
"tslib": "^2.3.0",
|
|
48
|
-
"@nx/devkit": "17.3.0-beta.
|
|
49
|
-
"@nx/js": "17.3.0-beta.
|
|
50
|
-
"@nrwl/jest": "17.3.0-beta.
|
|
48
|
+
"@nx/devkit": "17.3.0-beta.5",
|
|
49
|
+
"@nx/js": "17.3.0-beta.5",
|
|
50
|
+
"@nrwl/jest": "17.3.0-beta.5"
|
|
51
51
|
},
|
|
52
52
|
"publishConfig": {
|
|
53
53
|
"access": "public"
|
|
@@ -4,9 +4,13 @@ exports.configurationGenerator = void 0;
|
|
|
4
4
|
const init_1 = require("../init/init");
|
|
5
5
|
const check_for_test_target_1 = require("./lib/check-for-test-target");
|
|
6
6
|
const create_files_1 = require("./lib/create-files");
|
|
7
|
+
const create_jest_config_1 = require("./lib/create-jest-config");
|
|
8
|
+
const ensure_dependencies_1 = require("./lib/ensure-dependencies");
|
|
7
9
|
const update_tsconfig_1 = require("./lib/update-tsconfig");
|
|
10
|
+
const update_vscode_recommended_extensions_1 = require("./lib/update-vscode-recommended-extensions");
|
|
8
11
|
const update_workspace_1 = require("./lib/update-workspace");
|
|
9
12
|
const devkit_1 = require("@nx/devkit");
|
|
13
|
+
const js_1 = require("@nx/js");
|
|
10
14
|
const schemaDefaults = {
|
|
11
15
|
setupFile: 'none',
|
|
12
16
|
babelJest: false,
|
|
@@ -41,10 +45,17 @@ function normalizeOptions(tree, options) {
|
|
|
41
45
|
}
|
|
42
46
|
async function configurationGenerator(tree, schema) {
|
|
43
47
|
const options = normalizeOptions(tree, schema);
|
|
44
|
-
const
|
|
48
|
+
const tasks = [];
|
|
49
|
+
tasks.push(await (0, js_1.initGenerator)(tree, { ...schema, skipFormat: true }));
|
|
50
|
+
tasks.push(await (0, init_1.jestInitGenerator)(tree, options));
|
|
51
|
+
if (!schema.skipPackageJson) {
|
|
52
|
+
tasks.push((0, ensure_dependencies_1.ensureDependencies)(tree, options));
|
|
53
|
+
}
|
|
54
|
+
await (0, create_jest_config_1.createJestConfig)(tree, options);
|
|
45
55
|
(0, check_for_test_target_1.checkForTestTarget)(tree, options);
|
|
46
56
|
(0, create_files_1.createFiles)(tree, options);
|
|
47
57
|
(0, update_tsconfig_1.updateTsConfig)(tree, options);
|
|
58
|
+
(0, update_vscode_recommended_extensions_1.updateVsCodeRecommendedExtensions)(tree);
|
|
48
59
|
const nxJson = (0, devkit_1.readNxJson)(tree);
|
|
49
60
|
const hasPlugin = nxJson.plugins?.some((p) => typeof p === 'string'
|
|
50
61
|
? p === '@nx/jest/plugin'
|
|
@@ -55,7 +66,7 @@ async function configurationGenerator(tree, schema) {
|
|
|
55
66
|
if (!schema.skipFormat) {
|
|
56
67
|
await (0, devkit_1.formatFiles)(tree);
|
|
57
68
|
}
|
|
58
|
-
return
|
|
69
|
+
return (0, devkit_1.runTasksInSerial)(...tasks);
|
|
59
70
|
}
|
|
60
71
|
exports.configurationGenerator = configurationGenerator;
|
|
61
72
|
exports.default = configurationGenerator;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createJestConfig = void 0;
|
|
4
|
+
const devkit_1 = require("@nx/devkit");
|
|
5
|
+
const project_configuration_utils_1 = require("nx/src/project-graph/utils/project-configuration-utils");
|
|
6
|
+
const find_root_jest_files_1 = require("../../../utils/config/find-root-jest-files");
|
|
7
|
+
async function createJestConfig(tree, options) {
|
|
8
|
+
if (!tree.exists('jest.preset.js')) {
|
|
9
|
+
// preset is always js file.
|
|
10
|
+
tree.write(`jest.preset.js`, `
|
|
11
|
+
const nxPreset = require('@nx/jest/preset').default;
|
|
12
|
+
|
|
13
|
+
module.exports = { ...nxPreset }`);
|
|
14
|
+
}
|
|
15
|
+
if (options.rootProject) {
|
|
16
|
+
// we don't want any config to be made because the `configurationGenerator` will do it.
|
|
17
|
+
// will copy the template config file
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
const rootJestPath = (0, find_root_jest_files_1.findRootJestConfig)(tree);
|
|
21
|
+
if (!rootJestPath) {
|
|
22
|
+
// if there's not root jest config, we will create one and return
|
|
23
|
+
// this can happen when:
|
|
24
|
+
// - root jest config was renamed => in which case there is migration needed
|
|
25
|
+
// - root project didn't have jest setup => again, no migration is needed
|
|
26
|
+
generateGlobalConfig(tree, options.js);
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
if (tree.exists(rootJestPath)) {
|
|
30
|
+
// moving from root project config to monorepo-style config
|
|
31
|
+
const { nodes: projects } = await (0, devkit_1.createProjectGraphAsync)();
|
|
32
|
+
const projectConfigurations = Object.values(projects);
|
|
33
|
+
const rootProject = projectConfigurations.find((projectNode) => projectNode.data?.root === '.');
|
|
34
|
+
// root project might have been removed,
|
|
35
|
+
// if it's missing there's nothing to migrate
|
|
36
|
+
if (rootProject) {
|
|
37
|
+
const jestTarget = Object.entries(rootProject.data?.targets ?? {}).find(([_, t]) => ((t?.executor === '@nx/jest:jest' ||
|
|
38
|
+
t?.executor === '@nrwl/jest:jest') &&
|
|
39
|
+
t?.options?.jestConfig === rootJestPath) ||
|
|
40
|
+
(t?.executor === 'nx:run-commands' && t?.options?.command === 'jest'));
|
|
41
|
+
if (!jestTarget) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const [jestTargetName, jestTargetConfigInGraph] = jestTarget;
|
|
45
|
+
// if root project doesn't have jest target, there's nothing to migrate
|
|
46
|
+
const rootProjectConfig = (0, devkit_1.readProjectConfiguration)(tree, rootProject.name);
|
|
47
|
+
if (rootProjectConfig.targets['test']?.executor === 'nx:run-commands'
|
|
48
|
+
? rootProjectConfig.targets['test']?.command !== 'jest'
|
|
49
|
+
: rootProjectConfig.targets['test']?.options?.jestConfig !==
|
|
50
|
+
rootJestPath) {
|
|
51
|
+
// Jest target has already been updated
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
const jestProjectConfig = `jest.config.${rootProjectConfig.projectType === 'application' ? 'app' : 'lib'}.${options.js ? 'js' : 'ts'}`;
|
|
55
|
+
tree.rename(rootJestPath, jestProjectConfig);
|
|
56
|
+
const nxJson = (0, devkit_1.readNxJson)(tree);
|
|
57
|
+
const targetDefaults = (0, project_configuration_utils_1.readTargetDefaultsForTarget)(jestTargetName, nxJson.targetDefaults, jestTargetConfigInGraph.executor);
|
|
58
|
+
const target = (rootProjectConfig.targets[jestTargetName] ??=
|
|
59
|
+
jestTargetConfigInGraph.executor === 'nx:run-commands'
|
|
60
|
+
? { command: `jest --config ${jestProjectConfig}` }
|
|
61
|
+
: {
|
|
62
|
+
executor: jestTargetConfigInGraph.executor,
|
|
63
|
+
options: {},
|
|
64
|
+
});
|
|
65
|
+
if (target.executor === '@nx/jest:jest') {
|
|
66
|
+
target.options.jestConfig = jestProjectConfig;
|
|
67
|
+
}
|
|
68
|
+
if (targetDefaults?.cache === undefined) {
|
|
69
|
+
target.cache = jestTargetConfigInGraph.cache;
|
|
70
|
+
}
|
|
71
|
+
if (targetDefaults?.inputs === undefined) {
|
|
72
|
+
target.inputs = jestTargetConfigInGraph.inputs;
|
|
73
|
+
}
|
|
74
|
+
if (targetDefaults?.outputs === undefined) {
|
|
75
|
+
target.outputs = jestTargetConfigInGraph.outputs;
|
|
76
|
+
}
|
|
77
|
+
if (targetDefaults?.dependsOn === undefined) {
|
|
78
|
+
target.dependsOn = jestTargetConfigInGraph.dependsOn;
|
|
79
|
+
}
|
|
80
|
+
(0, devkit_1.updateProjectConfiguration)(tree, rootProject.name, rootProjectConfig);
|
|
81
|
+
// generate new global config as it was move to project config or is missing
|
|
82
|
+
generateGlobalConfig(tree, options.js);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
exports.createJestConfig = createJestConfig;
|
|
87
|
+
function generateGlobalConfig(tree, isJS) {
|
|
88
|
+
const contents = isJS
|
|
89
|
+
? (0, devkit_1.stripIndents) `
|
|
90
|
+
const { getJestProjects } = require('@nx/jest');
|
|
91
|
+
|
|
92
|
+
module.exports = {
|
|
93
|
+
projects: getJestProjects()
|
|
94
|
+
};`
|
|
95
|
+
: (0, devkit_1.stripIndents) `
|
|
96
|
+
import { getJestProjects } from '@nx/jest';
|
|
97
|
+
|
|
98
|
+
export default {
|
|
99
|
+
projects: getJestProjects()
|
|
100
|
+
};`;
|
|
101
|
+
tree.write(`jest.config.${isJS ? 'js' : 'ts'}`, contents);
|
|
102
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ensureDependencies = void 0;
|
|
4
|
+
const devkit_1 = require("@nx/devkit");
|
|
5
|
+
const versions_1 = require("../../../utils/versions");
|
|
6
|
+
function ensureDependencies(tree, options) {
|
|
7
|
+
const dependencies = {
|
|
8
|
+
tslib: versions_1.tslibVersion,
|
|
9
|
+
};
|
|
10
|
+
const devDeps = {
|
|
11
|
+
// because the default jest-preset uses ts-jest,
|
|
12
|
+
// jest will throw an error if it's not installed
|
|
13
|
+
// even if not using it in overriding transformers
|
|
14
|
+
'ts-jest': versions_1.tsJestVersion,
|
|
15
|
+
};
|
|
16
|
+
if (options.testEnvironment !== 'none') {
|
|
17
|
+
devDeps[`jest-environment-${options.testEnvironment}`] = versions_1.jestVersion;
|
|
18
|
+
}
|
|
19
|
+
if (!options.js) {
|
|
20
|
+
devDeps['ts-node'] = versions_1.tsNodeVersion;
|
|
21
|
+
devDeps['@types/jest'] = versions_1.jestTypesVersion;
|
|
22
|
+
devDeps['@types/node'] = versions_1.typesNodeVersion;
|
|
23
|
+
}
|
|
24
|
+
if (options.compiler === 'babel' || options.babelJest) {
|
|
25
|
+
devDeps['babel-jest'] = versions_1.babelJestVersion;
|
|
26
|
+
// in some cases @nx/js will not already be present i.e. node only projects
|
|
27
|
+
devDeps['@nx/js'] = versions_1.nxVersion;
|
|
28
|
+
}
|
|
29
|
+
else if (options.compiler === 'swc') {
|
|
30
|
+
devDeps['@swc/jest'] = versions_1.swcJestVersion;
|
|
31
|
+
}
|
|
32
|
+
return (0, devkit_1.addDependenciesToPackageJson)(tree, dependencies, devDeps);
|
|
33
|
+
}
|
|
34
|
+
exports.ensureDependencies = ensureDependencies;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.updateVsCodeRecommendedExtensions = void 0;
|
|
4
|
+
const devkit_1 = require("@nx/devkit");
|
|
5
|
+
function updateVsCodeRecommendedExtensions(host) {
|
|
6
|
+
if (!host.exists('.vscode/extensions.json')) {
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
(0, devkit_1.updateJson)(host, '.vscode/extensions.json', (json) => {
|
|
10
|
+
json.recommendations = json.recommendations || [];
|
|
11
|
+
const extension = 'firsttris.vscode-jest-runner';
|
|
12
|
+
if (!json.recommendations.includes(extension)) {
|
|
13
|
+
json.recommendations.push(extension);
|
|
14
|
+
}
|
|
15
|
+
return json;
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
exports.updateVsCodeRecommendedExtensions = updateVsCodeRecommendedExtensions;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { GeneratorCallback, Tree } from '@nx/devkit';
|
|
2
|
-
import { JestInitSchema } from './schema';
|
|
3
|
-
export declare function jestInitGenerator(tree: Tree,
|
|
1
|
+
import { type GeneratorCallback, type Tree } from '@nx/devkit';
|
|
2
|
+
import type { JestInitSchema } from './schema';
|
|
3
|
+
export declare function jestInitGenerator(tree: Tree, options: JestInitSchema): Promise<GeneratorCallback>;
|
|
4
4
|
export default jestInitGenerator;
|
|
@@ -2,32 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.jestInitGenerator = void 0;
|
|
4
4
|
const devkit_1 = require("@nx/devkit");
|
|
5
|
-
const js_1 = require("@nx/js");
|
|
6
|
-
const find_root_jest_files_1 = require("../../utils/config/find-root-jest-files");
|
|
7
5
|
const versions_1 = require("../../utils/versions");
|
|
8
|
-
const project_configuration_utils_1 = require("nx/src/project-graph/utils/project-configuration-utils");
|
|
9
|
-
const schemaDefaults = {
|
|
10
|
-
compiler: 'tsc',
|
|
11
|
-
js: false,
|
|
12
|
-
rootProject: false,
|
|
13
|
-
testEnvironment: 'jsdom',
|
|
14
|
-
};
|
|
15
|
-
function generateGlobalConfig(tree, isJS) {
|
|
16
|
-
const contents = isJS
|
|
17
|
-
? (0, devkit_1.stripIndents) `
|
|
18
|
-
const { getJestProjects } = require('@nx/jest');
|
|
19
|
-
|
|
20
|
-
module.exports = {
|
|
21
|
-
projects: getJestProjects()
|
|
22
|
-
};`
|
|
23
|
-
: (0, devkit_1.stripIndents) `
|
|
24
|
-
import { getJestProjects } from '@nx/jest';
|
|
25
|
-
|
|
26
|
-
export default {
|
|
27
|
-
projects: getJestProjects()
|
|
28
|
-
};`;
|
|
29
|
-
tree.write(`jest.config.${isJS ? 'js' : 'ts'}`, contents);
|
|
30
|
-
}
|
|
31
6
|
function addPlugin(tree) {
|
|
32
7
|
const nxJson = (0, devkit_1.readNxJson)(tree);
|
|
33
8
|
nxJson.plugins ??= [];
|
|
@@ -43,93 +18,6 @@ function addPlugin(tree) {
|
|
|
43
18
|
}
|
|
44
19
|
(0, devkit_1.updateNxJson)(tree, nxJson);
|
|
45
20
|
}
|
|
46
|
-
async function createJestConfig(tree, options) {
|
|
47
|
-
if (!tree.exists('jest.preset.js')) {
|
|
48
|
-
// preset is always js file.
|
|
49
|
-
tree.write(`jest.preset.js`, `
|
|
50
|
-
const nxPreset = require('@nx/jest/preset').default;
|
|
51
|
-
|
|
52
|
-
module.exports = { ...nxPreset }`);
|
|
53
|
-
const shouldAddPlugin = process.env.NX_PCV3 === 'true';
|
|
54
|
-
if (shouldAddPlugin) {
|
|
55
|
-
addPlugin(tree);
|
|
56
|
-
}
|
|
57
|
-
updateProductionFileSet(tree);
|
|
58
|
-
if (!shouldAddPlugin) {
|
|
59
|
-
addJestTargetDefaults(tree, shouldAddPlugin);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
if (options.rootProject) {
|
|
63
|
-
// we don't want any config to be made because the `configurationGenerator` will do it.
|
|
64
|
-
// will copy the template config file
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
const rootJestPath = (0, find_root_jest_files_1.findRootJestConfig)(tree);
|
|
68
|
-
if (!rootJestPath) {
|
|
69
|
-
// if there's not root jest config, we will create one and return
|
|
70
|
-
// this can happen when:
|
|
71
|
-
// - root jest config was renamed => in which case there is migration needed
|
|
72
|
-
// - root project didn't have jest setup => again, no migration is needed
|
|
73
|
-
generateGlobalConfig(tree, options.js);
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
if (tree.exists(rootJestPath)) {
|
|
77
|
-
// moving from root project config to monorepo-style config
|
|
78
|
-
const { nodes: projects } = await (0, devkit_1.createProjectGraphAsync)();
|
|
79
|
-
const projectConfigurations = Object.values(projects);
|
|
80
|
-
const rootProject = projectConfigurations.find((projectNode) => projectNode.data?.root === '.');
|
|
81
|
-
// root project might have been removed,
|
|
82
|
-
// if it's missing there's nothing to migrate
|
|
83
|
-
if (rootProject) {
|
|
84
|
-
const jestTarget = Object.entries(rootProject.data?.targets ?? {}).find(([_, t]) => ((t?.executor === '@nx/jest:jest' ||
|
|
85
|
-
t?.executor === '@nrwl/jest:jest') &&
|
|
86
|
-
t?.options?.jestConfig === rootJestPath) ||
|
|
87
|
-
(t?.executor === 'nx:run-commands' && t?.options?.command === 'jest'));
|
|
88
|
-
if (!jestTarget) {
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
const [jestTargetName, jestTargetConfigInGraph] = jestTarget;
|
|
92
|
-
// if root project doesn't have jest target, there's nothing to migrate
|
|
93
|
-
const rootProjectConfig = (0, devkit_1.readProjectConfiguration)(tree, rootProject.name);
|
|
94
|
-
if (rootProjectConfig.targets['test']?.executor === 'nx:run-commands'
|
|
95
|
-
? rootProjectConfig.targets['test']?.command !== 'jest'
|
|
96
|
-
: rootProjectConfig.targets['test']?.options?.jestConfig !==
|
|
97
|
-
rootJestPath) {
|
|
98
|
-
// Jest target has already been updated
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
const jestProjectConfig = `jest.config.${rootProjectConfig.projectType === 'application' ? 'app' : 'lib'}.${options.js ? 'js' : 'ts'}`;
|
|
102
|
-
tree.rename(rootJestPath, jestProjectConfig);
|
|
103
|
-
const nxJson = (0, devkit_1.readNxJson)(tree);
|
|
104
|
-
const targetDefaults = (0, project_configuration_utils_1.readTargetDefaultsForTarget)(jestTargetName, nxJson.targetDefaults, jestTargetConfigInGraph.executor);
|
|
105
|
-
const target = (rootProjectConfig.targets[jestTargetName] ??=
|
|
106
|
-
jestTargetConfigInGraph.executor === 'nx:run-commands'
|
|
107
|
-
? { command: `jest --config ${jestProjectConfig}` }
|
|
108
|
-
: {
|
|
109
|
-
executor: jestTargetConfigInGraph.executor,
|
|
110
|
-
options: {},
|
|
111
|
-
});
|
|
112
|
-
if (target.executor === '@nx/jest:jest') {
|
|
113
|
-
target.options.jestConfig = jestProjectConfig;
|
|
114
|
-
}
|
|
115
|
-
if (targetDefaults?.cache === undefined) {
|
|
116
|
-
target.cache = jestTargetConfigInGraph.cache;
|
|
117
|
-
}
|
|
118
|
-
if (targetDefaults?.inputs === undefined) {
|
|
119
|
-
target.inputs = jestTargetConfigInGraph.inputs;
|
|
120
|
-
}
|
|
121
|
-
if (targetDefaults?.outputs === undefined) {
|
|
122
|
-
target.outputs = jestTargetConfigInGraph.outputs;
|
|
123
|
-
}
|
|
124
|
-
if (targetDefaults?.dependsOn === undefined) {
|
|
125
|
-
target.dependsOn = jestTargetConfigInGraph.dependsOn;
|
|
126
|
-
}
|
|
127
|
-
(0, devkit_1.updateProjectConfiguration)(tree, rootProject.name, rootProjectConfig);
|
|
128
|
-
// generate new global config as it was move to project config or is missing
|
|
129
|
-
generateGlobalConfig(tree, options.js);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
21
|
function updateProductionFileSet(tree) {
|
|
134
22
|
const nxJson = (0, devkit_1.readNxJson)(tree);
|
|
135
23
|
const productionFileSet = nxJson.namedInputs?.production;
|
|
@@ -150,20 +38,18 @@ function updateProductionFileSet(tree) {
|
|
|
150
38
|
}
|
|
151
39
|
(0, devkit_1.updateNxJson)(tree, nxJson);
|
|
152
40
|
}
|
|
153
|
-
function addJestTargetDefaults(tree
|
|
41
|
+
function addJestTargetDefaults(tree) {
|
|
154
42
|
const nxJson = (0, devkit_1.readNxJson)(tree);
|
|
155
43
|
nxJson.targetDefaults ??= {};
|
|
156
44
|
nxJson.targetDefaults['@nx/jest:jest'] ??= {};
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
];
|
|
166
|
-
}
|
|
45
|
+
const productionFileSet = nxJson.namedInputs?.production;
|
|
46
|
+
nxJson.targetDefaults['@nx/jest:jest'].cache ??= true;
|
|
47
|
+
// Test targets depend on all their project's sources + production sources of dependencies
|
|
48
|
+
nxJson.targetDefaults['@nx/jest:jest'].inputs ??= [
|
|
49
|
+
'default',
|
|
50
|
+
productionFileSet ? '^production' : '^default',
|
|
51
|
+
'{workspaceRoot}/jest.preset.js',
|
|
52
|
+
];
|
|
167
53
|
nxJson.targetDefaults['@nx/jest:jest'].options ??= {
|
|
168
54
|
passWithNoTests: true,
|
|
169
55
|
};
|
|
@@ -175,70 +61,31 @@ function addJestTargetDefaults(tree, hasPlugin) {
|
|
|
175
61
|
};
|
|
176
62
|
(0, devkit_1.updateNxJson)(tree, nxJson);
|
|
177
63
|
}
|
|
178
|
-
function updateDependencies(tree
|
|
179
|
-
|
|
180
|
-
tslib: versions_1.tslibVersion,
|
|
181
|
-
};
|
|
182
|
-
const devDeps = {
|
|
64
|
+
function updateDependencies(tree) {
|
|
65
|
+
return (0, devkit_1.addDependenciesToPackageJson)(tree, {}, {
|
|
183
66
|
'@nx/jest': versions_1.nxVersion,
|
|
184
67
|
jest: versions_1.jestVersion,
|
|
185
|
-
// because the default jest-preset uses ts-jest,
|
|
186
|
-
// jest will throw an error if it's not installed
|
|
187
|
-
// even if not using it in overriding transformers
|
|
188
|
-
'ts-jest': versions_1.tsJestVersion,
|
|
189
|
-
};
|
|
190
|
-
if (options.testEnvironment !== 'none') {
|
|
191
|
-
devDeps[`jest-environment-${options.testEnvironment}`] = versions_1.jestVersion;
|
|
192
|
-
}
|
|
193
|
-
if (!options.js) {
|
|
194
|
-
devDeps['ts-node'] = versions_1.tsNodeVersion;
|
|
195
|
-
devDeps['@types/jest'] = versions_1.jestTypesVersion;
|
|
196
|
-
devDeps['@types/node'] = versions_1.typesNodeVersion;
|
|
197
|
-
}
|
|
198
|
-
if (options.compiler === 'babel' || options.babelJest) {
|
|
199
|
-
devDeps['babel-jest'] = versions_1.babelJestVersion;
|
|
200
|
-
// in some cases @nx/js will not already be present i.e. node only projects
|
|
201
|
-
devDeps['@nx/js'] = versions_1.nxVersion;
|
|
202
|
-
}
|
|
203
|
-
else if (options.compiler === 'swc') {
|
|
204
|
-
devDeps['@swc/jest'] = versions_1.swcJestVersion;
|
|
205
|
-
}
|
|
206
|
-
return (0, devkit_1.addDependenciesToPackageJson)(tree, dependencies, devDeps);
|
|
207
|
-
}
|
|
208
|
-
function updateExtensions(host) {
|
|
209
|
-
if (!host.exists('.vscode/extensions.json')) {
|
|
210
|
-
return;
|
|
211
|
-
}
|
|
212
|
-
(0, devkit_1.updateJson)(host, '.vscode/extensions.json', (json) => {
|
|
213
|
-
json.recommendations = json.recommendations || [];
|
|
214
|
-
const extension = 'firsttris.vscode-jest-runner';
|
|
215
|
-
if (!json.recommendations.includes(extension)) {
|
|
216
|
-
json.recommendations.push(extension);
|
|
217
|
-
}
|
|
218
|
-
return json;
|
|
219
68
|
});
|
|
220
69
|
}
|
|
221
|
-
async function jestInitGenerator(tree,
|
|
222
|
-
|
|
70
|
+
async function jestInitGenerator(tree, options) {
|
|
71
|
+
if (!tree.exists('jest.preset.js')) {
|
|
72
|
+
updateProductionFileSet(tree);
|
|
73
|
+
if (process.env.NX_PCV3 === 'true') {
|
|
74
|
+
addPlugin(tree);
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
addJestTargetDefaults(tree);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
223
80
|
const tasks = [];
|
|
224
|
-
tasks.push(await (0, js_1.initGenerator)(tree, {
|
|
225
|
-
...schema,
|
|
226
|
-
skipFormat: true,
|
|
227
|
-
}));
|
|
228
|
-
await createJestConfig(tree, options);
|
|
229
81
|
if (!options.skipPackageJson) {
|
|
230
|
-
(0, devkit_1.removeDependenciesFromPackageJson)(tree, ['@nx/jest'], []);
|
|
231
|
-
|
|
232
|
-
|
|
82
|
+
tasks.push((0, devkit_1.removeDependenciesFromPackageJson)(tree, ['@nx/jest'], []));
|
|
83
|
+
tasks.push(updateDependencies(tree));
|
|
84
|
+
}
|
|
85
|
+
if (!options.skipFormat) {
|
|
86
|
+
await (0, devkit_1.formatFiles)(tree);
|
|
233
87
|
}
|
|
234
|
-
updateExtensions(tree);
|
|
235
88
|
return (0, devkit_1.runTasksInSerial)(...tasks);
|
|
236
89
|
}
|
|
237
90
|
exports.jestInitGenerator = jestInitGenerator;
|
|
238
|
-
function normalizeOptions(options) {
|
|
239
|
-
return {
|
|
240
|
-
...schemaDefaults,
|
|
241
|
-
...options,
|
|
242
|
-
};
|
|
243
|
-
}
|
|
244
91
|
exports.default = jestInitGenerator;
|
|
@@ -1,11 +1,4 @@
|
|
|
1
1
|
export interface JestInitSchema {
|
|
2
|
-
|
|
3
|
-
js?: boolean;
|
|
2
|
+
skipFormat?: boolean;
|
|
4
3
|
skipPackageJson?: boolean;
|
|
5
|
-
testEnvironment?: 'node' | 'jsdom' | 'none';
|
|
6
|
-
/**
|
|
7
|
-
* @deprecated
|
|
8
|
-
*/
|
|
9
|
-
babelJest?: boolean;
|
|
10
|
-
rootProject?: boolean;
|
|
11
4
|
}
|
|
@@ -6,35 +6,16 @@
|
|
|
6
6
|
"description": "Add Jest Configuration to a workspace.",
|
|
7
7
|
"type": "object",
|
|
8
8
|
"properties": {
|
|
9
|
-
"
|
|
10
|
-
"
|
|
11
|
-
"alias": "babel-jest",
|
|
12
|
-
"description": "Use `babel-jest` instead of `ts-jest`.",
|
|
13
|
-
"default": false
|
|
14
|
-
},
|
|
15
|
-
"skipPackageJson": {
|
|
9
|
+
"skipFormat": {
|
|
10
|
+
"description": "Skip formatting files.",
|
|
16
11
|
"type": "boolean",
|
|
17
12
|
"default": false,
|
|
18
|
-
"description": "Do not add dependencies to `package.json`.",
|
|
19
13
|
"x-priority": "internal"
|
|
20
14
|
},
|
|
21
|
-
"
|
|
22
|
-
"type": "string",
|
|
23
|
-
"enum": ["jsdom", "node", "none"],
|
|
24
|
-
"description": "The test environment for jest. This controls which jest-environment-* package is installed",
|
|
25
|
-
"default": "jsdom",
|
|
26
|
-
"x-priority": "important"
|
|
27
|
-
},
|
|
28
|
-
"js": {
|
|
29
|
-
"type": "boolean",
|
|
30
|
-
"default": false,
|
|
31
|
-
"description": "Use JavaScript instead of TypeScript for config files"
|
|
32
|
-
},
|
|
33
|
-
"rootProject": {
|
|
34
|
-
"description": "initialize Jest for an application at the root of the workspace",
|
|
15
|
+
"skipPackageJson": {
|
|
35
16
|
"type": "boolean",
|
|
36
17
|
"default": false,
|
|
37
|
-
"
|
|
18
|
+
"description": "Do not add dependencies to `package.json`.",
|
|
38
19
|
"x-priority": "internal"
|
|
39
20
|
}
|
|
40
21
|
},
|