@nx/jest 22.7.0-beta.10 → 22.7.0-beta.12

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": "22.7.0-beta.10",
3
+ "version": "22.7.0-beta.12",
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": {
@@ -38,8 +38,8 @@
38
38
  "dependencies": {
39
39
  "@jest/reporters": "^30.0.2",
40
40
  "@jest/test-result": "^30.0.2",
41
- "@nx/devkit": "22.7.0-beta.10",
42
- "@nx/js": "22.7.0-beta.10",
41
+ "@nx/devkit": "22.7.0-beta.12",
42
+ "@nx/js": "22.7.0-beta.12",
43
43
  "@phenomnomnominal/tsquery": "~6.1.4",
44
44
  "identity-obj-proxy": "3.0.0",
45
45
  "jest-config": "^30.0.2",
@@ -53,7 +53,7 @@
53
53
  "yargs-parser": "21.1.1"
54
54
  },
55
55
  "devDependencies": {
56
- "nx": "22.7.0-beta.10"
56
+ "nx": "22.7.0-beta.12"
57
57
  },
58
58
  "publishConfig": {
59
59
  "access": "public"
@@ -1 +1 @@
1
- {"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../../../../packages/jest/src/plugins/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,aAAa,EAUd,MAAM,YAAY,CAAC;AA4BpB,MAAM,WAAW,iBAAiB;IAChC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,YAAY,CAAC,EAAE,MAAM,CAAC;IAEtB;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;;OAIG;IACH,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B;;;;;;;;OAQG;IACH,eAAe,CAAC,EAAE,OAAO,CAAC;CAC3B;AAmBD,eAAO,MAAM,WAAW,EAAE,aAAa,CAAC,iBAAiB,CAsFxD,CAAC;AAEF,eAAO,MAAM,aAAa,kCAAc,CAAC"}
1
+ {"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../../../../packages/jest/src/plugins/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,aAAa,EAUd,MAAM,YAAY,CAAC;AAiCpB,MAAM,WAAW,iBAAiB;IAChC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,YAAY,CAAC,EAAE,MAAM,CAAC;IAEtB;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;;OAIG;IACH,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B;;;;;;;;OAQG;IACH,eAAe,CAAC,EAAE,OAAO,CAAC;CAC3B;AAwBD,eAAO,MAAM,WAAW,EAAE,aAAa,CAAC,iBAAiB,CAuIxD,CAAC;AAEF,eAAO,MAAM,aAAa,kCAAc,CAAC"}
@@ -15,6 +15,8 @@ const globs_1 = require("nx/src/utils/globs");
15
15
  const installation_directory_1 = require("nx/src/utils/installation-directory");
16
16
  const plugins_1 = require("nx/src/utils/plugins");
17
17
  const workspace_context_1 = require("nx/src/utils/workspace-context");
18
+ const js_1 = require("@nx/js");
19
+ const internal_1 = require("@nx/js/src/internal");
18
20
  const versions_1 = require("../utils/versions");
19
21
  const REPORTER_BUILTINS = new Set(['default', 'github-actions', 'summary']);
20
22
  function readTargetsCache(cachePath) {
@@ -34,9 +36,15 @@ exports.createNodes = [
34
36
  const targetsCache = readTargetsCache(cachePath);
35
37
  // Cache jest preset(s) to avoid penalties of module load times. Most of jest configs will use the same preset.
36
38
  const presetCache = {};
39
+ // Cache tsconfig reads + isolatedModules resolution. Many projects share
40
+ // the same base tsconfig in their extends chain — read each file once.
41
+ const tsconfigJsonCache = new Map();
42
+ const tsconfigExistsCache = new Map();
43
+ const isolatedModulesCache = new Map();
37
44
  const isInPackageManagerWorkspaces = buildPackageJsonWorkspacesMatcher(context.workspaceRoot);
38
45
  options = normalizeOptions(options);
39
- const pmc = (0, devkit_1.getPackageManagerCommand)((0, devkit_1.detectPackageManager)(context.workspaceRoot));
46
+ const packageManager = (0, devkit_1.detectPackageManager)(context.workspaceRoot);
47
+ const pmc = (0, devkit_1.getPackageManagerCommand)(packageManager);
40
48
  const { roots: projectRoots, configFiles: validConfigFiles } = configFiles.reduce((acc, configFile) => {
41
49
  const potentialRoot = (0, node_path_1.dirname)(configFile);
42
50
  if (checkIfConfigFileShouldBeProject(configFile, potentialRoot, isInPackageManagerWorkspaces, context)) {
@@ -48,12 +56,41 @@ exports.createNodes = [
48
56
  roots: [],
49
57
  configFiles: [],
50
58
  });
51
- const hashes = await (0, calculate_hash_for_create_nodes_1.calculateHashesForCreateNodes)(projectRoots, options, context);
59
+ const lockFilePattern = (0, js_1.getLockFileName)(packageManager);
60
+ let requireCacheCleared = false;
61
+ const loadedConfigs = [];
62
+ for (let i = 0; i < validConfigFiles.length; i++) {
63
+ const configFilePath = validConfigFiles[i];
64
+ const projectRoot = projectRoots[i];
65
+ const absConfigFilePath = (0, node_path_1.resolve)(context.workspaceRoot, configFilePath);
66
+ if (!requireCacheCleared && require.cache[absConfigFilePath]) {
67
+ (0, config_utils_1.clearRequireCache)();
68
+ requireCacheCleared = true;
69
+ }
70
+ const rawConfig = await (0, config_utils_1.loadConfigFile)(absConfigFilePath, [
71
+ 'tsconfig.spec.json',
72
+ 'tsconfig.test.json',
73
+ 'tsconfig.jest.json',
74
+ 'tsconfig.json',
75
+ ]);
76
+ const { externalFiles, needsDtsInputs } = await collectExternalFileReferences(rawConfig, absConfigFilePath, projectRoot, context.workspaceRoot, {
77
+ presetCache,
78
+ tsconfigJsonCache,
79
+ tsconfigExistsCache,
80
+ isolatedModulesCache,
81
+ });
82
+ loadedConfigs.push({ rawConfig, externalFiles, needsDtsInputs });
83
+ }
84
+ const hashes = await (0, calculate_hash_for_create_nodes_1.calculateHashesForCreateNodes)(projectRoots, options, context, loadedConfigs.map(({ externalFiles }) => [
85
+ lockFilePattern,
86
+ ...externalFiles,
87
+ ]));
52
88
  try {
53
89
  return await (0, devkit_1.createNodesFromFiles)(async (configFilePath, options, context, idx) => {
54
90
  const projectRoot = projectRoots[idx];
55
91
  const hash = hashes[idx];
56
- targetsCache[hash] ??= await buildJestTargets(configFilePath, projectRoot, options, context, presetCache, pmc);
92
+ const { rawConfig, needsDtsInputs } = loadedConfigs[idx];
93
+ targetsCache[hash] ??= await buildJestTargets(rawConfig, needsDtsInputs, configFilePath, projectRoot, options, context, presetCache, pmc);
57
94
  const { targets, metadata } = targetsCache[hash];
58
95
  return {
59
96
  projects: {
@@ -103,18 +140,8 @@ function checkIfConfigFileShouldBeProject(configFilePath, projectRoot, isInPacka
103
140
  }
104
141
  return true;
105
142
  }
106
- async function buildJestTargets(configFilePath, projectRoot, options, context, presetCache, pmc) {
143
+ async function buildJestTargets(rawConfig, needsDtsInputs, configFilePath, projectRoot, options, context, presetCache, pmc) {
107
144
  const absConfigFilePath = (0, node_path_1.resolve)(context.workspaceRoot, configFilePath);
108
- if (require.cache[absConfigFilePath])
109
- (0, config_utils_1.clearRequireCache)();
110
- const rawConfig = await (0, config_utils_1.loadConfigFile)(absConfigFilePath,
111
- // lookup for the same files we look for in the resolver and fall back to tsconfig.json
112
- [
113
- 'tsconfig.spec.json',
114
- 'tsconfig.test.json',
115
- 'tsconfig.jest.json',
116
- 'tsconfig.json',
117
- ]);
118
145
  const targets = {};
119
146
  const namedInputs = (0, get_named_inputs_1.getNamedInputs)(projectRoot, context);
120
147
  const tsNodeCompilerOptions = JSON.stringify({
@@ -154,7 +181,7 @@ async function buildJestTargets(configFilePath, projectRoot, options, context, p
154
181
  ? (0, node_path_1.resolve)((0, node_path_1.dirname)(absConfigFilePath), rawConfig.rootDir)
155
182
  : (0, node_path_1.resolve)(context.workspaceRoot, projectRoot);
156
183
  const cache = (target.cache = true);
157
- const inputs = (target.inputs = await getInputs(namedInputs, rawConfig, rootDir, projectRoot, context.workspaceRoot, presetCache, useJestResolver));
184
+ const inputs = (target.inputs = await getInputs(namedInputs, rawConfig, rootDir, projectRoot, context.workspaceRoot, presetCache, needsDtsInputs, useJestResolver));
158
185
  let metadata;
159
186
  const groupName = options?.ciGroupName ?? (0, plugins_1.deriveGroupNameFromTarget)(options?.ciTargetName);
160
187
  if (disableJestRuntime) {
@@ -265,7 +292,7 @@ async function buildJestTargets(configFilePath, projectRoot, options, context, p
265
292
  });
266
293
  const jest = require(resolveJestPath(projectRoot, context.workspaceRoot));
267
294
  const source = new jest.SearchSource(jestContext);
268
- const jestVersion = (0, versions_1.getInstalledJestMajorVersion)();
295
+ const jestVersion = getJestMajorVersion();
269
296
  const specs = jestVersion >= 30
270
297
  ? await source.getTestPaths(config.globalConfig, config.projectConfig)
271
298
  : // @ts-expect-error Jest v29 doesn't have the projectConfig parameter
@@ -347,7 +374,7 @@ async function buildJestTargets(configFilePath, projectRoot, options, context, p
347
374
  }
348
375
  return { targets, metadata };
349
376
  }
350
- async function getInputs(namedInputs, rawConfig, rootDir, projectRoot, workspaceRoot, presetCache, useJestResolver) {
377
+ async function getInputs(namedInputs, rawConfig, rootDir, projectRoot, workspaceRoot, presetCache, needsDtsInputs, useJestResolver) {
351
378
  const inputs = [
352
379
  ...('production' in namedInputs
353
380
  ? ['default', '^production']
@@ -388,6 +415,16 @@ async function getInputs(namedInputs, rawConfig, rootDir, projectRoot, workspace
388
415
  }
389
416
  }
390
417
  inputs.push({ externalDependencies });
418
+ // When ts-jest runs without isolatedModules, it creates a TypeScript
419
+ // Language Service that reads .d.ts files from dependency projects.
420
+ // Declare these as dependentTasksOutputFiles so changes to dependency
421
+ // type declarations correctly invalidate the test cache.
422
+ if (needsDtsInputs) {
423
+ inputs.push({
424
+ dependentTasksOutputFiles: '**/*.d.ts',
425
+ transitive: true,
426
+ });
427
+ }
391
428
  return inputs;
392
429
  }
393
430
  function resolvePresetInputWithoutJestResolver(presetValue, rootDir, projectRoot, workspaceRoot) {
@@ -568,6 +605,96 @@ async function getTestPaths(projectRoot, rawConfig, rootDir, context, presetCach
568
605
  }
569
606
  return { specs: paths, testMatch };
570
607
  }
608
+ /**
609
+ * Collects workspace-relative paths to files whose CONTENT the plugin reads
610
+ * when computing inferred targets and that live OUTSIDE the project root.
611
+ *
612
+ * Only two kinds of files qualify:
613
+ * - The jest preset (loaded to read its `transform`, etc.)
614
+ * - Tsconfig files in the extends chain referenced by ts-jest (read to
615
+ * determine `isolatedModules`); only walked when ts-jest is not already
616
+ * known to be in isolated mode.
617
+ *
618
+ * Other config references (setup files, custom resolvers, transformers,
619
+ * etc.) are NOT collected — the plugin only resolves their paths and emits
620
+ * them as task inputs; their content is not read by the plugin, so changes
621
+ * to them don't influence inference.
622
+ */
623
+ async function collectExternalFileReferences(rawConfig, absConfigFilePath, projectRoot, workspaceRoot, caches) {
624
+ const { presetCache, tsconfigJsonCache, tsconfigExistsCache, isolatedModulesCache, } = caches;
625
+ const absWorkspaceRoot = (0, node_path_1.resolve)(workspaceRoot);
626
+ const rootDir = rawConfig.rootDir
627
+ ? (0, node_path_1.resolve)((0, node_path_1.dirname)(absConfigFilePath), rawConfig.rootDir)
628
+ : (0, node_path_1.resolve)(absWorkspaceRoot, projectRoot);
629
+ const absoluteProjectRoot = (0, node_path_1.resolve)(absWorkspaceRoot, projectRoot);
630
+ const externalFiles = new Set();
631
+ const addIfExternal = (absolutePath) => {
632
+ if (!absolutePath)
633
+ return;
634
+ const rel = (0, devkit_1.normalizePath)((0, node_path_1.relative)(absWorkspaceRoot, absolutePath));
635
+ if (rel.startsWith('..') || (0, node_path_1.isAbsolute)(rel))
636
+ return; // outside workspace
637
+ if (rel.includes('node_modules/'))
638
+ return; // covered by lockfile
639
+ const relToProject = (0, devkit_1.normalizePath)((0, node_path_1.relative)(absoluteProjectRoot, absolutePath));
640
+ if (!relToProject.startsWith('..') && !(0, node_path_1.isAbsolute)(relToProject))
641
+ return; // inside project root
642
+ externalFiles.add(rel);
643
+ };
644
+ // Preset path (content is loaded by the plugin to merge with rawConfig)
645
+ if (typeof rawConfig.preset === 'string') {
646
+ const replaced = replaceRootDirInPath(rootDir, rawConfig.preset);
647
+ if (replaced.startsWith('.') || (0, node_path_1.isAbsolute)(replaced)) {
648
+ addIfExternal((0, node_path_1.resolve)(rootDir, replaced));
649
+ }
650
+ }
651
+ // ts-jest tsconfig extends chain — only walked when ts-jest is in
652
+ // non-isolated mode (otherwise the chain doesn't influence the output).
653
+ // Loading the preset is required to merge its transform with the raw
654
+ // config, since presets are the common source of ts-jest configuration.
655
+ const presetConfig = await loadPresetConfig(rawConfig, rootDir, presetCache);
656
+ const transform = {
657
+ ...(presetConfig?.transform ?? {}),
658
+ ...(rawConfig.transform ?? {}),
659
+ };
660
+ let needsDtsInputs = false;
661
+ for (const value of Object.values(transform)) {
662
+ let transformPath;
663
+ let transformOptions;
664
+ if (Array.isArray(value)) {
665
+ transformPath = value[0];
666
+ transformOptions = value[1];
667
+ }
668
+ else if (typeof value === 'string') {
669
+ transformPath = value;
670
+ }
671
+ else {
672
+ continue;
673
+ }
674
+ if (!isTsJestTransformer(transformPath))
675
+ continue;
676
+ if (transformOptions?.isolatedModules === true) {
677
+ const tsJestMajor = getTsJestMajorVersion();
678
+ if (tsJestMajor !== null && tsJestMajor < 30)
679
+ continue;
680
+ }
681
+ const tsconfigAbsPath = transformOptions?.tsconfig
682
+ ? (0, node_path_1.resolve)(rootDir, replaceRootDirInPath(rootDir, transformOptions.tsconfig))
683
+ : findNearestTsconfig(rootDir, absWorkspaceRoot, tsconfigExistsCache);
684
+ if (!tsconfigAbsPath) {
685
+ // No tsconfig found — ts-jest defaults to non-isolated mode
686
+ needsDtsInputs = true;
687
+ continue;
688
+ }
689
+ const { value: isolatedValue, visitedFiles } = resolveIsolatedModules(tsconfigAbsPath, tsconfigJsonCache, isolatedModulesCache);
690
+ for (const visitedFile of visitedFiles) {
691
+ addIfExternal(visitedFile);
692
+ }
693
+ if (isolatedValue !== true)
694
+ needsDtsInputs = true;
695
+ }
696
+ return { externalFiles: [...externalFiles], needsDtsInputs };
697
+ }
571
698
  async function loadPresetConfig(rawConfig, rootDir, presetCache) {
572
699
  if (!rawConfig.preset)
573
700
  return null;
@@ -708,6 +835,93 @@ async function getConfigFileInputs(rawConfig, rootDir, presetCache, resolveFileP
708
835
  externalDeps: [...externalDeps],
709
836
  };
710
837
  }
838
+ /**
839
+ * Walks up from `startDir` looking for `tsconfig.json`, stopping at
840
+ * `stopDir` (inclusive). Mirrors `ts.findConfigFile` but capped at the
841
+ * workspace root to avoid escaping the monorepo.
842
+ */
843
+ function findNearestTsconfig(startDir, stopDir, existsCache) {
844
+ let dir = startDir;
845
+ while (true) {
846
+ const candidate = (0, node_path_1.join)(dir, 'tsconfig.json');
847
+ let exists = existsCache.get(candidate);
848
+ if (exists === undefined) {
849
+ exists = (0, node_fs_1.existsSync)(candidate);
850
+ existsCache.set(candidate, exists);
851
+ }
852
+ if (exists)
853
+ return candidate;
854
+ if (dir === stopDir)
855
+ return null;
856
+ const parent = (0, node_path_1.dirname)(dir);
857
+ if (parent === dir)
858
+ return null;
859
+ dir = parent;
860
+ }
861
+ }
862
+ function isTsJestTransformer(value) {
863
+ if (value === 'ts-jest' || value.startsWith('ts-jest/')) {
864
+ return true;
865
+ }
866
+ // Handle resolved paths (e.g. from require.resolve('ts-jest') in configs)
867
+ const normalized = (0, devkit_1.normalizePath)(value);
868
+ return (normalized.includes('/node_modules/ts-jest/') ||
869
+ normalized.endsWith('/node_modules/ts-jest'));
870
+ }
871
+ /**
872
+ * Returns the effective `compilerOptions.isolatedModules` for a tsconfig,
873
+ * walking the `extends` chain via `walkTsconfigExtendsChain`. Tri-state:
874
+ * `value` is `undefined` when the option is not set anywhere in the chain.
875
+ */
876
+ function resolveIsolatedModules(tsconfigPath, jsonCache, resultCache) {
877
+ const cached = resultCache.get(tsconfigPath);
878
+ if (cached)
879
+ return cached;
880
+ let value;
881
+ const visitedFiles = new Set();
882
+ (0, internal_1.walkTsconfigExtendsChain)(tsconfigPath, (absPath, rawJson) => {
883
+ visitedFiles.add(absPath);
884
+ const opts = rawJson?.compilerOptions;
885
+ if (opts?.isolatedModules !== undefined) {
886
+ value = opts.isolatedModules === true;
887
+ return 'stop';
888
+ }
889
+ // verbatimModuleSyntax: true implies isolatedModules: true (TS 5.0+,
890
+ // the minimum TS version Nx supports)
891
+ if (opts?.verbatimModuleSyntax === true) {
892
+ value = true;
893
+ return 'stop';
894
+ }
895
+ return 'continue';
896
+ }, { jsonCache });
897
+ const result = { value, visitedFiles };
898
+ resultCache.set(tsconfigPath, result);
899
+ return result;
900
+ }
901
+ // Module-level memoization: package versions don't change during a process
902
+ // lifetime, so it's safe to cache across createNodes invocations.
903
+ let cachedJestMajorVersion;
904
+ function getJestMajorVersion() {
905
+ if (cachedJestMajorVersion === undefined) {
906
+ cachedJestMajorVersion = (0, versions_1.getInstalledJestMajorVersion)();
907
+ }
908
+ return cachedJestMajorVersion;
909
+ }
910
+ let cachedTsJestMajorVersion;
911
+ function getTsJestMajorVersion() {
912
+ if (cachedTsJestMajorVersion !== undefined) {
913
+ return cachedTsJestMajorVersion;
914
+ }
915
+ try {
916
+ const { major } = require('semver');
917
+ const version = require('ts-jest/package.json').version;
918
+ cachedTsJestMajorVersion = major(version);
919
+ }
920
+ catch {
921
+ cachedTsJestMajorVersion = null;
922
+ }
923
+ return cachedTsJestMajorVersion;
924
+ }
711
925
  async function getJestOption(rawConfig, rootDir, optionName, presetCache) {
712
926
  if (rawConfig[optionName])
713
927
  return rawConfig[optionName];