@nx/jest 20.5.0-beta.2 → 20.5.0-beta.4

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nx/jest",
3
- "version": "20.5.0-beta.2",
3
+ "version": "20.5.0-beta.4",
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": {
@@ -37,8 +37,8 @@
37
37
  "dependencies": {
38
38
  "@jest/reporters": "^29.4.1",
39
39
  "@jest/test-result": "^29.4.1",
40
- "@nx/devkit": "20.5.0-beta.2",
41
- "@nx/js": "20.5.0-beta.2",
40
+ "@nx/devkit": "20.5.0-beta.4",
41
+ "@nx/js": "20.5.0-beta.4",
42
42
  "@phenomnomnominal/tsquery": "~5.0.1",
43
43
  "identity-obj-proxy": "3.0.0",
44
44
  "jest-config": "^29.4.1",
@@ -99,9 +99,10 @@ function generateGlobalConfig(tree, isJS) {
99
99
  module.exports = async () => ({
100
100
  projects: await getJestProjectsAsync()
101
101
  });`
102
- : `import { getJestProjectsAsync } from '@nx/jest';
102
+ : `import type { Config } from 'jest';
103
+ import { getJestProjectsAsync } from '@nx/jest';
103
104
 
104
- export default async () => ({
105
+ export default async (): Promise<Config> => ({
105
106
  projects: await getJestProjectsAsync()
106
107
  });`;
107
108
  tree.write(`jest.config.${isJS ? 'js' : 'ts'}`, contents);
@@ -7,7 +7,7 @@ export interface JestPluginOptions {
7
7
  */
8
8
  ciGroupName?: string;
9
9
  /**
10
- * Whether to use jest-config and jest-runtime to load Jest configuration and context.
10
+ * Whether to use jest-config and jest-runtime are used to load Jest configuration and context.
11
11
  * Disabling this is much faster but could be less correct since we are using our own config loader
12
12
  * and test matcher instead of Jest's.
13
13
  */
@@ -31,8 +31,36 @@ exports.createNodesV2 = [
31
31
  const targetsCache = readTargetsCache(cachePath);
32
32
  // Cache jest preset(s) to avoid penalties of module load times. Most of jest configs will use the same preset.
33
33
  const presetCache = {};
34
+ const packageManagerWorkspacesGlob = (0, globs_1.combineGlobPatterns)((0, package_json_1.getGlobPatternsFromPackageManagerWorkspaces)(context.workspaceRoot));
35
+ options = normalizeOptions(options);
36
+ const { roots: projectRoots, configFiles: validConfigFiles } = configFiles.reduce((acc, configFile) => {
37
+ const potentialRoot = (0, path_1.dirname)(configFile);
38
+ if (checkIfConfigFileShouldBeProject(configFile, potentialRoot, packageManagerWorkspacesGlob, context)) {
39
+ acc.roots.push(potentialRoot);
40
+ acc.configFiles.push(configFile);
41
+ }
42
+ return acc;
43
+ }, {
44
+ roots: [],
45
+ configFiles: [],
46
+ });
47
+ const hashes = await (0, calculate_hash_for_create_nodes_1.calculateHashesForCreateNodes)(projectRoots, options, context);
34
48
  try {
35
- return await (0, devkit_1.createNodesFromFiles)((configFile, options, context) => createNodesInternal(configFile, options, context, targetsCache, presetCache), configFiles, options, context);
49
+ return await (0, devkit_1.createNodesFromFiles)(async (configFilePath, options, context, idx) => {
50
+ const projectRoot = projectRoots[idx];
51
+ const hash = hashes[idx];
52
+ targetsCache[hash] ??= await buildJestTargets(configFilePath, projectRoot, options, context, presetCache);
53
+ const { targets, metadata } = targetsCache[hash];
54
+ return {
55
+ projects: {
56
+ [projectRoot]: {
57
+ root: projectRoot,
58
+ targets,
59
+ metadata,
60
+ },
61
+ },
62
+ };
63
+ }, validConfigFiles, options, context);
36
64
  }
37
65
  finally {
38
66
  writeTargetsToCache(cachePath, targetsCache);
@@ -45,26 +73,39 @@ exports.createNodesV2 = [
45
73
  */
46
74
  exports.createNodes = [
47
75
  jestConfigGlob,
48
- (...args) => {
76
+ async (configFilePath, options, context) => {
49
77
  devkit_1.logger.warn('`createNodes` is deprecated. Update your plugin to utilize createNodesV2 instead. In Nx 20, this will change to the createNodesV2 API.');
50
- return createNodesInternal(...args, {}, {});
78
+ const projectRoot = (0, path_1.dirname)(configFilePath);
79
+ const packageManagerWorkspacesGlob = (0, globs_1.combineGlobPatterns)((0, package_json_1.getGlobPatternsFromPackageManagerWorkspaces)(context.workspaceRoot));
80
+ if (!checkIfConfigFileShouldBeProject(configFilePath, projectRoot, packageManagerWorkspacesGlob, context)) {
81
+ return {};
82
+ }
83
+ options = normalizeOptions(options);
84
+ const { targets, metadata } = await buildJestTargets(configFilePath, projectRoot, options, context, {});
85
+ return {
86
+ projects: {
87
+ [projectRoot]: {
88
+ root: projectRoot,
89
+ targets,
90
+ metadata,
91
+ },
92
+ },
93
+ };
51
94
  },
52
95
  ];
53
- async function createNodesInternal(configFilePath, options, context, targetsCache, presetCache) {
54
- const projectRoot = (0, path_1.dirname)(configFilePath);
55
- const packageManagerWorkspacesGlob = (0, globs_1.combineGlobPatterns)((0, package_json_1.getGlobPatternsFromPackageManagerWorkspaces)(context.workspaceRoot));
96
+ function checkIfConfigFileShouldBeProject(configFilePath, projectRoot, packageManagerWorkspacesGlob, context) {
56
97
  // Do not create a project if package.json and project.json isn't there.
57
98
  const siblingFiles = (0, fs_1.readdirSync)((0, path_1.join)(context.workspaceRoot, projectRoot));
58
99
  if (!siblingFiles.includes('package.json') &&
59
100
  !siblingFiles.includes('project.json')) {
60
- return {};
101
+ return false;
61
102
  }
62
103
  else if (!siblingFiles.includes('project.json') &&
63
104
  siblingFiles.includes('package.json')) {
64
105
  const path = (0, devkit_1.joinPathFragments)(projectRoot, 'package.json');
65
106
  const isPackageJsonProject = (0, minimatch_1.minimatch)(path, packageManagerWorkspacesGlob);
66
107
  if (!isPackageJsonProject) {
67
- return {};
108
+ return false;
68
109
  }
69
110
  }
70
111
  const jestConfigContent = (0, fs_1.readFileSync)((0, path_1.resolve)(context.workspaceRoot, configFilePath), 'utf-8');
@@ -72,21 +113,9 @@ async function createNodesInternal(configFilePath, options, context, targetsCach
72
113
  // The `getJestProjectsAsync` function uses the project graph, which leads to a
73
114
  // circular dependency. We can skip this since it's no intended to be used for
74
115
  // an Nx project.
75
- return {};
116
+ return false;
76
117
  }
77
- options = normalizeOptions(options);
78
- const hash = await (0, calculate_hash_for_create_nodes_1.calculateHashForCreateNodes)(projectRoot, options, context);
79
- targetsCache[hash] ??= await buildJestTargets(configFilePath, projectRoot, options, context, presetCache);
80
- const { targets, metadata } = targetsCache[hash];
81
- return {
82
- projects: {
83
- [projectRoot]: {
84
- root: projectRoot,
85
- targets,
86
- metadata,
87
- },
88
- },
89
- };
118
+ return true;
90
119
  }
91
120
  async function buildJestTargets(configFilePath, projectRoot, options, context, presetCache) {
92
121
  const absConfigFilePath = (0, path_1.resolve)(context.workspaceRoot, configFilePath);
@@ -116,11 +145,13 @@ async function buildJestTargets(configFilePath, projectRoot, options, context, p
116
145
  },
117
146
  },
118
147
  });
148
+ // Not normalizing it here since also affects options for convert-to-inferred.
149
+ const disableJestRuntime = options.disableJestRuntime !== false;
119
150
  const cache = (target.cache = true);
120
- const inputs = (target.inputs = getInputs(namedInputs, rawConfig.preset, projectRoot, context.workspaceRoot, options.disableJestRuntime));
151
+ const inputs = (target.inputs = getInputs(namedInputs, rawConfig.preset, projectRoot, context.workspaceRoot, disableJestRuntime));
121
152
  let metadata;
122
153
  const groupName = options?.ciGroupName ?? deductGroupNameFromTarget(options?.ciTargetName);
123
- if (options.disableJestRuntime) {
154
+ if (disableJestRuntime) {
124
155
  const outputs = (target.outputs = getOutputs(projectRoot, rawConfig.coverageDirectory
125
156
  ? (0, path_1.join)(context.workspaceRoot, projectRoot, rawConfig.coverageDirectory)
126
157
  : undefined, undefined, context));
@@ -192,10 +223,17 @@ async function buildJestTargets(configFilePath, projectRoot, options, context, p
192
223
  }
193
224
  else {
194
225
  const { readConfig } = requireJestUtil('jest-config', projectRoot, context.workspaceRoot);
195
- const config = await readConfig({
196
- _: [],
197
- $0: undefined,
198
- }, rawConfig, undefined, (0, path_1.dirname)(absConfigFilePath));
226
+ let config;
227
+ try {
228
+ config = await readConfig({
229
+ _: [],
230
+ $0: undefined,
231
+ }, rawConfig, undefined, (0, path_1.dirname)(absConfigFilePath));
232
+ }
233
+ catch (e) {
234
+ console.error(e);
235
+ throw e;
236
+ }
199
237
  const outputs = (target.outputs = getOutputs(projectRoot, config.globalConfig?.coverageDirectory, config.globalConfig?.outputFile, context));
200
238
  if (options?.ciTargetName) {
201
239
  // nx-ignore-next-line
@@ -208,8 +246,7 @@ async function buildJestTargets(configFilePath, projectRoot, options, context, p
208
246
  const source = new jest.SearchSource(jestContext);
209
247
  const jestVersion = (0, version_utils_1.getInstalledJestMajorVersion)();
210
248
  const specs = jestVersion >= 30
211
- ? // @ts-expect-error Jest 30+ expects the project config as the second argument
212
- await source.getTestPaths(config.globalConfig, config.projectConfig)
249
+ ? await source.getTestPaths(config.globalConfig, config.projectConfig)
213
250
  : await source.getTestPaths(config.globalConfig);
214
251
  const testPaths = new Set(specs.tests.map(({ path }) => path));
215
252
  if (testPaths.size > 0) {
@@ -401,32 +438,19 @@ function requireJestUtil(packageName, projectRoot, workspaceRoot) {
401
438
  }
402
439
  async function getTestPaths(projectRoot, rawConfig, absConfigFilePath, context, presetCache) {
403
440
  const testMatch = await getJestOption(rawConfig, absConfigFilePath, 'testMatch', presetCache);
404
- if (testMatch) {
405
- return await (0, workspace_context_1.globWithWorkspaceContext)(context.workspaceRoot, testMatch.map((pattern) => (0, path_1.join)(projectRoot, pattern)), []);
406
- }
407
- else {
408
- const testRegex = await getJestOption(rawConfig, absConfigFilePath, 'testRegex', presetCache);
409
- if (testRegex) {
410
- const files = [];
411
- const testRegexes = Array.isArray(rawConfig.testRegex)
412
- ? rawConfig.testRegex.map((r) => new RegExp(r))
413
- : [new RegExp(rawConfig.testRegex)];
414
- const projectFiles = await (0, workspace_context_1.getFilesInDirectoryUsingContext)(context.workspaceRoot, projectRoot);
415
- for (const file of projectFiles) {
416
- if (testRegexes.some((r) => r.test(file)))
417
- files.push(file);
418
- }
419
- return files;
420
- }
421
- else {
422
- // Default copied from https://github.com/jestjs/jest/blob/d1a2ed7/packages/jest-config/src/Defaults.ts#L84
423
- const defaultTestMatch = [
424
- '**/__tests__/**/*.?([mc])[jt]s?(x)',
425
- '**/?(*.)+(spec|test).?([mc])[jt]s?(x)',
426
- ];
427
- return await (0, workspace_context_1.globWithWorkspaceContext)(context.workspaceRoot, defaultTestMatch.map((pattern) => (0, path_1.join)(projectRoot, pattern)), []);
428
- }
441
+ let paths = await (0, workspace_context_1.globWithWorkspaceContext)(context.workspaceRoot, (testMatch || [
442
+ // Default copied from https://github.com/jestjs/jest/blob/d1a2ed7/packages/jest-config/src/Defaults.ts#L84
443
+ '**/__tests__/**/*.?([mc])[jt]s?(x)',
444
+ '**/?(*.)+(spec|test).?([mc])[jt]s?(x)',
445
+ ]).map((pattern) => (0, path_1.join)(projectRoot, pattern)), []);
446
+ const testRegex = await getJestOption(rawConfig, absConfigFilePath, 'testRegex', presetCache);
447
+ if (testRegex) {
448
+ const testRegexes = Array.isArray(rawConfig.testRegex)
449
+ ? rawConfig.testRegex.map((r) => new RegExp(r))
450
+ : [new RegExp(rawConfig.testRegex)];
451
+ paths = paths.filter((path) => testRegexes.some((r) => r.test(path)));
429
452
  }
453
+ return paths;
430
454
  }
431
455
  async function getJestOption(rawConfig, absConfigFilePath, optionName, presetCache) {
432
456
  if (rawConfig[optionName])