@nx/jest 19.0.0-beta.1 → 19.0.0-beta.10
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/migrations.json +0 -64
- package/package.json +4 -4
- package/src/plugins/plugin.js +3 -11
- package/src/migrations/update-15-0-0/add-jest-inputs.d.ts +0 -2
- package/src/migrations/update-15-0-0/add-jest-inputs.js +0 -41
- package/src/migrations/update-15-8-0/update-configs-jest-29.d.ts +0 -3
- package/src/migrations/update-15-8-0/update-configs-jest-29.js +0 -146
- package/src/migrations/update-15-8-0/update-tests-jest-29.d.ts +0 -5
- package/src/migrations/update-15-8-0/update-tests-jest-29.js +0 -60
package/migrations.json
CHANGED
|
@@ -1,23 +1,5 @@
|
|
|
1
1
|
{
|
|
2
2
|
"generators": {
|
|
3
|
-
"add-jest-inputs": {
|
|
4
|
-
"version": "15.0.0-beta.0",
|
|
5
|
-
"cli": "nx",
|
|
6
|
-
"description": "Stop hashing jest spec files and config files for build targets and dependent tasks",
|
|
7
|
-
"factory": "./src/migrations/update-15-0-0/add-jest-inputs"
|
|
8
|
-
},
|
|
9
|
-
"update-configs-jest-29": {
|
|
10
|
-
"version": "15.8.0-beta.0",
|
|
11
|
-
"cli": "nx",
|
|
12
|
-
"description": "Update jest configs to support jest 29 changes (https://jestjs.io/docs/upgrading-to-jest29)",
|
|
13
|
-
"factory": "./src/migrations/update-15-8-0/update-configs-jest-29"
|
|
14
|
-
},
|
|
15
|
-
"update-tests-jest-29": {
|
|
16
|
-
"version": "15.8.0-beta.0",
|
|
17
|
-
"cli": "nx",
|
|
18
|
-
"description": "Update jest test files to support jest 29 changes (https://jestjs.io/docs/upgrading-to-jest29)",
|
|
19
|
-
"factory": "./src/migrations/update-15-8-0/update-tests-jest-29"
|
|
20
|
-
},
|
|
21
3
|
"update-16-0-0-add-nx-packages": {
|
|
22
4
|
"cli": "nx",
|
|
23
5
|
"version": "16.0.0-beta.1",
|
|
@@ -37,52 +19,6 @@
|
|
|
37
19
|
}
|
|
38
20
|
},
|
|
39
21
|
"packageJsonUpdates": {
|
|
40
|
-
"15.0.1-beta.3": {
|
|
41
|
-
"version": "15.0.1-beta.3",
|
|
42
|
-
"packages": {
|
|
43
|
-
"jest-environment-jsdom": {
|
|
44
|
-
"version": "~28.1.1",
|
|
45
|
-
"addToPackageJson": "devDependencies"
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
},
|
|
49
|
-
"15.8.0": {
|
|
50
|
-
"version": "15.8.0-beta.0",
|
|
51
|
-
"packages": {
|
|
52
|
-
"jest": {
|
|
53
|
-
"version": "~29.4.1",
|
|
54
|
-
"alwaysAddToPackageJson": false
|
|
55
|
-
},
|
|
56
|
-
"@types/jest": {
|
|
57
|
-
"version": "~29.4.0",
|
|
58
|
-
"alwaysAddToPackageJson": false
|
|
59
|
-
},
|
|
60
|
-
"expect": {
|
|
61
|
-
"version": "~29.4.1",
|
|
62
|
-
"alwaysAddToPackageJson": false
|
|
63
|
-
},
|
|
64
|
-
"@jest/globals": {
|
|
65
|
-
"version": "~29.4.1",
|
|
66
|
-
"alwaysAddToPackageJson": false
|
|
67
|
-
},
|
|
68
|
-
"jest-jasmine2": {
|
|
69
|
-
"version": "~29.4.1",
|
|
70
|
-
"alwaysAddToPackageJson": false
|
|
71
|
-
},
|
|
72
|
-
"jest-environment-jsdom": {
|
|
73
|
-
"version": "~29.4.1",
|
|
74
|
-
"alwaysAddToPackageJson": false
|
|
75
|
-
},
|
|
76
|
-
"ts-jest": {
|
|
77
|
-
"version": "~29.0.5",
|
|
78
|
-
"alwaysAddToPackageJson": false
|
|
79
|
-
},
|
|
80
|
-
"babel-jest": {
|
|
81
|
-
"version": "~29.4.1",
|
|
82
|
-
"alwaysAddToPackageJson": false
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
},
|
|
86
22
|
"16.0.0": {
|
|
87
23
|
"version": "16.0.0-beta.4",
|
|
88
24
|
"packages": {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nx/jest",
|
|
3
|
-
"version": "19.0.0-beta.
|
|
3
|
+
"version": "19.0.0-beta.10",
|
|
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": {
|
|
@@ -36,8 +36,8 @@
|
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"@jest/reporters": "^29.4.1",
|
|
38
38
|
"@jest/test-result": "^29.4.1",
|
|
39
|
-
"@nx/devkit": "19.0.0-beta.
|
|
40
|
-
"@nx/js": "19.0.0-beta.
|
|
39
|
+
"@nx/devkit": "19.0.0-beta.10",
|
|
40
|
+
"@nx/js": "19.0.0-beta.10",
|
|
41
41
|
"@phenomnomnominal/tsquery": "~5.0.1",
|
|
42
42
|
"chalk": "^4.1.0",
|
|
43
43
|
"identity-obj-proxy": "3.0.0",
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
"resolve.exports": "1.1.0",
|
|
49
49
|
"tslib": "^2.3.0",
|
|
50
50
|
"yargs-parser": "21.1.1",
|
|
51
|
-
"@nrwl/jest": "19.0.0-beta.
|
|
51
|
+
"@nrwl/jest": "19.0.0-beta.10"
|
|
52
52
|
},
|
|
53
53
|
"publishConfig": {
|
|
54
54
|
"access": "public"
|
package/src/plugins/plugin.js
CHANGED
|
@@ -8,6 +8,7 @@ const fs_1 = require("fs");
|
|
|
8
8
|
const jest_config_1 = require("jest-config");
|
|
9
9
|
const cache_directory_1 = require("nx/src/utils/cache-directory");
|
|
10
10
|
const calculate_hash_for_create_nodes_1 = require("@nx/devkit/src/utils/calculate-hash-for-create-nodes");
|
|
11
|
+
const config_utils_1 = require("@nx/devkit/src/utils/config-utils");
|
|
11
12
|
const package_json_workspaces_1 = require("nx/src/plugins/package-json-workspaces");
|
|
12
13
|
const globs_1 = require("nx/src/utils/globs");
|
|
13
14
|
const minimatch_1 = require("minimatch");
|
|
@@ -67,19 +68,10 @@ exports.createNodes = [
|
|
|
67
68
|
};
|
|
68
69
|
},
|
|
69
70
|
];
|
|
70
|
-
const jestValidatePath = (0, path_1.dirname)(require.resolve('jest-validate/package.json', {
|
|
71
|
-
paths: [(0, path_1.dirname)(require.resolve('jest-config'))],
|
|
72
|
-
}));
|
|
73
71
|
async function buildJestTargets(configFilePath, projectRoot, options, context) {
|
|
74
72
|
const absConfigFilePath = (0, path_1.resolve)(context.workspaceRoot, configFilePath);
|
|
75
73
|
if (require.cache[absConfigFilePath]) {
|
|
76
|
-
|
|
77
|
-
// Only delete the cache outside of jest-validate
|
|
78
|
-
// jest-validate has a Symbol which is important for jest config validation which breaks if the require cache is broken
|
|
79
|
-
if ((0, path_1.relative)(jestValidatePath, k).startsWith('../')) {
|
|
80
|
-
delete require.cache[k];
|
|
81
|
-
}
|
|
82
|
-
}
|
|
74
|
+
(0, config_utils_1.clearRequireCache)();
|
|
83
75
|
}
|
|
84
76
|
const config = await (0, jest_config_1.readConfig)({
|
|
85
77
|
_: [],
|
|
@@ -139,7 +131,7 @@ async function buildJestTargets(configFilePath, projectRoot, options, context) {
|
|
|
139
131
|
};
|
|
140
132
|
targetGroup.push(options.ciTargetName);
|
|
141
133
|
for (const testPath of testPaths) {
|
|
142
|
-
const relativePath = (0,
|
|
134
|
+
const relativePath = (0, devkit_1.normalizePath)((0, path_1.relative)((0, path_1.join)(context.workspaceRoot, projectRoot), testPath));
|
|
143
135
|
const targetName = `${options.ciTargetName}--${relativePath}`;
|
|
144
136
|
dependsOn.push(targetName);
|
|
145
137
|
targets[targetName] = {
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const devkit_1 = require("@nx/devkit");
|
|
4
|
-
const executor_options_utils_1 = require("@nx/devkit/src/generators/executor-options-utils");
|
|
5
|
-
async function default_1(tree) {
|
|
6
|
-
const nxJson = (0, devkit_1.readNxJson)(tree);
|
|
7
|
-
const jestTargets = getJestTargetNames(tree);
|
|
8
|
-
const hasProductionFileset = !!nxJson.namedInputs?.production;
|
|
9
|
-
if (jestTargets.size > 0 && hasProductionFileset) {
|
|
10
|
-
const productionFileset = new Set(nxJson.namedInputs.production);
|
|
11
|
-
for (const exclusion of [
|
|
12
|
-
'!{projectRoot}/**/?(*.)+(spec|test).[jt]s?(x)?(.snap)',
|
|
13
|
-
'!{projectRoot}/tsconfig.spec.json',
|
|
14
|
-
'!{projectRoot}/jest.config.[jt]s',
|
|
15
|
-
]) {
|
|
16
|
-
productionFileset.add(exclusion);
|
|
17
|
-
}
|
|
18
|
-
nxJson.namedInputs.production = Array.from(productionFileset);
|
|
19
|
-
}
|
|
20
|
-
for (const targetName of jestTargets) {
|
|
21
|
-
nxJson.targetDefaults ??= {};
|
|
22
|
-
const jestTargetDefaults = (nxJson.targetDefaults[targetName] ??= {});
|
|
23
|
-
jestTargetDefaults.inputs ??= [
|
|
24
|
-
'default',
|
|
25
|
-
hasProductionFileset ? '^production' : '^default',
|
|
26
|
-
...(tree.exists('jest.preset.js')
|
|
27
|
-
? ['{workspaceRoot}/jest.preset.js']
|
|
28
|
-
: []),
|
|
29
|
-
];
|
|
30
|
-
}
|
|
31
|
-
(0, devkit_1.updateNxJson)(tree, nxJson);
|
|
32
|
-
await (0, devkit_1.formatFiles)(tree);
|
|
33
|
-
}
|
|
34
|
-
exports.default = default_1;
|
|
35
|
-
function getJestTargetNames(tree) {
|
|
36
|
-
const jestTargetNames = new Set();
|
|
37
|
-
(0, executor_options_utils_1.forEachExecutorOptions)(tree, '@nrwl/jest:jest', (_, __, target) => {
|
|
38
|
-
jestTargetNames.add(target);
|
|
39
|
-
});
|
|
40
|
-
return jestTargetNames;
|
|
41
|
-
}
|
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.updateConfigsJest29 = void 0;
|
|
4
|
-
const devkit_1 = require("@nx/devkit");
|
|
5
|
-
const executor_options_utils_1 = require("@nx/devkit/src/generators/executor-options-utils");
|
|
6
|
-
const ast_utils_1 = require("../../utils/ast-utils");
|
|
7
|
-
const tsquery_1 = require("@phenomnomnominal/tsquery");
|
|
8
|
-
const ts = require("typescript");
|
|
9
|
-
const find_root_jest_files_1 = require("../../utils/config/find-root-jest-files");
|
|
10
|
-
async function updateConfigsJest29(tree) {
|
|
11
|
-
const rootPreset = (0, find_root_jest_files_1.findRootJestPreset)(tree);
|
|
12
|
-
const targetsWithJest = new Set();
|
|
13
|
-
// have to use graph so the negative configuration targets are expanded
|
|
14
|
-
const graph = await (0, devkit_1.createProjectGraphAsync)();
|
|
15
|
-
(0, executor_options_utils_1.forEachExecutorOptionsInGraph)(graph, '@nrwl/jest:jest', (options, projectName, targetName) => {
|
|
16
|
-
if (options.jestConfig && tree.exists(options.jestConfig)) {
|
|
17
|
-
targetsWithJest.add(targetName);
|
|
18
|
-
// if the default root preset exists or if the project doesn't have a 'preset' configured
|
|
19
|
-
// -> update snapshot config
|
|
20
|
-
if (!rootPreset || !hasPresetConfigured(tree, options.jestConfig)) {
|
|
21
|
-
addSnapshotOptionsToConfig(tree, options.jestConfig, `From within the project directory, run "nx test --update-snapshot"`);
|
|
22
|
-
}
|
|
23
|
-
updateTsJestOptions(tree, options.jestConfig);
|
|
24
|
-
updateNgJestOptions(tree, options.jestConfig);
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
if (rootPreset && tree.exists(rootPreset)) {
|
|
28
|
-
const cmd = `"nx affected --targets=${Array.from(targetsWithJest).join(',')} --update-snapshot"`;
|
|
29
|
-
addSnapshotOptionsToConfig(tree, rootPreset, cmd);
|
|
30
|
-
updateTsJestOptions(tree, rootPreset);
|
|
31
|
-
updateNgJestOptions(tree, rootPreset);
|
|
32
|
-
}
|
|
33
|
-
await (0, devkit_1.formatFiles)(tree);
|
|
34
|
-
devkit_1.logger.info((0, devkit_1.stripIndents) `NX Jest Snapshot format changed in v29.
|
|
35
|
-
By default Nx kept the older style to prevent breaking of existing tests with snapshots.
|
|
36
|
-
It's recommend you update to the latest format.
|
|
37
|
-
You can do this in your project's jest config file.
|
|
38
|
-
Remove the snapshotFormat property and re-run tests with the --update-snapshot flag.
|
|
39
|
-
More info: https://jestjs.io/docs/upgrading-to-jest29#snapshot-format`);
|
|
40
|
-
}
|
|
41
|
-
exports.updateConfigsJest29 = updateConfigsJest29;
|
|
42
|
-
function addSnapshotOptionsToConfig(tree, configPath, updateSnapshotExample) {
|
|
43
|
-
const config = tree.read(configPath, 'utf-8');
|
|
44
|
-
const hasSnapshotOptions = tsquery_1.tsquery.query(config, `${ast_utils_1.TS_QUERY_JEST_CONFIG_PREFIX} > ObjectLiteralExpression PropertyAssignment:has(Identifier[name="snapshotFormat"])`);
|
|
45
|
-
if (hasSnapshotOptions.length > 0) {
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
const updatedConfig = tsquery_1.tsquery.replace(config, `${ast_utils_1.TS_QUERY_JEST_CONFIG_PREFIX} > ObjectLiteralExpression`, (node) => {
|
|
49
|
-
return `{
|
|
50
|
-
${node.properties.map((p) => getNodeWithComments(config, p)).join(',\n')},
|
|
51
|
-
/* TODO: Update to latest Jest snapshotFormat
|
|
52
|
-
* By default Nx has kept the older style of Jest Snapshot formats
|
|
53
|
-
* to prevent breaking of any existing tests with snapshots.
|
|
54
|
-
* It's recommend you update to the latest format.
|
|
55
|
-
* You can do this by removing snapshotFormat property
|
|
56
|
-
* and running tests with --update-snapshot flag.
|
|
57
|
-
* Example: ${updateSnapshotExample}
|
|
58
|
-
* More info: https://jestjs.io/docs/upgrading-to-jest29#snapshot-format
|
|
59
|
-
*/
|
|
60
|
-
snapshotFormat: { escapeString: true, printBasicPrototype: true }
|
|
61
|
-
}`;
|
|
62
|
-
}, { visitAllChildren: false });
|
|
63
|
-
tree.write(configPath, updatedConfig);
|
|
64
|
-
}
|
|
65
|
-
function hasPresetConfigured(tree, configPath) {
|
|
66
|
-
const contents = tree.read(configPath, 'utf-8');
|
|
67
|
-
return (tsquery_1.tsquery.query(contents, `${ast_utils_1.TS_QUERY_JEST_CONFIG_PREFIX} > ObjectLiteralExpression PropertyAssignment:has(Identifier[name="preset"])`)?.length > 0);
|
|
68
|
-
}
|
|
69
|
-
function updateTsJestOptions(tree, configPath) {
|
|
70
|
-
// query for the globals property, if they don't have one then there's nothing to modify.
|
|
71
|
-
const contents = tree.read(configPath, 'utf-8');
|
|
72
|
-
let tsJestGlobalsConfig;
|
|
73
|
-
const noTsJestGlobals = tsquery_1.tsquery.replace(contents, `${ast_utils_1.TS_QUERY_JEST_CONFIG_PREFIX} > ObjectLiteralExpression PropertyAssignment:has(Identifier[name="globals"])`, (node) => {
|
|
74
|
-
if (tsJestGlobalsConfig) {
|
|
75
|
-
devkit_1.logger.warn((0, devkit_1.stripIndents) `Found more than one "globals" object in the jest config, ${configPath}
|
|
76
|
-
Will use the first one`);
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
tsJestGlobalsConfig = getGlobalTsJestConfig(node);
|
|
80
|
-
return getGlobalConfigWithoutTsJest(node);
|
|
81
|
-
});
|
|
82
|
-
if (!tsJestGlobalsConfig) {
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
const updatedTsJestTransformer = tsquery_1.tsquery.replace(noTsJestGlobals, `${ast_utils_1.TS_QUERY_JEST_CONFIG_PREFIX}> ObjectLiteralExpression PropertyAssignment:has(Identifier[name="transform"]) PropertyAssignment > :has(StringLiteral[value="ts-jest"], StringLiteral[value="jest-preset-angular"])`, (node) => {
|
|
86
|
-
return `[${node.getText()}, ${tsJestGlobalsConfig}]`;
|
|
87
|
-
});
|
|
88
|
-
tree.write(configPath, updatedTsJestTransformer);
|
|
89
|
-
}
|
|
90
|
-
function updateNgJestOptions(tree, configPath) {
|
|
91
|
-
const contents = tree.read(configPath, 'utf-8');
|
|
92
|
-
let ngJestTeardownConfig;
|
|
93
|
-
const noTeardownConfig = tsquery_1.tsquery.replace(contents, 'BinaryExpression:has(PropertyAccessExpression:has(Identifier[name=ngJest])) PropertyAssignment:has(Identifier[name=teardown])', (node) => {
|
|
94
|
-
ngJestTeardownConfig = node.initializer.getText();
|
|
95
|
-
return ' ';
|
|
96
|
-
});
|
|
97
|
-
if (!ngJestTeardownConfig) {
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
let maybeUpdatedTestEnvOpts = tsquery_1.tsquery.replace(noTeardownConfig, `${ast_utils_1.TS_QUERY_JEST_CONFIG_PREFIX} > ObjectLiteralExpression PropertyAssignment:has(Identifier[name="testEnvironmentOptions"]) ObjectLiteralExpression`, (node) => {
|
|
101
|
-
return `{
|
|
102
|
-
${node.properties
|
|
103
|
-
.map((p) => getNodeWithComments(noTeardownConfig, p))
|
|
104
|
-
.join(',\n')},
|
|
105
|
-
teardown: ${ngJestTeardownConfig}
|
|
106
|
-
}`;
|
|
107
|
-
});
|
|
108
|
-
if (maybeUpdatedTestEnvOpts !== noTeardownConfig) {
|
|
109
|
-
tree.write(configPath, maybeUpdatedTestEnvOpts);
|
|
110
|
-
return;
|
|
111
|
-
}
|
|
112
|
-
// didn't find existing testEnvironmentOptions, so add the new property
|
|
113
|
-
const updatedConfig = tsquery_1.tsquery.replace(maybeUpdatedTestEnvOpts, `${ast_utils_1.TS_QUERY_JEST_CONFIG_PREFIX} > ObjectLiteralExpression`, (node) => {
|
|
114
|
-
return `{
|
|
115
|
-
${node.properties
|
|
116
|
-
.map((p) => getNodeWithComments(maybeUpdatedTestEnvOpts, p))
|
|
117
|
-
.join(',\n')},
|
|
118
|
-
testEnvironmentOptions: { teardown: ${ngJestTeardownConfig} },
|
|
119
|
-
}`;
|
|
120
|
-
}, { visitAllChildren: false });
|
|
121
|
-
tree.write(configPath, updatedConfig);
|
|
122
|
-
}
|
|
123
|
-
function getGlobalTsJestConfig(node) {
|
|
124
|
-
const globalObject = node.initializer;
|
|
125
|
-
const foundConfig = globalObject.properties.find((p) => ts.isPropertyAssignment(p) && p.name.getText().includes('ts-jest'));
|
|
126
|
-
return foundConfig?.initializer?.getText() || '';
|
|
127
|
-
}
|
|
128
|
-
function getGlobalConfigWithoutTsJest(node) {
|
|
129
|
-
const globalObject = node?.initializer;
|
|
130
|
-
const withoutTsJest = globalObject?.properties?.filter((p) => {
|
|
131
|
-
return !(ts.isPropertyAssignment(p) && p.name.getText().includes('ts-jest'));
|
|
132
|
-
});
|
|
133
|
-
const globalConfigs = withoutTsJest.map((c) => c.getText()).join(',\n');
|
|
134
|
-
return `globals: { ${globalConfigs} }`;
|
|
135
|
-
}
|
|
136
|
-
function getNodeWithComments(fullText, node) {
|
|
137
|
-
const commentRanges = ts.getLeadingCommentRanges(fullText, node.getFullStart());
|
|
138
|
-
if (commentRanges?.length > 0) {
|
|
139
|
-
const withComments = `${commentRanges
|
|
140
|
-
.map((r) => fullText.slice(r.pos, r.end))
|
|
141
|
-
.join('\n')}\n${node.getText()}`;
|
|
142
|
-
return withComments;
|
|
143
|
-
}
|
|
144
|
-
return node.getText();
|
|
145
|
-
}
|
|
146
|
-
exports.default = updateConfigsJest29;
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import { Tree } from '@nx/devkit';
|
|
2
|
-
export declare function updateTestsJest29(tree: Tree): Promise<void>;
|
|
3
|
-
export declare function updateJestMockTypes(tree: Tree, filePath: string): void;
|
|
4
|
-
export declare function updateJestMocked(tree: Tree, filePath: string): void;
|
|
5
|
-
export default updateTestsJest29;
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.updateJestMocked = exports.updateJestMockTypes = exports.updateTestsJest29 = void 0;
|
|
4
|
-
const devkit_1 = require("@nx/devkit");
|
|
5
|
-
const executor_options_utils_1 = require("@nx/devkit/src/generators/executor-options-utils");
|
|
6
|
-
const tsquery_1 = require("@phenomnomnominal/tsquery");
|
|
7
|
-
const ast_utils_1 = require("../../utils/ast-utils");
|
|
8
|
-
async function updateTestsJest29(tree) {
|
|
9
|
-
const graph = await (0, devkit_1.createProjectGraphAsync)();
|
|
10
|
-
(0, executor_options_utils_1.forEachExecutorOptionsInGraph)(graph, '@nrwl/jest:jest', (options, projectName) => {
|
|
11
|
-
const projectConfig = (0, devkit_1.readProjectConfiguration)(tree, projectName);
|
|
12
|
-
(0, devkit_1.visitNotIgnoredFiles)(tree, projectConfig.sourceRoot || projectConfig.root, (file) => {
|
|
13
|
-
if (!ast_utils_1.TEST_FILE_PATTERN.test(file)) {
|
|
14
|
-
return;
|
|
15
|
-
}
|
|
16
|
-
updateJestMockTypes(tree, file);
|
|
17
|
-
updateJestMocked(tree, file);
|
|
18
|
-
});
|
|
19
|
-
});
|
|
20
|
-
await (0, devkit_1.formatFiles)(tree);
|
|
21
|
-
}
|
|
22
|
-
exports.updateTestsJest29 = updateTestsJest29;
|
|
23
|
-
function updateJestMockTypes(tree, filePath) {
|
|
24
|
-
const contents = tree.read(filePath, 'utf-8');
|
|
25
|
-
const updatedContent = tsquery_1.tsquery.replace(contents, ':matches(ImportDeclaration, VariableStatement):has(Identifier[name="MaybeMockedDeep"], Identifier[name="MaybeMocked"]):has(StringLiteral[value="jest-mock"])', (node) => {
|
|
26
|
-
const text = node.getText();
|
|
27
|
-
return (text
|
|
28
|
-
// MaybeMockedDeep and MaybeMocked now are exported as Mocked and MockedShallow
|
|
29
|
-
.replace('MaybeMockedDeep', 'Mocked')
|
|
30
|
-
.replace('MaybeMocked', 'MockedShallow'));
|
|
31
|
-
});
|
|
32
|
-
tree.write(filePath, updatedContent);
|
|
33
|
-
}
|
|
34
|
-
exports.updateJestMockTypes = updateJestMockTypes;
|
|
35
|
-
function updateJestMocked(tree, filePath) {
|
|
36
|
-
const contents = tree.read(filePath, 'utf-8');
|
|
37
|
-
const jestGlobalNodes = tsquery_1.tsquery.query(contents, ':matches(ImportDeclaration, VariableStatement):has(Identifier[name="jest"]):has(StringLiteral[value="@jest/globals"])');
|
|
38
|
-
// this only applies if using jest from @jest/globals
|
|
39
|
-
if (jestGlobalNodes.length === 0) {
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
const updatedJestMockTypes = tsquery_1.tsquery.replace(contents, 'CallExpression:has(Identifier[name="jest"]):has(Identifier[name="mocked"])', (node) => {
|
|
43
|
-
if (node.arguments.length === 2 &&
|
|
44
|
-
node.getText().startsWith('jest.mocked(')) {
|
|
45
|
-
const text = node.getText();
|
|
46
|
-
// jest.mocked(someObject, true); => jest.mocked(someObject);
|
|
47
|
-
if (node.arguments[1].getText() === 'true') {
|
|
48
|
-
return text.replace(/,\s*true/g, '');
|
|
49
|
-
}
|
|
50
|
-
// jest.mocked(someObject, false); => jest.mocked(someObject, {shallow: true});
|
|
51
|
-
// opt into the new behavior unless explicitly opting out
|
|
52
|
-
if (node.arguments[1].getText() === 'false') {
|
|
53
|
-
return text.replace('false', '{shallow: true}');
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
});
|
|
57
|
-
tree.write(filePath, updatedJestMockTypes);
|
|
58
|
-
}
|
|
59
|
-
exports.updateJestMocked = updateJestMocked;
|
|
60
|
-
exports.default = updateTestsJest29;
|