test-inventory 0.10.4 → 0.11.1

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 (50) hide show
  1. package/CHANGELOG.md +116 -0
  2. package/README.md +6 -4
  3. package/dist/cli.d.ts +7 -4
  4. package/dist/cli.js +18 -6
  5. package/dist/config-runs.d.ts +30 -4
  6. package/dist/config-runs.js +193 -38
  7. package/dist/config.d.ts +7 -6
  8. package/dist/config.js +260 -26
  9. package/dist/dependency-hash.d.ts +9 -1
  10. package/dist/dependency-hash.js +128 -13
  11. package/dist/diff.js +7 -2
  12. package/dist/extract.js +75 -17
  13. package/dist/files.js +2 -1
  14. package/dist/filter.js +1 -0
  15. package/dist/framework-version.d.ts +6 -0
  16. package/dist/framework-version.js +37 -9
  17. package/dist/frameworks/bindings.d.ts +9 -0
  18. package/dist/frameworks/bindings.js +28 -5
  19. package/dist/frameworks/bun.js +2 -2
  20. package/dist/frameworks/common.d.ts +12 -3
  21. package/dist/frameworks/common.js +30 -12
  22. package/dist/frameworks/jasmine.js +11 -3
  23. package/dist/frameworks/jest.js +11 -7
  24. package/dist/frameworks/mocha.js +3 -3
  25. package/dist/frameworks/node.js +6 -4
  26. package/dist/frameworks/playwright.d.ts +11 -3
  27. package/dist/frameworks/playwright.js +101 -32
  28. package/dist/frameworks/test-functions.d.ts +13 -7
  29. package/dist/frameworks/test-functions.js +41 -27
  30. package/dist/frameworks/vitest.js +11 -6
  31. package/dist/imported-values.js +12 -3
  32. package/dist/index.d.ts +2 -0
  33. package/dist/index.js +1 -0
  34. package/dist/model.d.ts +4 -1
  35. package/dist/node-scripts.d.ts +15 -1
  36. package/dist/node-scripts.js +140 -13
  37. package/dist/parse.d.ts +3 -0
  38. package/dist/parse.js +33 -0
  39. package/dist/records.js +7 -4
  40. package/dist/resolve.d.ts +3 -1
  41. package/dist/resolve.js +48 -15
  42. package/dist/run-commands.d.ts +20 -0
  43. package/dist/run-commands.js +107 -0
  44. package/dist/scan.js +15 -5
  45. package/dist/types.d.ts +9 -1
  46. package/dist/values.d.ts +25 -4
  47. package/dist/values.js +131 -22
  48. package/package.json +15 -7
  49. package/schema/diff-v1.json +12 -1
  50. package/schema/v1.json +9 -1
package/dist/config.js CHANGED
@@ -8,8 +8,9 @@ import { folders } from "./framework-version.js";
8
8
  import { commonJsExport, exportName, moduleExportsValue, ownExport, parseModule, requiredModule, starReexport, } from "./frameworks/bindings.js";
9
9
  import { atLeast, problem } from "./frameworks/common.js";
10
10
  import { parseFile } from "./parse.js";
11
+ import { findFile } from "./resolve.js";
11
12
  import { snippet } from "./source.js";
12
- import { isKey, isScopeNode, Values } from "./values.js";
13
+ import { assignedNames, bindingNames, isKey, isScopeNode, Values } from "./values.js";
13
14
  import { VITEST_INCLUDE, VITEST_TYPECHECK_INCLUDE, vitestExclude } from "./vitest-defaults.js";
14
15
  import { readToml, TomlError } from "./toml.js";
15
16
  import { readYaml, YamlError } from "./yaml.js";
@@ -84,6 +85,21 @@ function changesIn(parsed, values) {
84
85
  }
85
86
  /** The functions configs are written with, which return the config they are given or merge the ones they are given. */
86
87
  const CONFIG_FUNCTIONS = new Set(['defineConfig', 'defineProject', 'mergeConfig']);
88
+ /** The modules whose default export is next/jest's `nextJest()`. */
89
+ const NEXT_JEST = new Set(['next/jest', 'next/jest.js']);
90
+ /**
91
+ * The mocks next/jest maps imports to before the custom config's `moduleNameMapper`: files of the `next` package, which
92
+ * dependencies leave out as they do any installed package.
93
+ */
94
+ const NEXT_JEST_MOCKS = {
95
+ '^.+\\.module\\.(css|sass|scss)$': 'next/dist/build/jest/object-proxy.js',
96
+ '^.+\\.(css|sass|scss)$': 'next/dist/build/jest/__mocks__/styleMock.js',
97
+ '^.+\\.(png|jpg|jpeg|gif|webp|avif|ico|bmp)$': 'next/dist/build/jest/__mocks__/fileMock.js',
98
+ '^.+\\.(svg)$': 'next/dist/build/jest/__mocks__/fileMock.js',
99
+ '@next/font/(.*)': 'next/dist/build/jest/__mocks__/nextFontMock.js',
100
+ 'next/font/(.*)': 'next/dist/build/jest/__mocks__/nextFontMock.js',
101
+ '^server-only$': 'next/dist/build/jest/__mocks__/empty.js',
102
+ };
87
103
  /**
88
104
  * Node.js path functions a config builds its folders with, given the folder the runner starts in: the root, which
89
105
  * `path.resolve()` resolves a relative path from.
@@ -106,6 +122,7 @@ function attempt(read) {
106
122
  function unknown(where) {
107
123
  return { kind: 'unknown', where };
108
124
  }
125
+ const UNDEFINED = { kind: 'value', value: undefined };
109
126
  function emptyObject() {
110
127
  return { kind: 'object', keys: new Map(), rest: null };
111
128
  }
@@ -152,6 +169,30 @@ function spread(base, over, at) {
152
169
  return { kind: 'object', keys: new Map(over.keys), rest: over.rest };
153
170
  return { kind: 'object', keys: new Map([...base.keys, ...over.keys]), rest: base.rest };
154
171
  }
172
+ /**
173
+ * The config next/jest's `createJestConfig()` gives Jest for a custom one: the custom config, with next/jest's mocks
174
+ * before its `moduleNameMapper`, which can replace them, and `/node_modules/` and `/.next/` before its
175
+ * `testPathIgnorePatterns`. `at` is the call.
176
+ */
177
+ function withNextJest(custom, at) {
178
+ const config = spread(emptyObject(), custom, at);
179
+ const keys = new Map(config.keys);
180
+ const mocks = fromValue(NEXT_JEST_MOCKS);
181
+ const mapper = child(config, 'moduleNameMapper');
182
+ // A spread in the custom mapper the scan can't work out stays its unknown rest, as in any mapper, and the mocks stay
183
+ // too: it replaces one only by setting the same pattern. A whole mapper it can't work out may map any import.
184
+ const merged = mapper?.kind === 'object'
185
+ ? { kind: 'object', keys: new Map([...mocks.keys, ...mapper.keys]), rest: mapper.rest }
186
+ : unknown(at);
187
+ keys.set('moduleNameMapper', mapper === null ? mocks : merged);
188
+ const ignored = child(config, 'testPathIgnorePatterns');
189
+ const own = ignored === null ? [] : ignored.kind === 'list' ? ignored.items : [unknown(at)];
190
+ keys.set('testPathIgnorePatterns', {
191
+ kind: 'list',
192
+ items: [fromValue('/node_modules/'), fromValue('/.next/'), ...own],
193
+ });
194
+ return { ...config, keys };
195
+ }
155
196
  /** `{ ...a, ...b }` of two objects either of which may be unset, as Playwright merges `use` and `expect`. */
156
197
  function assign(a, b, at) {
157
198
  return [a, b].reduce((merged, over) => (over ? spread(merged, over, at) : merged), emptyObject());
@@ -303,6 +344,8 @@ class ConfigCode {
303
344
  done = new Map();
304
345
  /** Constants and exports being worked out, to stop at one that refers back to itself. */
305
346
  pending = new Set();
347
+ /** The config functions being read at a call, innermost last. */
348
+ calls = [];
306
349
  constructor(main, framework, root, version) {
307
350
  this.main = main;
308
351
  this.framework = framework;
@@ -379,6 +422,16 @@ class ConfigCode {
379
422
  }
380
423
  break;
381
424
  }
425
+ case 'LogicalExpression': {
426
+ // As a config function writes an optional argument, `options ?? {}` or `folder || './e2e'`.
427
+ const left = this.evaluate(expression.left, frame, scopes);
428
+ if (left.kind === 'unknown')
429
+ break;
430
+ const isFalsy = left.kind === 'value' && !left.value;
431
+ const { operator } = expression;
432
+ const takesRight = operator === '??' ? isNullish(left) : operator === '||' ? isFalsy : !isFalsy;
433
+ return takesRight ? this.evaluate(expression.right, frame, scopes) : left;
434
+ }
382
435
  case 'TemplateLiteral': {
383
436
  const values = expression.expressions.map((item) => this.evaluate(item, frame, scopes));
384
437
  if (values.every((item) => item.kind === 'value')) {
@@ -429,8 +482,14 @@ class ConfigCode {
429
482
  named(name, frame, scopes, site) {
430
483
  const constant = frame.values.constantOf(name, scopes);
431
484
  const declaration = frame.values.declarationOf(name, scopes);
485
+ // A function declares its own parameters.
486
+ const parameter = this.calls.find((call) => call.fn === declaration)?.parameters.get(name);
487
+ if (parameter)
488
+ return parameter;
432
489
  if (constant) {
433
- const setting = this.once(constant.init, site, () => this.evaluate(constant.init, frame, constant.scopes));
490
+ const call = this.calls.findLast((item) => constant.scopes.includes(item.fn));
491
+ const read = () => this.evaluate(constant.init, frame, constant.scopes);
492
+ const setting = this.once(constant.init, site, read, call?.done);
434
493
  return this.changed(setting, frame, declaration);
435
494
  }
436
495
  // An import, or a local file loaded with `require()`, which reads as one.
@@ -506,15 +565,15 @@ class ConfigCode {
506
565
  return unknown(site);
507
566
  return this.once(`${module.relativeFilePath}\0${name}`, site, () => this.exported(module, name, site));
508
567
  }
509
- once(key, site, read) {
510
- if (this.done.has(key))
511
- return this.done.get(key);
568
+ once(key, site, read, done = this.done) {
569
+ if (done.has(key))
570
+ return done.get(key);
512
571
  if (this.pending.has(key))
513
572
  return unknown(site);
514
573
  this.pending.add(key);
515
574
  const result = read();
516
575
  this.pending.delete(key);
517
- this.done.set(key, result);
576
+ done.set(key, result);
518
577
  return result;
519
578
  }
520
579
  /** A module a config file imports, read once; null when it isn't a local file or can't be parsed. */
@@ -617,6 +676,33 @@ class ConfigCode {
617
676
  ? values.map((value) => value.value)
618
677
  : null;
619
678
  }
679
+ /**
680
+ * The file `require.resolve('./global-setup')` or `import.meta.resolve('./global-setup.ts')` gives, found from the
681
+ * config's folder, as a path or a file URL; for `require.resolve()` of a package, its name, as a setting that names it
682
+ * directly does. Null when it can't be worked out or isn't there, and undefined for any other call.
683
+ */
684
+ resolvedPath(call, frame, scopes) {
685
+ const callee = unwrap(call.callee);
686
+ if (callee.type !== 'MemberExpression' || callee.computed || callee.property.name !== 'resolve')
687
+ return undefined;
688
+ const { object } = callee;
689
+ const isRequire = object.type === 'Identifier' &&
690
+ object.name === 'require' &&
691
+ frame.values.declarationOf('require', scopes) === null;
692
+ // `import.meta` is the only meta property with a `resolve()`.
693
+ const isImportMeta = object.type === 'MetaProperty';
694
+ if (!isRequire && !isImportMeta)
695
+ return undefined;
696
+ const name = this.strings(call.arguments.slice(0, 1), frame, scopes)?.[0];
697
+ if (name === undefined)
698
+ return null;
699
+ if (!/^\.\.?[\\/]/.test(name) && !path.isAbsolute(name))
700
+ return isRequire ? { kind: 'value', value: name } : null;
701
+ const file = findFile(path.resolve(path.dirname(frame.file), name));
702
+ if (file === null)
703
+ return null;
704
+ return { kind: 'value', value: isRequire ? file : pathToFileURL(file).href };
705
+ }
620
706
  /**
621
707
  * A call of `defineConfig()`, `defineProject()` or `mergeConfig()`, or of a Node.js path function such as
622
708
  * `path.join(__dirname, 'e2e')`; any other call can't be worked out.
@@ -629,6 +715,12 @@ class ConfigCode {
629
715
  if (required !== null && isGlobal && /^\.\.?\//.test(required)) {
630
716
  return this.fromModule(frame, required, 'default', site) ?? unknown(site);
631
717
  }
718
+ const resolved = this.resolvedPath(call, frame, scopes);
719
+ if (resolved !== undefined)
720
+ return resolved ?? unknown(site);
721
+ const custom = this.framework === 'jest' ? this.nextJestCustom(call, frame, scopes) : null;
722
+ if (custom)
723
+ return withNextJest(custom, site);
632
724
  const origin = this.origin(unwrap(call.callee), frame, scopes);
633
725
  const pathFunction = origin && PATH_FUNCTIONS[`${origin.source.replace(/^node:/, '')}.${origin.name}`];
634
726
  if (pathFunction) {
@@ -638,6 +730,14 @@ class ConfigCode {
638
730
  }
639
731
  const isPackage = origin !== null && (origin.source === '' || !/^[./#]/.test(origin.source));
640
732
  const name = isPackage && CONFIG_FUNCTIONS.has(origin.name) ? origin.name : null;
733
+ // A factory, as `createVitestConfig('node')`, gives what it returns, with its parameters bound to the call's
734
+ // arguments. The runner's own `defineConfig` is read as such, also from a workspace package, as Vite's own
735
+ // repository has. When none of the factory can be worked out, the call in the config is the place to point at.
736
+ const factory = name === null ? this.factory(unwrap(call.callee), frame, scopes) : null;
737
+ if (factory) {
738
+ const returned = this.called(factory, call, frame, scopes);
739
+ return returned.kind === 'unknown' ? unknown(site) : returned;
740
+ }
641
741
  const { arguments: given } = call;
642
742
  const needed = name === 'mergeConfig' ? 2 : 1;
643
743
  // Jest exports defineConfig() and mergeConfig() from 30.3; neither takes a project.
@@ -700,6 +800,136 @@ class ConfigCode {
700
800
  }
701
801
  return null;
702
802
  }
803
+ /**
804
+ * The custom config of next/jest's `createJestConfig(config)`, where `createJestConfig` is what `nextJest(options)`
805
+ * returns, or of `nextJest(options)(config)`; a function given is called, as next/jest calls it. Null for any other
806
+ * call.
807
+ */
808
+ nextJestCustom(call, frame, scopes) {
809
+ let callee = unwrap(call.callee);
810
+ let around = scopes;
811
+ if (callee.type === 'Identifier') {
812
+ const constant = frame.values.constantOf(callee.name, scopes);
813
+ if (!constant)
814
+ return null;
815
+ [callee, around] = [unwrap(constant.init), constant.scopes];
816
+ }
817
+ if (callee.type !== 'CallExpression' ||
818
+ !NEXT_JEST.has(this.wholeModule(unwrap(callee.callee), frame, around) ?? '')) {
819
+ return null;
820
+ }
821
+ const given = call.arguments.at(0);
822
+ if (given === undefined)
823
+ return emptyObject();
824
+ return given.type === 'SpreadElement' ? unknown({ frame, node: given }) : this.config(given, frame, scopes);
825
+ }
826
+ /**
827
+ * The package whose default export an expression is: `require('x')`, or a name declared with `require('x')` or
828
+ * imported with `import name from 'x'`; null for any other.
829
+ */
830
+ wholeModule(node, frame, scopes) {
831
+ if (node.type !== 'Identifier')
832
+ return requiredModule(node);
833
+ const constant = frame.values.constantOf(node.name, scopes);
834
+ if (constant)
835
+ return requiredModule(constant.init);
836
+ return frame.values.importedName(node.name, scopes) === 'default'
837
+ ? frame.values.importSource(node.name, scopes)
838
+ : null;
839
+ }
840
+ /**
841
+ * The function a config calls to build its settings: one it declares, or one a local file or a workspace package
842
+ * exports; null for any other, such as one from an installed package.
843
+ */
844
+ factory(callee, frame, scopes) {
845
+ if (callee.type !== 'Identifier')
846
+ return null;
847
+ const imported = frame.values.importedName(callee.name, scopes);
848
+ if (imported === null) {
849
+ const declared = frame.values.functionWithScopes(callee.name, scopes);
850
+ return declared && { ...declared, frame };
851
+ }
852
+ const module = this.open(frame, frame.values.importSource(callee.name, scopes));
853
+ return module && this.exportedFunction(module, imported, new Set([frame, module]));
854
+ }
855
+ /**
856
+ * The function a module exports under a name, written in the module or passed on from another; null for anything
857
+ * else. `seen` holds the modules followed so far, to stop at two that pass the name on to each other.
858
+ */
859
+ exportedFunction(frame, name, seen) {
860
+ const { program } = frame.parsed;
861
+ const own = ownExport(program, name);
862
+ if (own?.kind === 'specifier' && own.source !== null) {
863
+ const module = this.open(frame, own.source);
864
+ if (!module || seen.has(module))
865
+ return null;
866
+ return this.exportedFunction(module, own.local, new Set([...seen, module]));
867
+ }
868
+ const written = own === null ? commonJsExport(program, name) : own.kind === 'default' ? own.value : null;
869
+ const node = written && unwrap(written);
870
+ if (node?.type === 'FunctionDeclaration' || isFunctionNode(node))
871
+ return { fn: node, frame, scopes: [program] };
872
+ const local = own?.kind === 'variable' || own?.kind === 'function'
873
+ ? name
874
+ : own?.kind === 'specifier'
875
+ ? own.local
876
+ : node?.type === 'Identifier'
877
+ ? node.name
878
+ : null;
879
+ const declared = local === null ? null : frame.values.functionWithScopes(local, [program]);
880
+ return declared && { ...declared, frame };
881
+ }
882
+ /**
883
+ * What a call of a config function returns, with its parameters bound to the call's arguments. Unknown for an async
884
+ * function, which returns a promise, one that calls itself, or a call with a spread argument.
885
+ */
886
+ called(factory, call, frame, scopes) {
887
+ const { fn } = factory;
888
+ const isSpread = call.arguments.some((arg) => arg.type === 'SpreadElement');
889
+ if (fn.async || isSpread || this.calls.some((item) => item.fn === fn))
890
+ return unknown({ frame, node: call });
891
+ const args = call.arguments.map((arg) => this.evaluate(arg, frame, scopes));
892
+ const parameters = new Map();
893
+ // The call is read from here on, as the defaults of parameters can use the ones before them.
894
+ this.calls.push({ fn, parameters, done: new Map() });
895
+ const inside = [...factory.scopes, fn];
896
+ for (const [index, parameter] of fn.params.entries()) {
897
+ this.bind(parameter, args.at(index) ?? UNDEFINED, parameters, factory.frame, inside);
898
+ }
899
+ // A parameter the function assigns to may hold another value by the time it is read.
900
+ for (const name of assignedNames(fn.body)) {
901
+ if (parameters.has(name))
902
+ parameters.set(name, unknown({ frame: factory.frame, node: fn }));
903
+ }
904
+ const setting = this.returned(fn, factory.frame, factory.scopes);
905
+ this.calls.pop();
906
+ return setting;
907
+ }
908
+ /** Binds the names of a parameter to the parts of the setting it takes, as destructuring does. */
909
+ bind(pattern, value, names, frame, scopes) {
910
+ switch (pattern.type) {
911
+ case 'Identifier':
912
+ names.set(pattern.name, value);
913
+ return;
914
+ case 'AssignmentPattern': {
915
+ // A default takes the place of `undefined` only, so a value the scan can't work out stays unknown.
916
+ const isUndefined = value.kind === 'value' && value.value === undefined;
917
+ this.bind(pattern.left, isUndefined ? this.evaluate(pattern.right, frame, scopes) : value, names, frame, scopes);
918
+ return;
919
+ }
920
+ case 'ObjectPattern':
921
+ for (const property of pattern.properties) {
922
+ // The scan doesn't work out what a rest element or a computed key takes.
923
+ const key = property.type === 'Property' ? propertyKey(property) : null;
924
+ const part = key === null ? unknown({ frame, node: property }) : (child(value, key) ?? UNDEFINED);
925
+ this.bind(property.type === 'Property' ? property.value : property.argument, part, names, frame, scopes);
926
+ }
927
+ return;
928
+ default:
929
+ for (const name of bindingNames(pattern))
930
+ names.set(name, unknown({ frame, node: pattern }));
931
+ }
932
+ }
703
933
  /** What a config function returns, when it has a single `return` the scan can follow. */
704
934
  returned(fn, frame, scopes) {
705
935
  const values = returnValues(fn, true);
@@ -718,7 +948,7 @@ export class Configs {
718
948
  readerFor;
719
949
  inFolders = new Map();
720
950
  files = new Map();
721
- mocha;
951
+ mocha = new Map();
722
952
  used = new Set();
723
953
  reported = new Set();
724
954
  version;
@@ -769,9 +999,9 @@ export class Configs {
769
999
  });
770
1000
  }
771
1001
  /** Reports a setting the scan reads but can't work out, once; `key` names it, and `effect` says what the scan does. */
772
- reportUnknown(setting, key, effect) {
1002
+ reportUnknown(setting, key, effect, level = 'warning') {
773
1003
  const message = `The config's ${key} could not be worked out without running the config, so ${effect}.`;
774
- this.reportAt(setting.where, key, message);
1004
+ this.reportAt(setting.where, key, message, level);
775
1005
  }
776
1006
  /**
777
1007
  * Reports, once, a setting the runner mishandles, where it is written, or on its config when the scan built it from
@@ -783,7 +1013,7 @@ export class Configs {
783
1013
  else
784
1014
  this.reportConfig(config, message);
785
1015
  }
786
- reportAt(where, key, message) {
1016
+ reportAt(where, key, message, level = 'warning') {
787
1017
  if (where === null)
788
1018
  return;
789
1019
  const { frame, node } = where;
@@ -792,7 +1022,7 @@ export class Configs {
792
1022
  return;
793
1023
  this.reported.add(id);
794
1024
  const code = snippet(frame.parsed.source.text.slice(node.start, node.end));
795
- const found = problem('config-value-unknown', node, message, code);
1025
+ const found = { ...problem('config-value-unknown', node, message, code), level };
796
1026
  this.diagnostics.push(toDiagnostic(found, frame.relativeFilePath, frame.parsed.source));
797
1027
  }
798
1028
  /** Reports, once, a config the runner reads in a way the scan doesn't follow, or refuses. */
@@ -808,26 +1038,30 @@ export class Configs {
808
1038
  this.diagnostics.push(fileDiagnostic('config-value-unknown', relativeFilePath, message));
809
1039
  }
810
1040
  /**
811
- * The config Mocha runs with, started in the root: the first `.mocharc` file from the root up, joined with the `mocha`
812
- * key of the nearest package.json. Null without either.
1041
+ * The config Mocha runs with, started in a folder, the root by default: the first `.mocharc` file from there up, or
1042
+ * the file `--config` names, joined with the `mocha` key of the nearest package.json, or of the one `--package`
1043
+ * names. `false` stands for `--no-config` or `--no-package`, which leave that part out. Null without either.
813
1044
  */
814
- mochaConfig() {
815
- if (this.mocha !== undefined)
816
- return this.mocha;
817
- let rc = null;
818
- let manifest = null;
819
- for (const folder of folders(this.root)) {
820
- const name = rc ? undefined : CONFIG_FILES.mocha.find((item) => existsSync(path.join(folder, item)));
1045
+ mochaConfig(folder = this.root, rcFile, manifestFile) {
1046
+ const id = `${folder}\0${rcFile}\0${manifestFile}`;
1047
+ if (this.mocha.has(id))
1048
+ return this.mocha.get(id);
1049
+ let rc = rcFile ? this.at(rcFile) : null;
1050
+ let manifest = typeof manifestFile === 'string' ? manifestFile : null;
1051
+ for (const above of rcFile === undefined || manifestFile === undefined ? folders(folder) : []) {
1052
+ const name = rc || rcFile !== undefined ? undefined : CONFIG_FILES.mocha.find((item) => existsSync(path.join(above, item)));
821
1053
  if (name)
822
- rc = this.at(path.join(folder, name));
823
- if (manifest === null && existsSync(path.join(folder, 'package.json')))
824
- manifest = path.join(folder, 'package.json');
825
- if (rc && manifest)
1054
+ rc = this.at(path.join(above, name));
1055
+ if (manifest === null && manifestFile === undefined && existsSync(path.join(above, 'package.json'))) {
1056
+ manifest = path.join(above, 'package.json');
1057
+ }
1058
+ if ((rc || rcFile !== undefined) && (manifest || manifestFile !== undefined))
826
1059
  break;
827
1060
  }
828
1061
  const key = manifest && manifestKey(manifest, 'mocha') !== undefined ? this.at(manifest) : null;
829
- this.mocha = rc && key ? joinMochaConfigs(rc, key) : (rc ?? key);
830
- return this.mocha;
1062
+ const config = rc && key ? joinMochaConfigs(rc, key) : (rc ?? key);
1063
+ this.mocha.set(id, config);
1064
+ return config;
831
1065
  }
832
1066
  /**
833
1067
  * The config the jasmine command reads, started in the root: the first of `spec/support/jasmine.mjs` (from Jasmine
@@ -126,7 +126,8 @@ export interface FixtureNeeds {
126
126
  export interface Dependencies {
127
127
  hash: string;
128
128
  files: string[];
129
- isComplete: boolean;
129
+ /** The file and line of each place the test reaches that can't be followed, sorted. */
130
+ unfollowed: string[];
130
131
  }
131
132
  /**
132
133
  * Follows what each test runs, across the files of one scan: its call, the code around it, and every declaration
@@ -240,6 +241,13 @@ export declare class DependencyGraph {
240
241
  * function takes them whole, or the value can't be told, as a factory's result or a fixture from a package.
241
242
  */
242
243
  private fixtureUses;
244
+ /**
245
+ * The path or URL an expression builds from literals, its module's folder and Node.js path and URL functions, as
246
+ * `join(import.meta.dirname, 'fixtures')`, with the module's file placed in `PROJECT`; null for anything else.
247
+ */
248
+ private builtPath;
249
+ /** The Node.js path or URL function a callee names, as `path.join` for `join` imported from `node:path`; or null. */
250
+ private nodeFunction;
243
251
  /**
244
252
  * What an expression holds, followed through constants and imports to the function or value they are bound to; the
245
253
  * expression itself when it is something else, with the module and scopes to read it in.
@@ -210,6 +210,35 @@ const DECLARATION_FILE = /\.d\.[cm]?ts$/;
210
210
  */
211
211
  const IN_PROGRESS = [];
212
212
  const COMPUTED = "The module this loads is worked out at run time, so dependencyHash doesn't cover it.";
213
+ /**
214
+ * Where a module's file is placed while the path it loads is worked out, as `path.join(__dirname, 'fixtures')`: a
215
+ * folder of its own, so that the path comes out the same on every system and one outside the project stands out.
216
+ */
217
+ const PROJECT = '/__project__';
218
+ /** The path of a file URL; null for any other text. */
219
+ function fileUrlPath(url) {
220
+ const parsed = URL.parse(url);
221
+ return parsed?.protocol === 'file:' ? decodeURIComponent(parsed.pathname) : null;
222
+ }
223
+ /** Node.js path and URL functions a module builds the path it loads with; null when the result depends on the run. */
224
+ const PATH_FUNCTIONS = {
225
+ 'path.join': (...parts) => path.posix.join(...parts),
226
+ // Without an absolute part, the path is resolved from the folder the run starts in.
227
+ 'path.resolve': (...parts) => parts.some((part) => path.posix.isAbsolute(part)) ? path.posix.resolve(...parts) : null,
228
+ 'path.dirname': (part) => path.posix.dirname(part),
229
+ 'url.fileURLToPath': (url) => fileUrlPath(url),
230
+ 'url.pathToFileURL': (file) => (path.posix.isAbsolute(file) ? new URL(`file://${file}`).href : null),
231
+ };
232
+ /** The specifier a built path or file URL stands for from a module; null for one outside the project or a bare name. */
233
+ function specifierFrom(module, built) {
234
+ const file = built.startsWith('file:') ? decodeURIComponent(new URL(built).pathname) : built;
235
+ if (/^\.\.?\//.test(file))
236
+ return file;
237
+ if (!file.startsWith(`${PROJECT}/`))
238
+ return null;
239
+ const relative = path.posix.relative(path.posix.dirname(path.posix.join(PROJECT, module.relativeFilePath)), file);
240
+ return /^\.\.?\//.test(relative) ? relative : `./${relative}`;
241
+ }
213
242
  function isLocalSpecifier(specifier) {
214
243
  return /^\.\.?(?:\/|$)/.test(specifier) || specifier.startsWith('#');
215
244
  }
@@ -483,7 +512,9 @@ export class DependencyGraph {
483
512
  const own = added.get(file);
484
513
  return own ? part(file, [...(shared.byFile.get(file) ?? []), ...own]) : shared.parts.get(file);
485
514
  });
486
- return { hash: shortHash(`[${parts.join(',')}]`), files, isComplete: places.length === 0 };
515
+ // Places on one line, as two imports a statement makes, are one entry.
516
+ const unfollowed = places.map((place) => `${place.relativeFilePath}:${place.source.position(start(place)).line}`);
517
+ return { hash: shortHash(`[${parts.join(',')}]`), files, unfollowed: [...new Set(unfollowed)] };
487
518
  }
488
519
  newModule(relativeFilePath, parsed, text, values, readModule) {
489
520
  return {
@@ -1048,6 +1079,78 @@ export class DependencyGraph {
1048
1079
  }
1049
1080
  return isPlainValue(node) ? [] : null;
1050
1081
  }
1082
+ /**
1083
+ * The path or URL an expression builds from literals, its module's folder and Node.js path and URL functions, as
1084
+ * `join(import.meta.dirname, 'fixtures')`, with the module's file placed in `PROJECT`; null for anything else.
1085
+ */
1086
+ builtPath(module, value, scopes, types, depth = 0) {
1087
+ if (depth > MAX_VALUE_DEPTH)
1088
+ return null;
1089
+ const { module: at, node, scopes: where } = this.valueOf(module, value, scopes, types);
1090
+ const of = (part) => part.type === 'SpreadElement' ? null : this.builtPath(at, part, where, types, depth + 1);
1091
+ const file = path.posix.join(PROJECT, at.relativeFilePath);
1092
+ switch (node.type) {
1093
+ case 'Literal':
1094
+ return typeof node.value === 'string' ? node.value : null;
1095
+ case 'TemplateLiteral': {
1096
+ const parts = node.expressions.map(of);
1097
+ if (parts.includes(null))
1098
+ return null;
1099
+ return node.quasis.map((quasi, index) => quasi.value.cooked + (parts[index] ?? '')).join('');
1100
+ }
1101
+ case 'BinaryExpression': {
1102
+ const [left, right] = node.operator === '+' ? [of(node.left), of(node.right)] : [null, null];
1103
+ return left === null || right === null ? null : left + right;
1104
+ }
1105
+ case 'Identifier':
1106
+ // CommonJS gives every module these, and Vitest and Jest give them to ES modules as well.
1107
+ if (at.values.declarationOf(node.name, where) !== null)
1108
+ return null;
1109
+ return node.name === '__dirname' ? path.posix.dirname(file) : node.name === '__filename' ? file : null;
1110
+ case 'MemberExpression': {
1111
+ if (node.computed)
1112
+ return null;
1113
+ const { name } = node.property;
1114
+ if (node.object.type === 'MetaProperty') {
1115
+ if (name === 'dirname')
1116
+ return path.posix.dirname(file);
1117
+ if (name === 'filename')
1118
+ return file;
1119
+ return name === 'url' ? new URL(`file://${file}`).href : null;
1120
+ }
1121
+ // A URL's `href`, and the path of a file URL's `pathname`.
1122
+ const url = of(node.object);
1123
+ if (url === null)
1124
+ return null;
1125
+ return name === 'href' ? url : name === 'pathname' ? fileUrlPath(url) : null;
1126
+ }
1127
+ case 'NewExpression': {
1128
+ const isUrl = node.callee.type === 'Identifier' && node.callee.name === 'URL' && !at.values.declarationOf('URL', where);
1129
+ const parts = isUrl ? node.arguments.map(of) : [null];
1130
+ return parts.includes(null) ? null : (URL.parse(...parts)?.href ?? null);
1131
+ }
1132
+ case 'CallExpression': {
1133
+ const name = this.nodeFunction(at, node.callee, where);
1134
+ const parts = name === null ? [null] : node.arguments.map(of);
1135
+ return parts.includes(null) ? null : (PATH_FUNCTIONS[name]?.(...parts) ?? null);
1136
+ }
1137
+ }
1138
+ return null;
1139
+ }
1140
+ /** The Node.js path or URL function a callee names, as `path.join` for `join` imported from `node:path`; or null. */
1141
+ nodeFunction(module, callee, scopes) {
1142
+ const target = unwrap(callee);
1143
+ const isMember = target.type === 'MemberExpression' && !target.computed && target.object.type === 'Identifier';
1144
+ const local = target.type === 'Identifier' ? target.name : isMember ? target.object.name : null;
1145
+ const library = local === null ? null : module.values.importSource(local, scopes)?.replace(/^node:/, '');
1146
+ if (local === null || (library !== 'path' && library !== 'url'))
1147
+ return null;
1148
+ const imported = module.values.importedName(local, scopes);
1149
+ // `import path from 'node:path'`, `import * as path` and `const path = require('path')` name the whole module.
1150
+ const isModule = imported === null || imported === 'default';
1151
+ const name = !isMember ? imported : isModule ? target.property.name : null;
1152
+ return name === null ? null : `${library}.${name}`;
1153
+ }
1051
1154
  /**
1052
1155
  * What an expression holds, followed through constants and imports to the function or value they are bound to; the
1053
1156
  * expression itself when it is something else, with the module and scopes to read it in.
@@ -1075,8 +1178,13 @@ export class DependencyGraph {
1075
1178
  return { module, node, scopes };
1076
1179
  }
1077
1180
  importOf(module, specifier) {
1078
- const program = module.parsed.program;
1079
- return program.body.find((statement) => statement.type === 'ImportDeclaration' && contains(statement, specifier));
1181
+ // An import is also written inside a TypeScript `declare module` block, or a block where a loader such as reify
1182
+ // allows one, so the search goes down to the specifier.
1183
+ let node = module.parsed.program;
1184
+ while (node.type !== 'ImportDeclaration') {
1185
+ node = childNodes(node).find((child) => contains(child, specifier));
1186
+ }
1187
+ return node;
1080
1188
  }
1081
1189
  /** The units a unit uses directly: the declarations its names refer to, and the modules it loads. */
1082
1190
  edgesOf(unit) {
@@ -1095,17 +1203,24 @@ export class DependencyGraph {
1095
1203
  return;
1096
1204
  add(this.toDeclaration(module, declaration, name, member, types));
1097
1205
  };
1098
- /** A module loaded by the path in `argument`, or a place when the path is worked out at run time. */
1099
- const loadsByPath = (argument, at) => {
1206
+ /**
1207
+ * A module loaded by the path in `argument`, also one built from literals and the file's folder, or a place when
1208
+ * the path is worked out at run time.
1209
+ */
1210
+ const loadsByPath = (argument, scopes, at) => {
1100
1211
  // `vi.mock(import('./broker'))`: the walk reads the import() itself.
1101
1212
  if (argument?.type === 'ImportExpression')
1102
1213
  return;
1103
- if (argument?.type === 'Literal' && typeof argument.value === 'string') {
1104
- add(this.imported(module, argument.value, '*', types, at));
1105
- }
1106
- else {
1214
+ const built = argument && argument.type !== 'SpreadElement' ? this.builtPath(module, argument, scopes, types) : null;
1215
+ const specifier = argument?.type === 'Literal' && typeof argument.value === 'string'
1216
+ ? argument.value
1217
+ : built === null
1218
+ ? null
1219
+ : specifierFrom(module, built);
1220
+ if (specifier === null)
1107
1221
  edges.add(this.placeUnit(module, at, COMPUTED));
1108
- }
1222
+ else
1223
+ add(this.imported(module, specifier, '*', types, at));
1109
1224
  };
1110
1225
  /** The fixtures a call's functions ask for, when it registers a hook, a condition, a test or `use()` values. */
1111
1226
  const asks = (call, scopes) => {
@@ -1200,13 +1315,13 @@ export class DependencyGraph {
1200
1315
  break;
1201
1316
  }
1202
1317
  case 'ImportExpression':
1203
- loadsByPath(node.source, node);
1318
+ loadsByPath(node.source, scopes, node);
1204
1319
  break;
1205
1320
  case 'CallExpression': {
1206
1321
  const { callee } = node;
1207
1322
  asks(node, scopes);
1208
1323
  if (callee.type === 'Identifier' && callee.name === 'require')
1209
- loadsByPath(node.arguments.at(0), node);
1324
+ loadsByPath(node.arguments.at(0), scopes, node);
1210
1325
  if (callee.type === 'MemberExpression' && !callee.computed) {
1211
1326
  const { object, property } = callee;
1212
1327
  if (SNAPSHOT_MATCHERS.has(property.name))
@@ -1224,7 +1339,7 @@ export class DependencyGraph {
1224
1339
  unit.snapshots.push({ matcher: target, name: null, isParts: false });
1225
1340
  }
1226
1341
  if (object.type === 'Identifier' && object.name === 'vi' && VI_MODULE_CALLS.has(property.name)) {
1227
- loadsByPath(node.arguments.at(0), node);
1342
+ loadsByPath(node.arguments.at(0), scopes, node);
1228
1343
  }
1229
1344
  if (object.type === 'MetaProperty' && property.name.startsWith('glob')) {
1230
1345
  edges.add(this.placeUnit(module, node, "The modules this loads match a pattern, so dependencyHash doesn't cover them."));