test-inventory 0.8.0 → 0.9.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 (61) hide show
  1. package/CHANGELOG.md +229 -0
  2. package/README.md +16 -11
  3. package/dist/case-titles.d.ts +9 -0
  4. package/dist/case-titles.js +82 -0
  5. package/dist/cli.d.ts +1 -1
  6. package/dist/cli.js +4 -2
  7. package/dist/config-dependencies.js +19 -0
  8. package/dist/config-runs.d.ts +95 -3
  9. package/dist/config-runs.js +784 -16
  10. package/dist/config.d.ts +24 -0
  11. package/dist/config.js +139 -19
  12. package/dist/diagnostics.js +14 -1
  13. package/dist/extract.d.ts +2 -0
  14. package/dist/extract.js +36 -10
  15. package/dist/files.d.ts +3 -2
  16. package/dist/files.js +52 -8
  17. package/dist/filter.js +1 -0
  18. package/dist/framework-version.d.ts +4 -2
  19. package/dist/framework-version.js +170 -15
  20. package/dist/frameworks/bun.d.ts +44 -0
  21. package/dist/frameworks/bun.js +280 -0
  22. package/dist/frameworks/common.d.ts +3 -1
  23. package/dist/frameworks/common.js +11 -9
  24. package/dist/frameworks/cypress.d.ts +31 -0
  25. package/dist/frameworks/cypress.js +70 -0
  26. package/dist/frameworks/jasmine.d.ts +29 -0
  27. package/dist/frameworks/jasmine.js +228 -0
  28. package/dist/frameworks/jest.js +1 -1
  29. package/dist/frameworks/mocha.d.ts +50 -0
  30. package/dist/frameworks/mocha.js +307 -0
  31. package/dist/frameworks/node.d.ts +30 -0
  32. package/dist/frameworks/node.js +271 -0
  33. package/dist/frameworks/playwright.d.ts +18 -0
  34. package/dist/frameworks/playwright.js +84 -0
  35. package/dist/frameworks/test-functions.d.ts +26 -1
  36. package/dist/frameworks/test-functions.js +73 -22
  37. package/dist/frameworks/vitest.d.ts +9 -1
  38. package/dist/frameworks/vitest.js +36 -5
  39. package/dist/frameworks/webdriverio.d.ts +27 -0
  40. package/dist/frameworks/webdriverio.js +37 -0
  41. package/dist/model.d.ts +4 -0
  42. package/dist/node-scripts.d.ts +11 -0
  43. package/dist/node-scripts.js +136 -0
  44. package/dist/options.d.ts +13 -4
  45. package/dist/options.js +22 -3
  46. package/dist/records.d.ts +10 -2
  47. package/dist/records.js +83 -16
  48. package/dist/resolve.d.ts +27 -4
  49. package/dist/resolve.js +64 -16
  50. package/dist/scan-file.d.ts +8 -0
  51. package/dist/scan-file.js +18 -2
  52. package/dist/scan.js +42 -7
  53. package/dist/toml.d.ts +13 -0
  54. package/dist/toml.js +179 -0
  55. package/dist/types.d.ts +19 -9
  56. package/dist/values.d.ts +5 -0
  57. package/dist/values.js +13 -0
  58. package/dist/yaml.d.ts +13 -0
  59. package/dist/yaml.js +287 -0
  60. package/package.json +13 -6
  61. package/schema/v1.json +26 -6
@@ -1,9 +1,12 @@
1
1
  import { existsSync, readFileSync, statSync } from 'node:fs';
2
2
  import path from 'node:path';
3
+ import { pathToFileURL } from 'node:url';
3
4
  import picomatch from 'picomatch';
4
5
  import { globSync } from 'tinyglobby';
5
6
  import { mergeDeep, settingAt, unknownText, } from "./config.js";
6
7
  import { atLeast } from "./frameworks/common.js";
8
+ import { nodeTestCommands, scriptsRunBy } from "./node-scripts.js";
9
+ import { durationOf } from "./frameworks/mocha.js";
7
10
  import { VITEST_BENCHMARK_INCLUDE, VITEST_INCLUDE, VITEST_TYPECHECK_INCLUDE, vitestExclude, } from "./vitest-defaults.js";
8
11
  const PLAYWRIGHT_TEST_MATCH = '**/*.@(spec|test).?(c|m)[jt]s?(x)';
9
12
  /** A file Vitest takes as a project config, by name. */
@@ -114,6 +117,9 @@ export class ConfigRuns {
114
117
  folderStates = new Map();
115
118
  /** The project Jest runs with when no config is found up to the root. */
116
119
  jestDefaults = null;
120
+ mochaDefaults = null;
121
+ nodeRuns = null;
122
+ bunFiles = null;
117
123
  /** The Vitest configs whose projects are being found, with their names, to stop at one whose projects name itself. */
118
124
  expanding = new Map();
119
125
  constructor(configs, framework, version, findsFiles) {
@@ -132,6 +138,18 @@ export class ConfigRuns {
132
138
  return { ...placement, tags: this.configTags(placement.config) };
133
139
  }
134
140
  place(file) {
141
+ if (this.framework === 'mocha')
142
+ return this.placeMocha(file);
143
+ if (this.framework === 'jasmine')
144
+ return this.placeJasmine(file);
145
+ if (this.framework === 'cypress')
146
+ return this.placeCypress(file);
147
+ if (this.framework === 'webdriverio')
148
+ return this.placeWebdriverIO(file);
149
+ if (this.framework === 'node')
150
+ return this.placeNode(file);
151
+ if (this.framework === 'bun')
152
+ return this.placeBun(file);
135
153
  // Jest started at the root reads the config there only, and the others through its `projects`.
136
154
  const rootConfig = this.framework === 'jest' ? this.configs.inFolder(this.configs.root) : null;
137
155
  const around = this.framework !== 'jest' ? this.configs.around(file) : rootConfig ? [rootConfig] : [];
@@ -207,6 +225,491 @@ export class ConfigRuns {
207
225
  const project = this.projectsOf(config).find((item) => item.runs && item.about.name === name);
208
226
  return project ? { about: project.about, runs: project.runs } : null;
209
227
  }
228
+ /** Mocha runs one set of files, from the config it starts with, or its defaults without one. */
229
+ placeMocha(file) {
230
+ const config = this.configs.mochaConfig();
231
+ const project = config
232
+ ? this.projectsOf(config)[0]
233
+ : (this.mochaDefaults ??= this.mochaProject(this.noConfig(), emptySettings()));
234
+ if (project.unknown) {
235
+ this.configs.reportUnknown(project.unknown.setting, project.unknown.key, this.findsFiles ? NOT_FOUND : ASSUMED);
236
+ return { config, settings: project.settings, runs: null, projects: null };
237
+ }
238
+ const runs = project.runs(file);
239
+ if (!config)
240
+ return { config: null, settings: null, runs, projects: null };
241
+ return { config, settings: project.settings, runs, projects: runs ? [project.about] : [] };
242
+ }
243
+ /**
244
+ * The files Mocha runs, as it looks them up from the root, the folder it starts in, wherever its config is: each
245
+ * `spec` entry a glob, a file, a file without its extension, or a folder whose files with one of the `extension`s it
246
+ * takes, also in folders below with `recursive`, less those `ignore` matches; and the `file` entries, loaded first.
247
+ * With globs, every file they match, less `ignore`. A timeout may be written as `'2s'`.
248
+ */
249
+ mochaProject(config, settings) {
250
+ const folder = this.configs.root;
251
+ const lists = {};
252
+ for (const [key, fallback] of [
253
+ ['spec', ['./test']],
254
+ ['extension', ['js', 'cjs', 'mjs']],
255
+ ['ignore', []],
256
+ ['file', []],
257
+ ]) {
258
+ const setting = settingAt(settings, [key]) ?? (key === 'ignore' ? settingAt(settings, ['exclude']) : null);
259
+ const found = setting === null || isUnset(setting) ? [...fallback] : patterns(setting, false);
260
+ if (!Array.isArray(found))
261
+ return this.unknownProject(config, found, key);
262
+ lists[key] = found.map((item) => (key === 'extension' ? item.replace(/^\./, '') : item));
263
+ }
264
+ const recursiveSetting = settingAt(settings, ['recursive']);
265
+ if (recursiveSetting?.kind === 'unknown')
266
+ return this.unknownProject(config, recursiveSetting, 'recursive');
267
+ const recursive = recursiveSetting?.kind === 'value' && recursiveSetting.value === true;
268
+ const extensions = lists.extension;
269
+ // Mocha matches `ignore` against the path as its lookup finds it, without `./`, so a pattern starting with `./`
270
+ // matches nothing; picomatch would drop the `./` and match.
271
+ const ignored = lists.ignore.map((glob) => (glob.startsWith('./') ? () => false : picomatch(posix(glob))));
272
+ const specs = lists.spec.map((spec) => mochaSpec(folder, spec, extensions, recursive));
273
+ // A `file` entry is loaded as written, before the specs, and `ignore` doesn't apply to it.
274
+ const loaded = new Set(lists.file.map((entry) => path.resolve(folder, entry)));
275
+ const duration = (key) => {
276
+ const setting = settingAt(settings, [key]);
277
+ const value = setting?.kind === 'value' ? durationOf(setting.value, key) : null;
278
+ return value === null ? setting : { kind: 'value', value };
279
+ };
280
+ const about = {
281
+ name: '',
282
+ testDir: folder,
283
+ dependencies: [],
284
+ settings,
285
+ root: folder,
286
+ container: null,
287
+ config,
288
+ grep: null,
289
+ grepRoot: folder,
290
+ runsByDefault: true,
291
+ defaults: { ...NO_DEFAULTS, timeout: duration('timeout'), retry: duration('retries') },
292
+ };
293
+ return {
294
+ config,
295
+ settings,
296
+ about,
297
+ unknown: null,
298
+ runs: (file) => {
299
+ const relative = posix(path.relative(folder, file));
300
+ if (loaded.has(file))
301
+ return 'test';
302
+ // A project's scripts often give Mocha the spec on its command line, which no config shows, so with globs the
303
+ // scan takes each file they match as run, less `ignore`.
304
+ if (this.findsFiles && !specs.some((spec) => spec(file)))
305
+ return false;
306
+ return ignored.some((isMatch) => isMatch(relative)) ? false : 'test';
307
+ },
308
+ };
309
+ }
310
+ /**
311
+ * Jasmine runs one set of files, from its config. Without one it runs none, unless it is given files: Karma and
312
+ * Angular projects give it theirs from their own configs, which the scan doesn't read, so their globs decide.
313
+ */
314
+ placeJasmine(file) {
315
+ const config = this.configs.jasmineConfig();
316
+ if (!config)
317
+ return { config: null, settings: null, runs: this.findsFiles ? false : 'test', projects: null };
318
+ const [project] = this.projectsOf(config);
319
+ if (project.unknown) {
320
+ this.configs.reportUnknown(project.unknown.setting, project.unknown.key, this.findsFiles ? NOT_FOUND : ASSUMED);
321
+ return { config, settings: project.settings, runs: null, projects: null };
322
+ }
323
+ const runs = project.runs(file);
324
+ return { config, settings: project.settings, runs, projects: runs ? [project.about] : [] };
325
+ }
326
+ /**
327
+ * The files Jasmine runs: those under `spec_dir`, from the root, that a `spec_files` glob matches, less those a glob
328
+ * starting with `!` matches. With globs, every file they match, as files given to Jasmine replace `spec_files`.
329
+ */
330
+ jasmineProject(config) {
331
+ const folder = this.configs.root;
332
+ const settings = config.settings;
333
+ const dirSetting = settingAt(settings, ['spec_dir']);
334
+ if (dirSetting?.kind === 'unknown')
335
+ return this.unknownProject(config, dirSetting, 'spec_dir');
336
+ const specDir = dirSetting?.kind === 'value' && typeof dirSetting.value === 'string' ? dirSetting.value : '';
337
+ const filesSetting = settingAt(settings, ['spec_files']);
338
+ const found = filesSetting === null || isUnset(filesSetting) ? [] : patterns(filesSetting, false);
339
+ if (!Array.isArray(found))
340
+ return this.unknownProject(config, found, 'spec_files');
341
+ const globs = found.map((glob) => ({ exclude: glob.startsWith('!'), glob: glob.replace(/^!/, '') }));
342
+ const matcher = (exclude) => {
343
+ const list = globs.filter((item) => item.exclude === exclude).map((item) => posix(path.join(specDir, item.glob)));
344
+ return list.length > 0 ? picomatch(list) : () => false;
345
+ };
346
+ const [included, excluded] = [matcher(false), matcher(true)];
347
+ const about = {
348
+ name: '',
349
+ testDir: path.resolve(folder, specDir),
350
+ dependencies: [],
351
+ settings,
352
+ root: folder,
353
+ container: null,
354
+ config,
355
+ grep: null,
356
+ grepRoot: folder,
357
+ runsByDefault: true,
358
+ defaults: NO_DEFAULTS,
359
+ };
360
+ return {
361
+ config,
362
+ settings,
363
+ about,
364
+ unknown: null,
365
+ runs: (file) => {
366
+ // Files given to Jasmine replace `spec_files`, its `!` globs included.
367
+ if (!this.findsFiles)
368
+ return 'test';
369
+ const relative = posix(path.relative(folder, file));
370
+ return included(relative) && !excluded(relative) ? 'test' : false;
371
+ },
372
+ };
373
+ }
374
+ /**
375
+ * Cypress runs a file in its e2e project, or in its component project when the config sets up component testing.
376
+ * Without a config it runs nothing, unless it is given files.
377
+ */
378
+ placeCypress(file) {
379
+ const config = this.configs.rootConfig();
380
+ if (!config)
381
+ return { config: null, settings: null, runs: this.findsFiles ? false : 'test', projects: null };
382
+ const projects = this.projectsOf(config);
383
+ const unknown = projects.find((project) => project.unknown);
384
+ if (unknown?.unknown) {
385
+ this.configs.reportUnknown(unknown.unknown.setting, unknown.unknown.key, this.findsFiles ? NOT_FOUND : ASSUMED);
386
+ return { config, settings: unknown.settings, runs: null, projects: null };
387
+ }
388
+ // Cypress runs a file given to it only when its testing type's `specPattern` takes the file too.
389
+ const running = projects.filter((project) => project.runs?.(file));
390
+ return {
391
+ config,
392
+ settings: config.settings,
393
+ runs: running.length > 0 ? 'test' : false,
394
+ projects: running.map((project) => project.about),
395
+ };
396
+ }
397
+ /**
398
+ * Cypress's testing types as projects: `e2e`, and `component` when the config has it. Each runs the files under the
399
+ * config's folder that its `specPattern` matches, dot files left out, without those its `excludeSpecPattern` matches,
400
+ * dot files included, and those in `node_modules` unless a `specPattern` names it; component testing also leaves out
401
+ * the files e2e's `specPattern` matches. `cypress run` matches each pattern against the whole path, as globby does.
402
+ */
403
+ cypressProjects(config) {
404
+ const folder = path.dirname(config.file);
405
+ const types = [
406
+ ['e2e', ['cypress/e2e/**/*.cy.{js,jsx,ts,tsx}'], ['*.hot-update.js']],
407
+ ];
408
+ if (settingAt(config.settings, ['component']) !== null) {
409
+ types.push(['component', ['**/*.cy.{js,jsx,ts,tsx}'], ['**/__snapshots__/*', '**/__image_snapshots__/*']]);
410
+ }
411
+ // Cypress takes a pattern written from the config's folder as one relative to it.
412
+ const relative = (glob) => posix(glob.startsWith(folder + path.sep) ? path.relative(folder, glob) : glob);
413
+ const projects = [];
414
+ let e2eSpecs = [];
415
+ for (const [type, defaultSpecs, defaultExcludes] of types) {
416
+ const lists = [];
417
+ for (const [key, fallback] of [
418
+ ['specPattern', defaultSpecs],
419
+ ['excludeSpecPattern', defaultExcludes],
420
+ ]) {
421
+ const setting = settingAt(config.settings, [type, key]);
422
+ const found = setting === null || isUnset(setting) ? fallback : patterns(setting, false);
423
+ if (!Array.isArray(found))
424
+ return [this.unknownProject(config, found, `${type}.${key}`)];
425
+ lists.push(found);
426
+ }
427
+ const [specs, excludes] = lists;
428
+ if (type === 'e2e')
429
+ e2eSpecs = specs;
430
+ // globby takes a `!` pattern among the specs as files to leave out, and a folder as every file below it; the
431
+ // excludes are fast-glob's `ignore`, which drops a `!` and skips everything in a folder it matches.
432
+ const below = (glob) => [glob, `${glob.replace(/\/+$/, '')}/**`];
433
+ const isFolder = (glob) => !picomatch.scan(glob).isGlob && statSync(path.resolve(folder, glob), { throwIfNoEntry: false })?.isDirectory();
434
+ const isSpec = picomatch(specs
435
+ .filter((glob) => !glob.startsWith('!'))
436
+ .map(relative)
437
+ .flatMap((glob) => (isFolder(glob) ? below(glob) : [glob])));
438
+ const excluded = [...excludes, ...(type === 'component' ? e2eSpecs : [])]
439
+ .map((glob) => relative(glob.replace(/^!/, '')))
440
+ .flatMap(below);
441
+ excluded.push(...specs.filter((glob) => glob.startsWith('!')).flatMap((glob) => below(relative(glob.slice(1)))));
442
+ if (!specs.some((glob) => glob.includes('node_modules')))
443
+ excluded.push('**/node_modules/**');
444
+ const isExcluded = picomatch(excluded, { dot: true });
445
+ const retry = settingAt(config.settings, [type, 'retries']) ?? settingAt(config.settings, ['retries']);
446
+ const written = retry?.kind === 'object' ? settingAt(retry, ['runMode']) : retry;
447
+ // Cypress takes `null` as no retries set.
448
+ const runMode = written?.kind === 'value' && written.value === null ? null : written;
449
+ const about = {
450
+ name: type,
451
+ testDir: folder,
452
+ dependencies: [],
453
+ settings: config.settings,
454
+ root: folder,
455
+ container: null,
456
+ config,
457
+ grep: null,
458
+ grepRoot: folder,
459
+ runsByDefault: true,
460
+ defaults: { ...NO_DEFAULTS, retry: runMode },
461
+ };
462
+ projects.push({
463
+ config,
464
+ settings: config.settings,
465
+ about,
466
+ unknown: null,
467
+ runs: (file) => {
468
+ const from = posix(path.relative(folder, file));
469
+ return isSpec(from) && !isExcluded(from) ? 'test' : false;
470
+ },
471
+ });
472
+ }
473
+ return projects;
474
+ }
475
+ /** `bun test` runs the files of `bunFilesOf`, with the settings of the `bunfig.toml` in the root, when there is one. */
476
+ placeBun(file) {
477
+ const config = this.configs.rootConfig();
478
+ if (!this.bunFiles) {
479
+ this.bunFiles = this.bunFilesOf(config);
480
+ if (this.bunFiles.refused)
481
+ this.configs.reportConfig(config, this.bunFiles.refused);
482
+ }
483
+ const found = this.bunFiles;
484
+ const runs = found.refused === null && (!this.findsFiles || found.runs(file)) ? 'test' : false;
485
+ if (!config)
486
+ return { config: null, settings: null, runs, projects: null };
487
+ const projects = runs ? [found.isConcurrent(file) ? found.concurrent : found.about] : [];
488
+ return { config, settings: config.settings, runs, projects };
489
+ }
490
+ /**
491
+ * The files `bun test` runs: those under `[test] root`, the root by default, whose name without its extension ends
492
+ * with `.test`, `_test`, `.spec` or `_spec`, with a JavaScript or TypeScript extension, outside dot folders and
493
+ * `node_modules`, and from Bun 1.3.11 outside the folders and files a `pathIgnorePatterns` glob matches. `[test] retry`
494
+ * applies to every test, and the tests of the files a `concurrentTestGlob` glob matches run at once.
495
+ */
496
+ bunFilesOf(config) {
497
+ const folder = this.configs.root;
498
+ const settings = config?.settings ?? emptySettings();
499
+ // Bun reads a string or a list of them, and ignores a `root` of another type.
500
+ const read = (key) => {
501
+ const setting = settingAt(settings, ['test', key]);
502
+ if (setting === null)
503
+ return [];
504
+ const found = patterns(setting, false);
505
+ return Array.isArray(found) ? found : null;
506
+ };
507
+ const roots = read('root') ?? [];
508
+ // Bun 1.2 reads neither `pathIgnorePatterns` nor `retry`. It reads the first from 1.3.11, and the version holds no
509
+ // patch to tell 1.3.10 from it, so all of 1.3 reads it.
510
+ const readsFrom13 = atLeast(this.version, 1, 3);
511
+ const ignored = readsFrom13 ? read('pathIgnorePatterns') : [];
512
+ const concurrent = read('concurrentTestGlob');
513
+ const wrong = ignored === null ? 'pathIgnorePatterns' : concurrent === null ? 'concurrentTestGlob' : null;
514
+ const refused = wrong &&
515
+ `The config's test.${wrong} is not a string or a list of strings, so Bun refuses the config and runs no test.`;
516
+ const testRoot = path.resolve(folder, roots[0] ?? '.');
517
+ const isIgnored = picomatch(ignored ?? []);
518
+ const isConcurrent = picomatch(concurrent ?? []);
519
+ const about = (parallel) => ({
520
+ name: '',
521
+ testDir: testRoot,
522
+ dependencies: [],
523
+ settings,
524
+ root: folder,
525
+ container: null,
526
+ config: config,
527
+ grep: null,
528
+ grepRoot: folder,
529
+ runsByDefault: true,
530
+ defaults: {
531
+ ...NO_DEFAULTS,
532
+ retry: readsFrom13 ? settingAt(settings, ['test', 'retry']) : null,
533
+ parallel: parallel ? { kind: 'value', value: true } : null,
534
+ },
535
+ });
536
+ return {
537
+ refused,
538
+ about: about(false),
539
+ concurrent: about(true),
540
+ // Bun 1.4.4 matches the glob against the file's whole path.
541
+ isConcurrent: (file) => isConcurrent(posix(file)),
542
+ runs: (file) => {
543
+ if (!isInside(testRoot, file))
544
+ return false;
545
+ const relative = posix(path.relative(folder, file));
546
+ const parts = relative.split('/');
547
+ if (parts.some((part) => part.startsWith('.') || part === 'node_modules'))
548
+ return false;
549
+ if (parts.some((_, index) => isIgnored(parts.slice(0, index + 1).join('/'))))
550
+ return false;
551
+ return /(\.test|_test|\.spec|_spec)\.(js|jsx|ts|tsx|mjs|cjs|mts|cts)$/i.test(relative);
552
+ },
553
+ };
554
+ }
555
+ /** `node --test` runs the files its arguments name, or without any, those its default patterns match; see `nodeFiles`. */
556
+ placeNode(file) {
557
+ if (!this.findsFiles)
558
+ return { config: null, settings: null, runs: 'test', projects: null };
559
+ this.nodeRuns ??= this.nodeFiles();
560
+ const runs = this.nodeRuns(posix(path.relative(this.configs.root, file)));
561
+ return { config: null, settings: null, runs: runs ? 'test' : false, projects: null };
562
+ }
563
+ /**
564
+ * The files `node --test` runs from the root, outside dot folders and `node_modules`: those the arguments of the
565
+ * `test` script and the scripts it runs give, or the first script that runs it, each a glob or a path, and from
566
+ * Node.js 26.8 a folder for the default patterns inside it; without arguments, the default patterns. They take
567
+ * TypeScript files too from Node.js 22.18, which strips types by default.
568
+ */
569
+ nodeFiles() {
570
+ const extensions = stripsTypes(this.version) ? 'cjs,mjs,js,cts,mts,ts' : 'cjs,mjs,js';
571
+ const defaults = ['**/*.test', '**/*-test', '**/*_test', '**/test-*', '**/test', '**/test/**/*'].map((glob) => `${glob}.{${extensions}}`);
572
+ const searchesFolders = atLeast(this.version, 26, 8);
573
+ const commands = nodeTestArguments(path.join(this.configs.root, 'package.json'));
574
+ // Each command runs the files it gives, or without any, those the default patterns match.
575
+ const globs = (commands.length > 0 ? commands : [[]]).flatMap((files) => files.length === 0
576
+ ? defaults
577
+ : files.flatMap((argument) => {
578
+ const folder = path.resolve(this.configs.root, argument);
579
+ if (!searchesFolders || !existsSync(folder) || !statSync(folder).isDirectory())
580
+ return [argument];
581
+ return defaults.map((glob) => `${argument.replace(/\/+$/, '')}/${glob}`);
582
+ }));
583
+ const isMatch = picomatch(globs.map((glob) => path.posix.normalize(posix(glob))));
584
+ return (relative) => isMatch(relative) && !relative.split('/').includes('node_modules');
585
+ }
586
+ /** WebdriverIO runs the files of the config in the root; without one, it runs nothing, unless it is given files. */
587
+ placeWebdriverIO(file) {
588
+ const config = this.configs.rootConfig();
589
+ if (!config)
590
+ return { config: null, settings: null, runs: this.findsFiles ? false : 'test', projects: null };
591
+ const [project] = this.projectsOf(config);
592
+ if (project.unknown) {
593
+ this.configs.reportUnknown(project.unknown.setting, project.unknown.key, this.findsFiles ? NOT_FOUND : ASSUMED);
594
+ return { config, settings: project.settings, runs: null, projects: null };
595
+ }
596
+ const runs = project.runs(file);
597
+ return { config, settings: project.settings, runs, projects: runs ? [project.about] : [] };
598
+ }
599
+ /**
600
+ * The files `wdio` runs: those under `rootDir`, the config's folder by default, that a `specs` glob matches, also in
601
+ * the nested lists that group files into one worker, less those `exclude` leaves out. A capability's own `specs` replace the config's for it, and its `exclude` adds to the config's. Globs leave out
602
+ * dot files and folders, and one without a `/` matches a file's name at any depth. When every `exclude` entry holds a
603
+ * `/`, `\` or `*`, they are globs too; otherwise each one leaves out every file whose URL contains it. Files given
604
+ * with `--spec` replace `specs`, so scanned with globs, every file they match counts as run, less the excludes. The
605
+ * config's `mochaOpts.timeout` and `retries`, or `jasmineOpts.defaultTimeoutInterval`, apply to every test.
606
+ */
607
+ webdriverioProject(config) {
608
+ const { settings } = config;
609
+ const writtenRoot = settingAt(settings, ['rootDir']);
610
+ if (writtenRoot !== null &&
611
+ !isUnset(writtenRoot) &&
612
+ !(writtenRoot.kind === 'value' && typeof writtenRoot.value === 'string')) {
613
+ return this.unknownProject(config, unknownAt(writtenRoot), 'rootDir');
614
+ }
615
+ // The config's `rootDir`, from the folder `wdio` starts in, replaces the config's folder.
616
+ const folder = writtenRoot?.kind === 'value' && typeof writtenRoot.value === 'string'
617
+ ? path.resolve(this.configs.root, writtenRoot.value)
618
+ : path.dirname(config.file);
619
+ const globs = (owner, keys) => {
620
+ const setting = keys.map((key) => settingAt(owner, [key])).find((item) => item !== null && !isUnset(item));
621
+ if (!setting)
622
+ return null;
623
+ // A single glob is taken as a list of one; a nested list groups files into one worker.
624
+ const items = setting.kind === 'list'
625
+ ? setting.items.flatMap((item) => (item.kind === 'list' ? item.items : [item]))
626
+ : [setting];
627
+ const found = patterns({ kind: 'list', items }, false);
628
+ return Array.isArray(found) ? found : found;
629
+ };
630
+ // WebdriverIO globs each pattern with `/` for `\`, from `rootDir`.
631
+ const fromRoot = (glob) => {
632
+ const slashed = glob.replaceAll('\\', '/');
633
+ return path.posix.normalize(path.isAbsolute(slashed) ? posix(path.relative(folder, slashed)) : slashed);
634
+ };
635
+ // As glob's matchBase does, a pattern without a slash matches at any depth, outside dot folders too; the slash of
636
+ // `./` counts. A pattern that starts with `!` matches no file in glob.
637
+ const matcher = (list) => picomatch(list
638
+ .filter((glob) => !glob.startsWith('!'))
639
+ .map((glob) => (/[/\\]/.test(glob) ? fromRoot(glob) : `**/${fromRoot(glob)}`)));
640
+ const specs = globs(settings, ['specs']) ?? [];
641
+ if (!Array.isArray(specs))
642
+ return this.unknownProject(config, specs, 'specs');
643
+ const written = globs(settings, ['exclude']) ?? [];
644
+ if (!Array.isArray(written))
645
+ return this.unknownProject(config, written, 'exclude');
646
+ const excludes = written.filter((item) => !isSuiteName(settings, item));
647
+ const isGlob = excludes.every((item) => /[/\\*]/.test(item));
648
+ const isPathExcluded = matcher(excludes);
649
+ const isExcluded = (file, relative) => isGlob ? isPathExcluded(relative) : excludes.some((item) => pathToFileURL(file).href.includes(item));
650
+ // Each capability may set its own specs, which replace the config's, and excludes, added to the config's, inside
651
+ // `alwaysMatch` when it has one. WebdriverIO 10 reads only `wdio:specs` and `wdio:exclude`.
652
+ const capabilities = settingAt(settings, ['capabilities']);
653
+ const [specKeys, excludeKeys] = atLeast(this.version, 10, 0)
654
+ ? [['wdio:specs'], ['wdio:exclude']]
655
+ : [
656
+ ['specs', 'wdio:specs'],
657
+ ['exclude', 'wdio:exclude'],
658
+ ];
659
+ const runs = [];
660
+ for (const capability of capabilities?.kind === 'list' && capabilities.items.length > 0
661
+ ? capabilities.items
662
+ : [emptySettings()]) {
663
+ const options = settingAt(capability, ['alwaysMatch']) ?? capability;
664
+ const own = globs(options, specKeys);
665
+ if (own !== null && !Array.isArray(own))
666
+ return this.unknownProject(config, own, 'capabilities.specs');
667
+ const exclude = globs(options, excludeKeys) ?? [];
668
+ if (!Array.isArray(exclude))
669
+ return this.unknownProject(config, exclude, 'capabilities.exclude');
670
+ runs.push({ isSpec: matcher(own ?? specs), isExcluded: matcher(exclude) });
671
+ }
672
+ const framework = settingAt(settings, ['framework']);
673
+ const isJasmine = framework?.kind === 'value' && framework.value === 'jasmine';
674
+ const options = settingAt(settings, [isJasmine ? 'jasmineOpts' : 'mochaOpts']) ?? emptySettings();
675
+ const duration = (key) => {
676
+ const setting = settingAt(options, [key]);
677
+ const value = setting?.kind === 'value' ? durationOf(setting.value, key) : null;
678
+ return value === null ? setting : { kind: 'value', value };
679
+ };
680
+ // A `defaultTimeoutInterval` of 0 replaces WebdriverIO's default, and its Jasmine adapter then takes 60 seconds.
681
+ const interval = settingAt(options, ['defaultTimeoutInterval']);
682
+ const jasmineTimeout = interval?.kind === 'value' && interval.value === 0 ? { kind: 'value', value: 60_000 } : interval;
683
+ const about = {
684
+ name: '',
685
+ testDir: folder,
686
+ dependencies: [],
687
+ settings,
688
+ root: folder,
689
+ container: null,
690
+ config,
691
+ grep: null,
692
+ grepRoot: folder,
693
+ runsByDefault: true,
694
+ defaults: isJasmine
695
+ ? { ...NO_DEFAULTS, timeout: jasmineTimeout }
696
+ : { ...NO_DEFAULTS, timeout: duration('timeout'), retry: duration('retries') },
697
+ };
698
+ return {
699
+ config,
700
+ settings,
701
+ about,
702
+ unknown: null,
703
+ runs: (file) => {
704
+ const relative = posix(path.relative(folder, file));
705
+ if (isExcluded(file, relative))
706
+ return false;
707
+ // Files given with `--spec` replace every capability's specs.
708
+ const running = runs.some((run) => (!this.findsFiles || run.isSpec(relative)) && !run.isExcluded(relative));
709
+ return running ? 'test' : false;
710
+ },
711
+ };
712
+ }
210
713
  projectsOf(config) {
211
714
  let projects = this.projects.get(config);
212
715
  if (!projects) {
@@ -214,6 +717,14 @@ export class ConfigRuns {
214
717
  projects = this.playwrightProjects(config);
215
718
  else if (this.framework === 'jest')
216
719
  projects = this.jestProjects(config);
720
+ else if (this.framework === 'mocha')
721
+ projects = [this.mochaProject(config, config.settings)];
722
+ else if (this.framework === 'jasmine')
723
+ projects = [this.jasmineProject(config)];
724
+ else if (this.framework === 'cypress')
725
+ projects = this.cypressProjects(config);
726
+ else if (this.framework === 'webdriverio')
727
+ projects = [this.webdriverioProject(config)];
217
728
  else
218
729
  projects = this.vitestProjects(config);
219
730
  this.projects.set(config, projects);
@@ -221,8 +732,12 @@ export class ConfigRuns {
221
732
  return projects;
222
733
  }
223
734
  /** Vitest's `retry`: before 4.1, which reads only a count, an object is one the scan can't work out. */
224
- vitestRetry(setting) {
225
- return setting?.kind === 'object' && !atLeast(this.version, 4, 1) ? unknownAt(setting) : setting;
735
+ vitestRetry(config, setting) {
736
+ if (setting?.kind !== 'object' || atLeast(this.version, 4, 1))
737
+ return setting;
738
+ const message = "The config's test.retry is an object, which Vitest reads from 4.1; before, it runs no test.";
739
+ this.configs.reportSetting(setting, config, 'test.retry', message);
740
+ return unknownAt(setting);
226
741
  }
227
742
  /** A project whose files can't be worked out, because of `setting` under `key`: it may run any file. */
228
743
  unknownProject(config, setting, key) {
@@ -714,7 +1229,26 @@ export class ConfigRuns {
714
1229
  const ownTags = isVitest5 ? settingAt(entry, ['test', 'tags']) : null;
715
1230
  const settings = mergeDeep(base, entry);
716
1231
  const name = this.listedName(isVitest5 ? entry : settings, index, container);
717
- return this.vitestProject(config, settings, folder, ownTags, name);
1232
+ const root = this.inlineRoot(config, entry, folder);
1233
+ if (typeof root !== 'string')
1234
+ return [this.unknownProject(config, root.setting, root.key)];
1235
+ return this.vitestProject(config, settings, root, ownTags, name, true);
1236
+ }
1237
+ /**
1238
+ * The folder an inline project runs from. Without a root of its own, every version runs it from the config's root;
1239
+ * Vitest 5 also resolves its own root from there, where older versions resolve it from the config's folder.
1240
+ */
1241
+ inlineRoot(config, entry, folder) {
1242
+ const parent = writtenRoot(config.settings);
1243
+ const parentRoot = typeof parent === 'string' ? path.resolve(folder, parent) : (parent ?? folder);
1244
+ const own = writtenRoot(entry);
1245
+ if (own === null)
1246
+ return parentRoot;
1247
+ if (typeof own !== 'string')
1248
+ return own;
1249
+ if (!atLeast(this.version, 5, 0))
1250
+ return path.resolve(folder, own);
1251
+ return typeof parentRoot === 'string' ? path.resolve(parentRoot, own) : parentRoot;
718
1252
  }
719
1253
  /**
720
1254
  * The projects of a config file or folder `test.projects` names: the config's own, also the projects it lists in
@@ -739,10 +1273,11 @@ export class ConfigRuns {
739
1273
  }
740
1274
  /**
741
1275
  * A Vitest project, with the name Vitest gives it: its files are found from `test.dir`, else `test.root` or `root`,
742
- * else its folder. With browser mode on, each of `browser.instances` is a project of its own, named as Vitest names it.
1276
+ * else its folder; with `isRoot`, `folder` is its root already. With browser mode on, each of `browser.instances` is
1277
+ * a project of its own, named as Vitest names it.
743
1278
  */
744
- vitestProject(config, settings, folder, ownTags, name) {
745
- const project = this.vitestFiles(config, settings, folder, ownTags, name);
1279
+ vitestProject(config, settings, folder, ownTags, name, isRoot = false) {
1280
+ const project = this.vitestFiles(config, settings, folder, ownTags, name, isRoot);
746
1281
  if (project.unknown)
747
1282
  return [project];
748
1283
  const enabled = settingAt(settings, ['test', 'browser', 'enabled']);
@@ -765,11 +1300,11 @@ export class ConfigRuns {
765
1300
  });
766
1301
  }
767
1302
  /** The files a Vitest project runs and its settings; `ownTags`, when set, in place of the tags in `settings`. */
768
- vitestFiles(config, settings, folder, ownTags, name) {
1303
+ vitestFiles(config, settings, folder, ownTags, name, isRoot) {
769
1304
  let root = folder;
770
1305
  let dir = null;
771
1306
  let hasTestRoot = false;
772
- for (const key of ['test.root', 'root', 'test.dir']) {
1307
+ for (const key of isRoot ? ['test.dir'] : ['test.root', 'root', 'test.dir']) {
773
1308
  const setting = settingAt(settings, key.split('.'));
774
1309
  if (setting === null || isUnset(setting))
775
1310
  continue;
@@ -830,7 +1365,7 @@ export class ConfigRuns {
830
1365
  runsByDefault: true,
831
1366
  defaults: {
832
1367
  timeout: settingAt(settings, ['test', 'testTimeout']),
833
- retry: this.vitestRetry(settingAt(settings, ['test', 'retry'])),
1368
+ retry: this.vitestRetry(config, settingAt(settings, ['test', 'retry'])),
834
1369
  repeatEach: null,
835
1370
  // Vitest 5 added `repeats` to the config.
836
1371
  repeats: atLeast(this.version, 5, 0) ? settingAt(settings, ['test', 'repeats']) : null,
@@ -916,7 +1451,49 @@ function hasInSourceTests(file) {
916
1451
  function unknownAt(setting) {
917
1452
  return setting.kind === 'unknown' ? setting : { kind: 'unknown', where: null };
918
1453
  }
1454
+ /**
1455
+ * The root a config sets, `test.root` over Vite's `root`, as written; null when it sets none, and the setting and its
1456
+ * key when it can't be worked out.
1457
+ */
1458
+ function writtenRoot(settings) {
1459
+ for (const key of ['test.root', 'root']) {
1460
+ const setting = settingAt(settings, key.split('.'));
1461
+ if (setting === null || isUnset(setting))
1462
+ continue;
1463
+ if (setting.kind !== 'value' || typeof setting.value !== 'string')
1464
+ return { setting: unknownAt(setting), key };
1465
+ return setting.value;
1466
+ }
1467
+ return null;
1468
+ }
919
1469
  /** A config's settings without `test.projects`, as an inline project inherits them; both are known objects. */
1470
+ /**
1471
+ * Whether Mocha's lookup of one `spec` entry finds a file: an entry with glob characters is a glob, which takes any
1472
+ * file it matches; otherwise a file, the entry with one of the extensions added, or a folder, whose files with one of
1473
+ * the extensions it takes, without hidden ones, and those of folders below with `recursive`.
1474
+ */
1475
+ function mochaSpec(folder, spec, extensions, recursive) {
1476
+ const target = path.resolve(folder, spec);
1477
+ if (picomatch.scan(posix(spec)).isGlob) {
1478
+ // An absolute glob, as `path.join(__dirname, 'test', '**', '*.spec.js')` builds it, is taken from the folder.
1479
+ const written = path.isAbsolute(spec) ? path.relative(folder, spec) : spec;
1480
+ const isMatch = picomatch(posix(written).replace(/^\.\//, ''));
1481
+ return (file) => isMatch(posix(path.relative(folder, file)));
1482
+ }
1483
+ const stat = statSync(target, { throwIfNoEntry: false });
1484
+ if (stat?.isFile())
1485
+ return (file) => file === target;
1486
+ if (!stat?.isDirectory())
1487
+ return (file) => extensions.some((extension) => file === `${target}.${extension}`);
1488
+ return (file) => {
1489
+ if (!isInside(target, file) || path.basename(file).startsWith('.'))
1490
+ return false;
1491
+ if (!recursive && path.dirname(file) !== target)
1492
+ return false;
1493
+ // An extension may have several parts, as `spec.js`.
1494
+ return extensions.some((extension) => file.endsWith(`.${extension}`));
1495
+ };
1496
+ }
920
1497
  function withoutProjects(settings) {
921
1498
  const root = settings;
922
1499
  const test = root.keys.get('test');
@@ -1125,21 +1702,19 @@ const UNKNOWN_DEFAULTS = {
1125
1702
  parallel: false,
1126
1703
  };
1127
1704
  /**
1128
- * What the config a file uses gives it: Playwright's `forbidOnly`, read from the top level only, as Playwright reads
1129
- * it, and for each test, the projects that run it and what they set; null without a config.
1705
+ * What the config a file uses gives it: Playwright's `forbidOnly` or Vitest's `allowOnly`, read from the top level
1706
+ * only, as the runners read them, and for each test, the projects that run it and what they set; null without a config.
1130
1707
  */
1131
1708
  export function fileConfig(placement, framework, version, file) {
1132
1709
  const { config, projects } = placement;
1133
1710
  if (config === null) {
1134
1711
  // Without a config, Vitest defines no tags and keeps strictTags on.
1135
1712
  const tags = vitestTags(null, null, null, atLeast(version, 5, 0));
1136
- return framework === 'vitest' ? { forbidOnly: false, forTest: () => NO_CONFIG, tags, configTags: [] } : null;
1713
+ const forbidOnly = onlyRule(framework, null);
1714
+ return framework === 'vitest' ? { forbidOnly, forTest: () => NO_CONFIG, tags, configTags: [] } : null;
1137
1715
  }
1138
- const forbidOnly = framework === 'playwright' ? settingAt(config.settings, ['forbidOnly']) : null;
1139
1716
  return {
1140
- forbidOnly: forbidOnly?.kind === 'unknown'
1141
- ? (unknownText(forbidOnly) ?? false)
1142
- : forbidOnly?.kind === 'value' && forbidOnly.value === true,
1717
+ forbidOnly: onlyRule(framework, config.settings),
1143
1718
  forTest(test, suites) {
1144
1719
  if (projects === null)
1145
1720
  return { projects: null, defaults: UNKNOWN_DEFAULTS };
@@ -1151,6 +1726,44 @@ export function fileConfig(placement, framework, version, file) {
1151
1726
  configTags: placement.tags ?? [],
1152
1727
  };
1153
1728
  }
1729
+ /**
1730
+ * Why a `.only` fails: with Playwright's `forbidOnly` or Mocha's `forbid-only`, the run, before any test; with Vitest's
1731
+ * `allowOnly` off, the test itself, which Vitest marks failed instead of running it (checked in 3.2.4, 4.1.12 and
1732
+ * 5.1.0). `allowOnly` is off by default when `process.env.CI` is set. Null when nothing fails.
1733
+ */
1734
+ function onlyRule(framework, settings) {
1735
+ if (framework === 'playwright' || framework === 'mocha') {
1736
+ // Mocha takes its options in kebab case or camel case.
1737
+ const [name, keys] = framework === 'mocha' ? ['forbid-only', ['forbid-only', 'forbidOnly']] : ['forbidOnly', ['forbidOnly']];
1738
+ const forbid = settings && keys.map((key) => settingAt(settings, [key])).find((setting) => setting !== null);
1739
+ const condition = forbid?.kind === 'unknown' ? unknownText(forbid) : null;
1740
+ if (condition) {
1741
+ const message = `The config sets ${name} when ${condition}, so then this .only fails the run before any test runs.`;
1742
+ return { message, isSure: false };
1743
+ }
1744
+ if (forbid?.kind !== 'value' || forbid.value !== true)
1745
+ return null;
1746
+ return { message: `The config sets ${name}, so this .only fails the run before any test runs.`, isSure: true };
1747
+ }
1748
+ if (framework !== 'vitest')
1749
+ return null;
1750
+ const allow = settings && settingAt(settings, ['test', 'allowOnly']);
1751
+ const effect = 'Vitest fails this .only test instead of running it';
1752
+ if (allow === null || isUnset(allow)) {
1753
+ return {
1754
+ message: `Vitest's allowOnly is off when process.env.CI is set, as in CI, and then ${effect}.`,
1755
+ isSure: false,
1756
+ };
1757
+ }
1758
+ const condition = allow.kind === 'unknown' ? unknownText(allow) : null;
1759
+ if (condition) {
1760
+ return { message: `The config sets allowOnly to ${condition}; when that is false, ${effect}.`, isSure: false };
1761
+ }
1762
+ // An object or a list is true, as is any value but `false` and the other falsy ones.
1763
+ if (allow.kind !== 'value' || allow.value)
1764
+ return null;
1765
+ return { message: `The config sets allowOnly to ${String(allow.value)}, so ${effect}.`, isSure: true };
1766
+ }
1154
1767
  /** The tags the projects that run a file define, when they all define the same; null otherwise. */
1155
1768
  function agreedTags(projects) {
1156
1769
  const [first, ...others] = (projects ?? []).map(({ defaults }) => defaults.tags);
@@ -1205,3 +1818,158 @@ function isLocalPreset(preset) {
1205
1818
  function isFileAt(file) {
1206
1819
  return existsSync(file) && statSync(file).isFile();
1207
1820
  }
1821
+ /**
1822
+ * The interface Mocha's `ui` gives a file's tests: BDD by default, or TDD. Another one, QUnit, exports or a package,
1823
+ * declares tests in a way the scan doesn't read, which is reported on the config, and the file is read as BDD.
1824
+ */
1825
+ export function mochaInterface(placement, configs) {
1826
+ const { config } = placement;
1827
+ const ui = config ? settingAt(config.settings, ['ui']) : null;
1828
+ if (ui === null || isUnset(ui))
1829
+ return 'bdd';
1830
+ if (ui.kind !== 'value') {
1831
+ configs.reportUnknown(unknownAt(ui), 'ui', "the scan reads the tests as Mocha's default, BDD");
1832
+ return 'bdd';
1833
+ }
1834
+ if (ui.value === 'bdd' || ui.value === 'tdd')
1835
+ return ui.value;
1836
+ const message = `The config's ui '${String(ui.value)}' is an interface the scan doesn't read, so it reads the tests as Mocha's default, BDD.`;
1837
+ configs.reportConfig(config, message);
1838
+ return 'bdd';
1839
+ }
1840
+ /**
1841
+ * Whether an `exclude` entry names one of the config's `suites` rather than files: WebdriverIO leaves that suite out of
1842
+ * a `--suite` run, and takes it as no exclusion otherwise.
1843
+ */
1844
+ function isSuiteName(settings, item) {
1845
+ if (/[/\\*?]/.test(item) || /\.(js|ts|mjs|cjs|es6|feature)$/i.test(item))
1846
+ return false;
1847
+ const suites = settingAt(settings, ['suites']);
1848
+ return suites?.kind === 'object' && suites.keys.has(item);
1849
+ }
1850
+ /** The interface WebdriverIO's `mochaOpts.ui` gives a file's tests, read as Mocha's `ui`. */
1851
+ export function webdriverioInterface(placement, configs) {
1852
+ if (placement.config === null || webdriverioFramework(placement, null) === 'jasmine')
1853
+ return 'bdd';
1854
+ const options = settingAt(placement.config.settings, ['mochaOpts']);
1855
+ return mochaInterface({ ...placement, config: { ...placement.config, settings: options ?? emptySettings() } }, configs);
1856
+ }
1857
+ /**
1858
+ * The framework a WebdriverIO config runs the specs with: Mocha by default, or Jasmine. Cucumber runs feature files,
1859
+ * which the scan doesn't read; that and a value the scan can't work out are reported on the config when `configs` is
1860
+ * given, and the files are read as Mocha's.
1861
+ */
1862
+ export function webdriverioFramework(placement, configs) {
1863
+ const { config } = placement;
1864
+ const framework = config ? settingAt(config.settings, ['framework']) : null;
1865
+ if (framework === null || isUnset(framework))
1866
+ return 'mocha';
1867
+ if (framework.kind === 'value' && (framework.value === 'mocha' || framework.value === 'jasmine'))
1868
+ return framework.value;
1869
+ if (framework.kind !== 'value') {
1870
+ configs?.reportUnknown(unknownAt(framework), 'framework', "the scan reads the tests as WebdriverIO's default, Mocha's");
1871
+ }
1872
+ else {
1873
+ const message = `The config's framework '${String(framework.value)}' is one the scan doesn't read, so it reads the tests as WebdriverIO's default, Mocha's.`;
1874
+ configs?.reportConfig(config, message);
1875
+ }
1876
+ return 'mocha';
1877
+ }
1878
+ /**
1879
+ * How the config a file runs with maps its imports, for its dependencies: Vite's `resolve.alias` and Vitest's
1880
+ * `test.alias`, from the project and from the config file, and Jest's `moduleNameMapper`, `modulePaths` and
1881
+ * `moduleDirectories` other than `node_modules`, with `<rootDir>` in them. Null for the other runners, without a
1882
+ * config, or when it maps nothing.
1883
+ */
1884
+ export function importMap(placement, framework) {
1885
+ const { config, settings } = placement;
1886
+ if (!config || !settings)
1887
+ return null;
1888
+ // The files of a project share its map, so the modules they import are read once.
1889
+ if (!importMaps.has(settings))
1890
+ importMaps.set(settings, readImportMap(config, settings, placement, framework));
1891
+ return importMaps.get(settings) ?? null;
1892
+ }
1893
+ const importMaps = new WeakMap();
1894
+ function readImportMap(config, settings, placement, framework) {
1895
+ const map = { aliases: [], paths: [], directories: [], root: null };
1896
+ if (framework === 'vitest') {
1897
+ map.root = placement.projects?.[0]?.root ?? path.dirname(config.file);
1898
+ const sources = settings === config.settings ? [settings] : [settings, config.settings];
1899
+ for (const source of sources) {
1900
+ for (const key of [
1901
+ ['test', 'alias'],
1902
+ ['resolve', 'alias'],
1903
+ ])
1904
+ map.aliases.push(...viteAliases(settingAt(source, key)));
1905
+ }
1906
+ }
1907
+ else if (framework === 'jest') {
1908
+ const rootDir = placement.projects?.[0]?.root ?? path.dirname(config.file);
1909
+ const mapper = settingAt(settings, ['moduleNameMapper']);
1910
+ for (const [pattern, target] of mapper?.kind === 'object' ? mapper.keys : []) {
1911
+ const replacements = (target.kind === 'list' ? strings(target) : stringOf(target)).map((item) => replaceRootDir(rootDir, item));
1912
+ const find = regExp(pattern, '');
1913
+ if (find && replacements.length > 0)
1914
+ map.aliases.push({ find, replacements, replacesName: true });
1915
+ }
1916
+ map.paths = strings(settingAt(settings, ['modulePaths'])).map((item) => path.resolve(rootDir, replaceRootDir(rootDir, item)));
1917
+ // A `<rootDir>` entry of moduleDirectories is a folder, as a modulePaths entry is, not a name to look for.
1918
+ for (const item of strings(settingAt(settings, ['moduleDirectories']))) {
1919
+ const directory = replaceRootDir(rootDir, item);
1920
+ if (path.isAbsolute(directory))
1921
+ map.paths.push(directory);
1922
+ else if (directory !== 'node_modules')
1923
+ map.directories.push(directory);
1924
+ }
1925
+ }
1926
+ return map.aliases.length + map.paths.length + map.directories.length > 0 ? map : null;
1927
+ }
1928
+ /**
1929
+ * Vite's aliases: an object of names and their replacements, or a list of `{ find, replacement }`, where `find` is a
1930
+ * name or a regex. Entries the scan can't work out are left out.
1931
+ */
1932
+ function viteAliases(setting) {
1933
+ if (setting?.kind === 'object') {
1934
+ return [...setting.keys].flatMap(([find, target]) => stringOf(target).map((replacement) => ({ find, replacements: [replacement], replacesName: false })));
1935
+ }
1936
+ if (setting?.kind !== 'list')
1937
+ return [];
1938
+ return setting.items.flatMap((item) => {
1939
+ const find = settingAt(item, ['find']);
1940
+ const replacements = stringOf(settingAt(item, ['replacement']));
1941
+ const name = find?.kind === 'value' && typeof find.value === 'string' ? find.value : null;
1942
+ const pattern = find?.kind === 'regex' ? regExp(find.source, find.flags) : null;
1943
+ const written = name ?? pattern;
1944
+ return written !== null && replacements.length > 0 ? [{ find: written, replacements, replacesName: false }] : [];
1945
+ });
1946
+ }
1947
+ /** A setting's string as a list of one; empty for anything else. */
1948
+ function stringOf(setting) {
1949
+ return setting?.kind === 'value' && typeof setting.value === 'string' ? [setting.value] : [];
1950
+ }
1951
+ /** Whether a Node.js version runs TypeScript files without a flag: from 22.18 on the 22 line, and from 23.6. */
1952
+ function stripsTypes(version) {
1953
+ if (version === null)
1954
+ return true;
1955
+ const [major, minor] = version;
1956
+ return major > 23 || (major === 23 && minor >= 6) || (major === 22 && minor >= 18);
1957
+ }
1958
+ /**
1959
+ * The `node --test` commands of the `test` script and the scripts it runs, or else of the first script that runs one,
1960
+ * each as the files it gives; none when no script runs it or there is no package.json.
1961
+ */
1962
+ function nodeTestArguments(manifest) {
1963
+ let scripts = {};
1964
+ try {
1965
+ scripts = JSON.parse(readFileSync(manifest, 'utf8')).scripts ?? {};
1966
+ }
1967
+ catch {
1968
+ return [];
1969
+ }
1970
+ const fromTest = scriptsRunBy(scripts, 'test').flatMap(nodeTestCommands);
1971
+ if (fromTest.length > 0)
1972
+ return fromTest;
1973
+ const others = Object.values(scripts).filter((script) => typeof script === 'string');
1974
+ return others.map(nodeTestCommands).find((commands) => commands.length > 0) ?? [];
1975
+ }