@nx/jest 16.8.0-beta.4 → 16.8.0-beta.6

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.
Files changed (26) hide show
  1. package/package.json +5 -5
  2. package/plugins/resolver.js +10 -3
  3. package/src/executors/jest/jest.impl.js +144 -116
  4. package/src/executors/jest/summary.js +4 -4
  5. package/src/generators/configuration/configuration.js +16 -15
  6. package/src/generators/configuration/lib/check-for-test-target.js +1 -2
  7. package/src/generators/configuration/lib/create-files.js +11 -3
  8. package/src/generators/configuration/lib/update-workspace.js +1 -2
  9. package/src/generators/init/init.js +27 -27
  10. package/src/migrations/update-12-1-2/update-jest-preset-angular.js +5 -8
  11. package/src/migrations/update-12-1-2/update-ts-jest.js +5 -9
  12. package/src/migrations/update-12-4-0/add-test-environment-for-node.js +5 -8
  13. package/src/migrations/update-12-4-0/update-jest-preset-angular.js +14 -20
  14. package/src/migrations/update-12-6-0/update-base-jest-config.js +4 -8
  15. package/src/migrations/update-13-1-2/update-tsconfigs-for-tests.js +3 -6
  16. package/src/migrations/update-13-4-4/add-missing-root-babel-config.js +7 -10
  17. package/src/migrations/update-14-0-0/update-jest-config-ext.js +46 -50
  18. package/src/migrations/update-14-6-0/update-configs-jest-28.js +6 -7
  19. package/src/migrations/update-14-6-0/update-tests-jest-28.js +12 -16
  20. package/src/migrations/update-15-0-0/add-jest-inputs.js +27 -32
  21. package/src/migrations/update-15-8-0/update-configs-jest-29.js +28 -34
  22. package/src/migrations/update-15-8-0/update-tests-jest-29.js +11 -14
  23. package/src/migrations/update-16-0-0-add-nx-packages/update-16-0-0-add-nx-packages.js +3 -6
  24. package/src/migrations/update-16-5-0/add-test-setup-to-inputs-ignore.js +11 -15
  25. package/src/utils/config/functions.js +3 -5
  26. package/src/utils/config/get-jest-projects.js +1 -2
@@ -1,45 +1,42 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.updateConfigsJest29 = void 0;
4
- const tslib_1 = require("tslib");
5
4
  const devkit_1 = require("@nx/devkit");
6
5
  const executor_options_utils_1 = require("@nx/devkit/src/generators/executor-options-utils");
7
6
  const ast_utils_1 = require("../../utils/ast-utils");
8
7
  const tsquery_1 = require("@phenomnomnominal/tsquery");
9
8
  const ts = require("typescript");
10
9
  const find_root_jest_files_1 = require("../../utils/config/find-root-jest-files");
11
- function updateConfigsJest29(tree) {
12
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
13
- const rootPreset = (0, find_root_jest_files_1.findRootJestPreset)(tree);
14
- const targetsWithJest = new Set();
15
- // have to use graph so the negative configuration targets are expanded
16
- const graph = yield (0, devkit_1.createProjectGraphAsync)();
17
- (0, executor_options_utils_1.forEachExecutorOptionsInGraph)(graph, '@nrwl/jest:jest', (options, projectName, targetName) => {
18
- if (options.jestConfig && tree.exists(options.jestConfig)) {
19
- targetsWithJest.add(targetName);
20
- // if the default root preset exists or if the project doesn't have a 'preset' configured
21
- // -> update snapshot config
22
- if (!rootPreset || !hasPresetConfigured(tree, options.jestConfig)) {
23
- addSnapshotOptionsToConfig(tree, options.jestConfig, `From within the project directory, run "nx test --update-snapshot"`);
24
- }
25
- updateTsJestOptions(tree, options.jestConfig);
26
- updateNgJestOptions(tree, options.jestConfig);
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"`);
27
22
  }
28
- });
29
- if (rootPreset && tree.exists(rootPreset)) {
30
- const cmd = `"nx affected --targets=${Array.from(targetsWithJest).join(',')} --update-snapshot"`;
31
- addSnapshotOptionsToConfig(tree, rootPreset, cmd);
32
- updateTsJestOptions(tree, rootPreset);
33
- updateNgJestOptions(tree, rootPreset);
23
+ updateTsJestOptions(tree, options.jestConfig);
24
+ updateNgJestOptions(tree, options.jestConfig);
34
25
  }
35
- yield (0, devkit_1.formatFiles)(tree);
36
- devkit_1.logger.info((0, devkit_1.stripIndents) `NX Jest Snapshot format changed in v29.
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.
37
35
  By default Nx kept the older style to prevent breaking of existing tests with snapshots.
38
36
  It's recommend you update to the latest format.
39
37
  You can do this in your project's jest config file.
40
38
  Remove the snapshotFormat property and re-run tests with the --update-snapshot flag.
41
39
  More info: https://jestjs.io/docs/upgrading-to-jest29#snapshot-format`);
42
- });
43
40
  }
44
41
  exports.updateConfigsJest29 = updateConfigsJest29;
45
42
  function addSnapshotOptionsToConfig(tree, configPath, updateSnapshotExample) {
@@ -66,9 +63,8 @@ snapshotFormat: { escapeString: true, printBasicPrototype: true }
66
63
  tree.write(configPath, updatedConfig);
67
64
  }
68
65
  function hasPresetConfigured(tree, configPath) {
69
- var _a;
70
66
  const contents = tree.read(configPath, 'utf-8');
71
- return (((_a = tsquery_1.tsquery.query(contents, `${ast_utils_1.TS_QUERY_JEST_CONFIG_PREFIX} > ObjectLiteralExpression PropertyAssignment:has(Identifier[name="preset"])`)) === null || _a === void 0 ? void 0 : _a.length) > 0);
67
+ return (tsquery_1.tsquery.query(contents, `${ast_utils_1.TS_QUERY_JEST_CONFIG_PREFIX} > ObjectLiteralExpression PropertyAssignment:has(Identifier[name="preset"])`)?.length > 0);
72
68
  }
73
69
  function updateTsJestOptions(tree, configPath) {
74
70
  // query for the globals property, if they don't have one then there's nothing to modify.
@@ -125,15 +121,13 @@ testEnvironmentOptions: { teardown: ${ngJestTeardownConfig} },
125
121
  tree.write(configPath, updatedConfig);
126
122
  }
127
123
  function getGlobalTsJestConfig(node) {
128
- var _a;
129
124
  const globalObject = node.initializer;
130
125
  const foundConfig = globalObject.properties.find((p) => ts.isPropertyAssignment(p) && p.name.getText().includes('ts-jest'));
131
- return ((_a = foundConfig === null || foundConfig === void 0 ? void 0 : foundConfig.initializer) === null || _a === void 0 ? void 0 : _a.getText()) || '';
126
+ return foundConfig?.initializer?.getText() || '';
132
127
  }
133
128
  function getGlobalConfigWithoutTsJest(node) {
134
- var _a;
135
- const globalObject = node === null || node === void 0 ? void 0 : node.initializer;
136
- const withoutTsJest = (_a = globalObject === null || globalObject === void 0 ? void 0 : globalObject.properties) === null || _a === void 0 ? void 0 : _a.filter((p) => {
129
+ const globalObject = node?.initializer;
130
+ const withoutTsJest = globalObject?.properties?.filter((p) => {
137
131
  return !(ts.isPropertyAssignment(p) && p.name.getText().includes('ts-jest'));
138
132
  });
139
133
  const globalConfigs = withoutTsJest.map((c) => c.getText()).join(',\n');
@@ -141,7 +135,7 @@ function getGlobalConfigWithoutTsJest(node) {
141
135
  }
142
136
  function getNodeWithComments(fullText, node) {
143
137
  const commentRanges = ts.getLeadingCommentRanges(fullText, node.getFullStart());
144
- if ((commentRanges === null || commentRanges === void 0 ? void 0 : commentRanges.length) > 0) {
138
+ if (commentRanges?.length > 0) {
145
139
  const withComments = `${commentRanges
146
140
  .map((r) => fullText.slice(r.pos, r.end))
147
141
  .join('\n')}\n${node.getText()}`;
@@ -1,26 +1,23 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.updateJestMocked = exports.updateJestMockTypes = exports.updateTestsJest29 = void 0;
4
- const tslib_1 = require("tslib");
5
4
  const devkit_1 = require("@nx/devkit");
6
5
  const executor_options_utils_1 = require("@nx/devkit/src/generators/executor-options-utils");
7
6
  const tsquery_1 = require("@phenomnomnominal/tsquery");
8
7
  const ast_utils_1 = require("../../utils/ast-utils");
9
- function updateTestsJest29(tree) {
10
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
11
- const graph = yield (0, devkit_1.createProjectGraphAsync)();
12
- (0, executor_options_utils_1.forEachExecutorOptionsInGraph)(graph, '@nrwl/jest:jest', (options, projectName) => {
13
- const projectConfig = (0, devkit_1.readProjectConfiguration)(tree, projectName);
14
- (0, devkit_1.visitNotIgnoredFiles)(tree, projectConfig.sourceRoot || projectConfig.root, (file) => {
15
- if (!ast_utils_1.TEST_FILE_PATTERN.test(file)) {
16
- return;
17
- }
18
- updateJestMockTypes(tree, file);
19
- updateJestMocked(tree, file);
20
- });
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);
21
18
  });
22
- yield (0, devkit_1.formatFiles)(tree);
23
19
  });
20
+ await (0, devkit_1.formatFiles)(tree);
24
21
  }
25
22
  exports.updateTestsJest29 = updateTestsJest29;
26
23
  function updateJestMockTypes(tree, filePath) {
@@ -1,12 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const tslib_1 = require("tslib");
4
3
  const devkit_1 = require("@nx/devkit");
5
4
  const replace_package_1 = require("@nx/devkit/src/utils/replace-package");
6
- function replacePackage(tree) {
7
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
8
- yield (0, replace_package_1.replaceNrwlPackageWithNxPackage)(tree, '@nrwl/jest', '@nx/jest');
9
- yield (0, devkit_1.formatFiles)(tree);
10
- });
5
+ async function replacePackage(tree) {
6
+ await (0, replace_package_1.replaceNrwlPackageWithNxPackage)(tree, '@nrwl/jest', '@nx/jest');
7
+ await (0, devkit_1.formatFiles)(tree);
11
8
  }
12
9
  exports.default = replacePackage;
@@ -1,22 +1,18 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.addTestSetupToIgnoredInputs = void 0;
4
- const tslib_1 = require("tslib");
5
4
  const devkit_1 = require("@nx/devkit");
6
- function addTestSetupToIgnoredInputs(tree) {
7
- var _a;
8
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
9
- const nxJson = (0, devkit_1.readNxJson)(tree);
10
- if (!nxJson) {
11
- return;
12
- }
13
- if (((_a = nxJson.namedInputs) === null || _a === void 0 ? void 0 : _a.production) &&
14
- !nxJson.namedInputs.production.includes('!{projectRoot}/src/test-setup.[jt]s')) {
15
- nxJson.namedInputs.production.push('!{projectRoot}/src/test-setup.[jt]s');
16
- (0, devkit_1.updateNxJson)(tree, nxJson);
17
- }
18
- yield (0, devkit_1.formatFiles)(tree);
19
- });
5
+ async function addTestSetupToIgnoredInputs(tree) {
6
+ const nxJson = (0, devkit_1.readNxJson)(tree);
7
+ if (!nxJson) {
8
+ return;
9
+ }
10
+ if (nxJson.namedInputs?.production &&
11
+ !nxJson.namedInputs.production.includes('!{projectRoot}/src/test-setup.[jt]s')) {
12
+ nxJson.namedInputs.production.push('!{projectRoot}/src/test-setup.[jt]s');
13
+ (0, devkit_1.updateNxJson)(tree, nxJson);
14
+ }
15
+ await (0, devkit_1.formatFiles)(tree);
20
16
  }
21
17
  exports.addTestSetupToIgnoredInputs = addTestSetupToIgnoredInputs;
22
18
  exports.default = addTestSetupToIgnoredInputs;
@@ -125,23 +125,21 @@ function removeProperty(object, properties) {
125
125
  }
126
126
  exports.removeProperty = removeProperty;
127
127
  function isModuleExport(node) {
128
- var _a, _b;
129
128
  if (!tsModule) {
130
129
  tsModule = (0, ensure_typescript_1.ensureTypescript)();
131
130
  }
132
131
  return (tsModule.isExpressionStatement(node) &&
133
- ((_a = node.expression) === null || _a === void 0 ? void 0 : _a.kind) &&
132
+ node.expression?.kind &&
134
133
  tsModule.isBinaryExpression(node.expression) &&
135
134
  node.expression.left.getText() === 'module.exports' &&
136
- ((_b = node.expression.operatorToken) === null || _b === void 0 ? void 0 : _b.kind) === tsModule.SyntaxKind.EqualsToken);
135
+ node.expression.operatorToken?.kind === tsModule.SyntaxKind.EqualsToken);
137
136
  }
138
137
  function isDefaultExport(node) {
139
- var _a;
140
138
  if (!tsModule) {
141
139
  tsModule = (0, ensure_typescript_1.ensureTypescript)();
142
140
  }
143
141
  return (tsModule.isExportAssignment(node) &&
144
- ((_a = node.expression) === null || _a === void 0 ? void 0 : _a.kind) &&
142
+ node.expression?.kind &&
145
143
  tsModule.isObjectLiteralExpression(node.expression) &&
146
144
  node.getText().startsWith('export default'));
147
145
  }
@@ -19,7 +19,6 @@ function getJestConfigProjectPath(projectJestConfigPath) {
19
19
  *
20
20
  **/
21
21
  function getJestProjects() {
22
- var _a;
23
22
  const ws = (0, file_utils_1.readWorkspaceConfig)({
24
23
  format: 'nx',
25
24
  });
@@ -33,7 +32,7 @@ function getJestProjects() {
33
32
  targetConfiguration.executor !== '@nrwl/jest:jest') {
34
33
  continue;
35
34
  }
36
- if ((_a = targetConfiguration.options) === null || _a === void 0 ? void 0 : _a.jestConfig) {
35
+ if (targetConfiguration.options?.jestConfig) {
37
36
  jestConfigurationSet.add(getJestConfigProjectPath(targetConfiguration.options.jestConfig));
38
37
  }
39
38
  if (targetConfiguration.configurations) {