vitest-native 0.14.1 → 0.15.0-rc.2
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/README.md +11 -10
- package/dist/cli.d.mts +9 -1
- package/dist/cli.mjs +2609 -361
- package/dist/context.d.mts +19 -0
- package/dist/context.mjs +47 -1
- package/dist/docs-url.d.mts +4 -0
- package/dist/docs-url.mjs +9 -0
- package/dist/errors.d.mts +21 -1
- package/dist/hang-watch-setup.mjs +55 -0
- package/dist/hang-watch.d.mts +56 -0
- package/dist/hang-watch.mjs +343 -0
- package/dist/helpers.cjs +24 -53
- package/dist/helpers.mjs +22 -1
- package/dist/host-D0r3P_T1.mjs +1985 -0
- package/dist/{index-aNBCTfA5.d.mts → index-JOR602XU.d.mts} +105 -2
- package/dist/index.d.mts +2 -1
- package/dist/index.mjs +723 -149
- package/dist/jest-compat/aliases.d.mts +1 -0
- package/dist/jest-compat/aliases.mjs +11 -0
- package/dist/jest-compat/interop.mjs +1 -1
- package/dist/jest-compat/jest-object.mjs +23 -5
- package/dist/jest-compat/node-registry.mjs +836 -68
- package/dist/jest-compat/setup.mjs +205 -12
- package/dist/jest-compat.cjs +383 -47
- package/dist/jest-compat.d.cts +19 -2
- package/dist/jest-compat.d.mts +19 -2
- package/dist/jest-compat.mjs +232 -47
- package/dist/matchers.d.cts +0 -1
- package/dist/matchers.d.mts +0 -1
- package/dist/native/assets.mjs +3 -1
- package/dist/native/babel-caller.d.mts +13 -0
- package/dist/native/babel-caller.mjs +24 -0
- package/dist/native/babel-toolchain.mjs +27 -3
- package/dist/native/boundary.mjs +45 -8
- package/dist/native/bounded-process.mjs +63 -5
- package/dist/native/bridge-client.mjs +133 -0
- package/dist/native/bridge-server.mjs +215 -0
- package/dist/native/bridge-thread.mjs +79 -0
- package/dist/native/bridge.mjs +512 -0
- package/dist/native/compile-error.mjs +120 -0
- package/dist/native/explain.mjs +3 -2
- package/dist/native/expo-module-mocks.mjs +2 -0
- package/dist/native/export-condition-recovery.mjs +20 -99
- package/dist/native/hooks.mjs +256 -133
- package/dist/native/jest-resolvers.mjs +94 -0
- package/dist/native/library-jest.mjs +352 -0
- package/dist/native/loader.mjs +167 -73
- package/dist/native/match.mjs +67 -7
- package/dist/native/module-reset.mjs +44 -49
- package/dist/native/ownership-ledger.d.mts +87 -0
- package/dist/native/ownership-ledger.mjs +524 -0
- package/dist/native/ownership.d.mts +20 -0
- package/dist/native/ownership.mjs +114 -0
- package/dist/native/registry-compiler.mjs +39 -5
- package/dist/native/registry-process.mjs +327 -28
- package/dist/native/registry.mjs +752 -200
- package/dist/native/resident-health.mjs +1 -0
- package/dist/native/resolve.d.mts +7 -0
- package/dist/native/resolve.mjs +119 -4
- package/dist/native/rntl-hooks.mjs +9 -5
- package/dist/native/rntl-text-host.mjs +5 -15
- package/dist/native/root-spelling.d.mts +6 -0
- package/dist/native/root-spelling.mjs +77 -0
- package/dist/native/runner.mjs +2 -19
- package/dist/native/setup.mjs +119 -38
- package/dist/native/shared-resolver.d.mts +141 -0
- package/dist/native/shared-resolver.mjs +719 -0
- package/dist/native/test-runner.mjs +26 -0
- package/dist/native/transform.mjs +18 -22
- package/dist/native/worker.mjs +9 -4
- package/dist/node-dispatch.mjs +293 -0
- package/dist/{preset-map-CogjYmSw.mjs → preset-map-DdAexIwz.mjs} +113 -2
- package/dist/{presets-CXDyg51l.mjs → presets-_EaSw29m.mjs} +710 -130
- package/dist/presets.d.mts +1 -1
- package/dist/presets.mjs +1 -1
- package/dist/rntl-matchers.d.ts +11 -15
- package/dist/rntl-support.mjs +28 -13
- package/dist/root-mocks-BNXeosbR.mjs +155 -0
- package/dist/serializer.cjs +7 -5
- package/dist/serializer.d.cts +7 -5
- package/dist/serializer.d.mts +7 -5
- package/dist/serializer.mjs +7 -5
- package/dist/setup.mjs +26 -27
- package/docs/migrating-from-jest.md +9 -232
- package/docs/migrating-from-vitest-react-native.md +4 -4
- package/docs/release-readiness.md +9 -11
- package/docs/versioning.md +38 -9
- package/package.json +59 -33
- package/dist/host-BAAAlSkO.mjs +0 -1028
package/dist/native/loader.mjs
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
// Node ESM loader hook (registered via module.register). Intercepts import() of RN —
|
|
2
2
|
// which Module._extensions cannot — Flow-stripping and serving boundary mock source.
|
|
3
|
+
import { nodeDispatch } from "../node-dispatch.mjs";
|
|
3
4
|
import Module, { createRequire } from "node:module";
|
|
4
5
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
5
6
|
import { projectContext } from "../context.mjs";
|
|
7
|
+
import { cjsConditionsWithout } from "./export-condition-recovery.mjs";
|
|
8
|
+
import { ownershipResolverInputs, workerSharedResolver } from "./shared-resolver.mjs";
|
|
6
9
|
import path from "node:path";
|
|
7
10
|
import fs from "node:fs";
|
|
8
11
|
import {
|
|
@@ -24,6 +27,7 @@ import {
|
|
|
24
27
|
NODE_MODULES_PATH,
|
|
25
28
|
containsPath,
|
|
26
29
|
isUtilitySubpath,
|
|
30
|
+
isVitestOwnFile,
|
|
27
31
|
packageNameOf,
|
|
28
32
|
isPackageEntryFile,
|
|
29
33
|
presetPackageOfFile,
|
|
@@ -32,6 +36,7 @@ import {
|
|
|
32
36
|
} from "./match.mjs";
|
|
33
37
|
import {
|
|
34
38
|
createNativeOwnershipPolicy,
|
|
39
|
+
workerOwnershipPolicy,
|
|
35
40
|
isResidentPackageName,
|
|
36
41
|
isRuntimeResidentFile,
|
|
37
42
|
isTestRuntimeResidentFile,
|
|
@@ -168,6 +173,12 @@ function versionable(url) {
|
|
|
168
173
|
return true;
|
|
169
174
|
}
|
|
170
175
|
|
|
176
|
+
let SHARED_RESOLVER = null;
|
|
177
|
+
// experimental.nodeMocks: a module the test mocks is served to Node's ESM imports too
|
|
178
|
+
// (resolveMocked, mockModule), through Node's CommonJS loader's per-file mock step
|
|
179
|
+
// (jest-compat/node-registry.mjs).
|
|
180
|
+
let NODE_MOCKS = false;
|
|
181
|
+
|
|
171
182
|
export function initialize(data) {
|
|
172
183
|
if (data && data.projectRoot) PROJECT_ROOT = data.projectRoot;
|
|
173
184
|
PLATFORM = data?.platform === "android" ? "android" : "ios";
|
|
@@ -177,13 +188,29 @@ export function initialize(data) {
|
|
|
177
188
|
// The project context arrives by reference in `data`: this may be its own thread.
|
|
178
189
|
const context = data?.context ? projectContext(data.context) : null;
|
|
179
190
|
DIAGNOSTICS = context?.diagnostics === true;
|
|
191
|
+
NODE_MOCKS = context?.nodeMocks === true;
|
|
192
|
+
// experimental.ownershipModel: the resolver Vite and the require hooks use, from the
|
|
193
|
+
// same record, checked here against the conditions Node applies in this worker.
|
|
194
|
+
// Never built with the flag off.
|
|
195
|
+
const sharedInputs = ownershipResolverInputs(
|
|
196
|
+
context,
|
|
197
|
+
"ESM loader",
|
|
198
|
+
cjsConditionsWithout("react-native"),
|
|
199
|
+
);
|
|
200
|
+
// The same core as the require hooks: on the Node floor this loader runs in-thread.
|
|
201
|
+
SHARED_RESOLVER = sharedInputs === null ? null : workerSharedResolver(sharedInputs, "esm");
|
|
180
202
|
const configuredOwnership = parseNativeOwnershipManifest(context?.ownership);
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
203
|
+
// The same inputs as the require hooks' (hooks.mjs), so the thread builds one policy.
|
|
204
|
+
ownership = workerOwnershipPolicy(
|
|
205
|
+
{
|
|
206
|
+
projectRoot: PROJECT_ROOT,
|
|
207
|
+
projectDirs: configuredOwnership?.projectDirs ?? [],
|
|
208
|
+
reactNativeRoots: configuredOwnership?.reactNativeRoots ?? [],
|
|
209
|
+
serverDepsInlineAll: configuredOwnership?.enforcement === "overridden-by-inline-all",
|
|
210
|
+
runtimeTransforms: data?.transformPkgs ?? [],
|
|
211
|
+
},
|
|
212
|
+
context,
|
|
213
|
+
);
|
|
187
214
|
isExtra = ownership.matchesNodeTransformedFile;
|
|
188
215
|
workspaceStamp.clear();
|
|
189
216
|
if (data && data.presetExports) presetExports = data.presetExports;
|
|
@@ -260,7 +287,21 @@ function shadowsSubpath(pkg, specifier) {
|
|
|
260
287
|
return leaf !== null && (presetExports[pkg] || []).includes(leaf);
|
|
261
288
|
}
|
|
262
289
|
|
|
263
|
-
|
|
290
|
+
/** Whether the preset redirect (resolveBefore) serves `specifier`. */
|
|
291
|
+
function isPresetSpecifier(specifier) {
|
|
292
|
+
if (Object.prototype.hasOwnProperty.call(presetExports, specifier)) return true;
|
|
293
|
+
if (specifier.endsWith(".json") || isUtilitySubpath(specifier)) return false;
|
|
294
|
+
const specExt = /\.([a-z0-9]+)$/i.exec(specifier);
|
|
295
|
+
if (specExt && assetExtSet.has(specExt[1].toLowerCase())) return false;
|
|
296
|
+
const pkg = packageNameOf(specifier);
|
|
297
|
+
return (
|
|
298
|
+
pkg !== specifier &&
|
|
299
|
+
Object.prototype.hasOwnProperty.call(presetExports, pkg) &&
|
|
300
|
+
shadowsSubpath(pkg, specifier)
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
function resolveBefore(specifier, context, presets = true) {
|
|
264
305
|
// Preset redirect (ESM): a bare import of a preset package — whether from the
|
|
265
306
|
// test graph or, crucially, nested inside an externalized third-party lib — is
|
|
266
307
|
// redirected to a synthetic module that re-exports the runtime preset mock. This
|
|
@@ -271,28 +312,16 @@ function resolveBefore(specifier, context) {
|
|
|
271
312
|
// Exempt: JSON subpaths (package.json version gates), asset subpaths, and
|
|
272
313
|
// Node-safe utility entries (jest-utils, mock, plugin) — those pass through
|
|
273
314
|
// to the real file.
|
|
274
|
-
if (
|
|
315
|
+
if (presets && isPresetSpecifier(specifier)) {
|
|
275
316
|
return { done: { url: PRESET_SCHEME + specifier, shortCircuit: true } };
|
|
276
317
|
}
|
|
277
|
-
if (!specifier.endsWith(".json") && !isUtilitySubpath(specifier)) {
|
|
278
|
-
const specExt = /\.([a-z0-9]+)$/i.exec(specifier);
|
|
279
|
-
if (!specExt || !assetExtSet.has(specExt[1].toLowerCase())) {
|
|
280
|
-
const pkg = packageNameOf(specifier);
|
|
281
|
-
if (
|
|
282
|
-
pkg !== specifier &&
|
|
283
|
-
Object.prototype.hasOwnProperty.call(presetExports, pkg) &&
|
|
284
|
-
shadowsSubpath(pkg, specifier)
|
|
285
|
-
) {
|
|
286
|
-
return { done: { url: PRESET_SCHEME + specifier, shortCircuit: true } };
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
318
|
|
|
291
319
|
const parent =
|
|
292
320
|
context.parentURL && context.parentURL.startsWith("file:")
|
|
293
321
|
? fileURLToPath(context.parentURL)
|
|
294
322
|
: null;
|
|
295
|
-
|
|
323
|
+
// Not when resolving the file a preset stands in for (resolveMocked passes presets: false).
|
|
324
|
+
const presetRequest = presets ? presetRequestForFile(specifier, parent) : null;
|
|
296
325
|
if (presetRequest) {
|
|
297
326
|
const file = specifier.startsWith("file:") ? fileURLToPath(specifier) : specifier;
|
|
298
327
|
const query = new URLSearchParams({ request: presetRequest, file });
|
|
@@ -312,6 +341,14 @@ function resolveBefore(specifier, context) {
|
|
|
312
341
|
// land on a .json file, which still needs the import attribute injected below.
|
|
313
342
|
if (hit) resolved = { url: pathToFileURL(hit).href, shortCircuit: true };
|
|
314
343
|
}
|
|
344
|
+
if (!resolved && SHARED_RESOLVER !== null && parent) {
|
|
345
|
+
// `require` wins, as in the other realms: a package without an `exports` target is
|
|
346
|
+
// answered by Node's CommonJS resolution (`main`).
|
|
347
|
+
const shared = SHARED_RESOLVER.resolve(specifier, parent, (id) =>
|
|
348
|
+
createRequire(parent).resolve(id),
|
|
349
|
+
);
|
|
350
|
+
if (shared) resolved = { url: pathToFileURL(shared).href, shortCircuit: true };
|
|
351
|
+
}
|
|
315
352
|
|
|
316
353
|
return { parent, resolved };
|
|
317
354
|
}
|
|
@@ -412,48 +449,8 @@ function stampedResolutionKey(specifier, context) {
|
|
|
412
449
|
].join("\0");
|
|
413
450
|
}
|
|
414
451
|
|
|
415
|
-
//
|
|
416
|
-
//
|
|
417
|
-
// synchronously on the calling thread, without a cross-thread round trip per
|
|
418
|
-
// resolve and load (see installLoaderHooks in setup.mjs).
|
|
419
|
-
export async function resolve(specifier, context, nextResolve) {
|
|
420
|
-
const before = resolveBefore(specifier, context);
|
|
421
|
-
if (before.done) return before.done;
|
|
422
|
-
let resolved = before.resolved;
|
|
423
|
-
if (!resolved) {
|
|
424
|
-
const key = stampedResolutionKey(specifier, context);
|
|
425
|
-
// A hit skips nextResolve, which Node accepts only from a short-circuited result.
|
|
426
|
-
const hit = key ? stampedResolutions.get(key) : undefined;
|
|
427
|
-
resolved = hit ? { ...hit, shortCircuit: true } : undefined;
|
|
428
|
-
if (!resolved) {
|
|
429
|
-
try {
|
|
430
|
-
resolved = await nextResolve(specifier, context);
|
|
431
|
-
if (key) stampedResolutions.set(key, resolved);
|
|
432
|
-
} catch (err) {
|
|
433
|
-
resolved = resolveRecover(err, specifier, before.parent);
|
|
434
|
-
}
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
|
-
return resolveAfter(resolved, context);
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
/**
|
|
441
|
-
* Whether Node's in-thread hooks (module.registerHooks) carry the engine on `version`:
|
|
442
|
-
* Node 22 from 22.22.3, Node 24 from 24.12.0, and Node 26 on. Measured across Node minors
|
|
443
|
-
* and patches, the releases before those that have the hooks fail with them in-thread
|
|
444
|
-
* while the threaded hooks (module.register) pass: expo-router's elements fail to load
|
|
445
|
-
* ("request for 'node:module' is from a module not been linked", through 22.22.2 and
|
|
446
|
-
* 24.11), and the hot runtime's CommonJS lookup reset stops mattering, so its mutation
|
|
447
|
-
* control passes when it must fail. Node 25 was not measured, and Node 23 is past its
|
|
448
|
-
* end of life, so both keep the threaded hooks.
|
|
449
|
-
*/
|
|
450
|
-
export function inThreadHooksReliable(version) {
|
|
451
|
-
const [major, minor, patch] = version.replace(/^v/, "").split(".").map(Number);
|
|
452
|
-
if (major === 22) return minor > 22 || (minor === 22 && patch >= 3);
|
|
453
|
-
if (major === 24) return minor >= 12;
|
|
454
|
-
return major >= 26;
|
|
455
|
-
}
|
|
456
|
-
|
|
452
|
+
// The hooks run in-thread (module.registerHooks, see installLoaderHooks in setup.mjs):
|
|
453
|
+
// synchronous, on the calling thread, with no cross-thread round trip per resolve and load.
|
|
457
454
|
/**
|
|
458
455
|
* Whether an in-thread hook call (module.registerHooks) comes from `require()` rather
|
|
459
456
|
* than `import`. An import's context always lists its conditions, "import" among them.
|
|
@@ -469,7 +466,55 @@ export function isRequireContext(context) {
|
|
|
469
466
|
}
|
|
470
467
|
|
|
471
468
|
export function resolveSync(specifier, context, nextResolve) {
|
|
472
|
-
|
|
469
|
+
if (NODE_MOCKS) {
|
|
470
|
+
const mocked = resolveMocked(specifier, context, nextResolve);
|
|
471
|
+
if (mocked !== null) return mocked;
|
|
472
|
+
}
|
|
473
|
+
return resolvePlain(specifier, context, nextResolve, true);
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
// experimental.nodeMocks: the query that marks a mocked import's URL (R2). A mocked
|
|
477
|
+
// module is its own entry in Node's ESM cache, apart from Vitest's load of the real
|
|
478
|
+
// file, and a new one for each registration (and so each test file).
|
|
479
|
+
const MOCK_PARAM = "vitest-native-mock";
|
|
480
|
+
|
|
481
|
+
/**
|
|
482
|
+
* A module Node loaded importing a mocked module: the mocked file's URL, marked. Decided
|
|
483
|
+
* here, where the importer is known (R1): Vitest's own loads (its evaluator, loading an
|
|
484
|
+
* original) are never mocked, and a mock outranks a preset, as in the CommonJS
|
|
485
|
+
* dispatcher. Otherwise the resolution it made to find out, when that is the plain one
|
|
486
|
+
* (no preset applies), or null.
|
|
487
|
+
*/
|
|
488
|
+
function resolveMocked(specifier, context, nextResolve) {
|
|
489
|
+
const mockFor = globalThis.__vitest_native_node_mock_for;
|
|
490
|
+
if (mockFor?.markFor === undefined) return null;
|
|
491
|
+
const parent =
|
|
492
|
+
typeof context.parentURL === "string" && context.parentURL.startsWith("file:")
|
|
493
|
+
? fileURLToPath(context.parentURL)
|
|
494
|
+
: null;
|
|
495
|
+
if (parent === null || isVitestOwnFile(parent) || !mockFor.mayReach(specifier, parent)) {
|
|
496
|
+
return null;
|
|
497
|
+
}
|
|
498
|
+
// The file a preset stands in for, which only a mock of it needs, may not resolve at
|
|
499
|
+
// all (a deep path the package does not export): the preset serves it then.
|
|
500
|
+
const preset = isPresetSpecifier(specifier);
|
|
501
|
+
let resolved;
|
|
502
|
+
try {
|
|
503
|
+
resolved = resolvePlain(specifier, context, nextResolve, false);
|
|
504
|
+
} catch (err) {
|
|
505
|
+
if (preset) return null;
|
|
506
|
+
throw err;
|
|
507
|
+
}
|
|
508
|
+
const plain = preset ? null : resolved;
|
|
509
|
+
if (typeof resolved?.url !== "string" || !resolved.url.startsWith("file:")) return plain;
|
|
510
|
+
const mark = mockFor.markFor(fileURLToPath(resolved.url), specifier, parent);
|
|
511
|
+
if (mark === null) return plain;
|
|
512
|
+
const join = resolved.url.includes("?") ? "&" : "?";
|
|
513
|
+
return { ...resolved, url: `${resolved.url}${join}${MOCK_PARAM}=${mark}`, shortCircuit: true };
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
function resolvePlain(specifier, context, nextResolve, presets) {
|
|
517
|
+
const before = resolveBefore(specifier, context, presets);
|
|
473
518
|
if (before.done) return before.done;
|
|
474
519
|
let resolved = before.resolved;
|
|
475
520
|
if (!resolved) {
|
|
@@ -489,10 +534,6 @@ export function resolveSync(specifier, context, nextResolve) {
|
|
|
489
534
|
return resolveAfter(resolved, context);
|
|
490
535
|
}
|
|
491
536
|
|
|
492
|
-
export async function load(url, context, nextLoad) {
|
|
493
|
-
return loadSync(url, context, nextLoad);
|
|
494
|
-
}
|
|
495
|
-
|
|
496
537
|
/**
|
|
497
538
|
* A module this loader would compile, served as a facade that requires it, so Node's
|
|
498
539
|
* CommonJS loader owns the compiled module, compiled there by the require hook
|
|
@@ -552,6 +593,52 @@ export function requireFacade(url, file, compiled) {
|
|
|
552
593
|
};
|
|
553
594
|
}
|
|
554
595
|
|
|
596
|
+
/**
|
|
597
|
+
* A file Node loads as published. True ESM among them is recorded in the ownership ledger
|
|
598
|
+
* (ownership-ledger.mjs), through the engine's dispatcher channel, before Node evaluates
|
|
599
|
+
* the module: this loader runs in the worker's thread (module.registerHooks). CommonJS is
|
|
600
|
+
* never recorded here: Node evaluates it through Module._load, which the ledger observes
|
|
601
|
+
* (nodeDispatch().onNodeEvaluation), and that record knows when the hot reset drops it.
|
|
602
|
+
*/
|
|
603
|
+
function recordModule(url, result) {
|
|
604
|
+
if (result?.format !== "module") return;
|
|
605
|
+
nodeDispatch().channel("ownership").nodeEsm?.(url);
|
|
606
|
+
}
|
|
607
|
+
function loadAsPublished(url, context, nextLoad) {
|
|
608
|
+
const result = nextLoad(url, context);
|
|
609
|
+
if (typeof result?.then === "function") {
|
|
610
|
+
return result.then((settled) => {
|
|
611
|
+
recordModule(url, settled);
|
|
612
|
+
return settled;
|
|
613
|
+
});
|
|
614
|
+
}
|
|
615
|
+
recordModule(url, result);
|
|
616
|
+
return result;
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
/**
|
|
620
|
+
* A mocked module for an ES module Node loaded (R4): an ES module whose default export is
|
|
621
|
+
* the mock's `default` and whose named exports are the mock's own keys, as the test's
|
|
622
|
+
* import reads them. The value comes through Node's CommonJS loader, whose mock step
|
|
623
|
+
* answers it from the same registration (R3).
|
|
624
|
+
*/
|
|
625
|
+
function mockModule(url, file, value) {
|
|
626
|
+
const names = Object.keys(value).filter((name) => name !== "default" && name !== "__esModule");
|
|
627
|
+
return {
|
|
628
|
+
format: "module",
|
|
629
|
+
source: [
|
|
630
|
+
`import { createRequire } from "node:module";`,
|
|
631
|
+
`const m = createRequire(${JSON.stringify(url)})(${JSON.stringify(file)});`,
|
|
632
|
+
`export default m.default;`,
|
|
633
|
+
...names.map(
|
|
634
|
+
(name, i) =>
|
|
635
|
+
`const e${i} = m[${JSON.stringify(name)}];\nexport { e${i} as ${JSON.stringify(name)} };`,
|
|
636
|
+
),
|
|
637
|
+
].join("\n"),
|
|
638
|
+
shortCircuit: true,
|
|
639
|
+
};
|
|
640
|
+
}
|
|
641
|
+
|
|
555
642
|
/**
|
|
556
643
|
* Hot generation facades: what keeps a finished test file's packages from staying in
|
|
557
644
|
* memory for the rest of the run.
|
|
@@ -854,6 +941,13 @@ function loadModule(url, context, nextLoad) {
|
|
|
854
941
|
const file = fileURLToPath(url);
|
|
855
942
|
const norm = file.replace(/\\/g, "/");
|
|
856
943
|
|
|
944
|
+
// Only a URL resolveMocked marked is a mock; every other load of the file is real.
|
|
945
|
+
if (NODE_MOCKS && url.includes(`${MOCK_PARAM}=`)) {
|
|
946
|
+
const mockFor = globalThis.__vitest_native_node_mock_for;
|
|
947
|
+
const value = mockFor === undefined ? undefined : mockFor(file);
|
|
948
|
+
if (value !== undefined && value !== mockFor.NONE) return mockModule(url, file, value);
|
|
949
|
+
}
|
|
950
|
+
|
|
857
951
|
// Asset imports (`import logo from './logo.png'`, `import font from './Icon.ttf'`)
|
|
858
952
|
// from ANY package: Node's ESM loader can't parse a binary asset as a module and
|
|
859
953
|
// throws. Serve the module Metro generates for it (assets.mjs) — the same one the
|
|
@@ -886,7 +980,7 @@ function loadModule(url, context, nextLoad) {
|
|
|
886
980
|
// No source: transformRN reads the file only when it holds no output for it.
|
|
887
981
|
return requireFacade(url, file, transformRN(file, undefined, PROJECT_ROOT, PLATFORM));
|
|
888
982
|
}
|
|
889
|
-
if (!isRN && !isExtra(norm)) return
|
|
983
|
+
if (!isRN && !isExtra(norm)) return loadAsPublished(url, context, nextLoad);
|
|
890
984
|
|
|
891
985
|
if (isRN) {
|
|
892
986
|
// RN's main index exports everything via lazy getters (`module.exports = {
|
|
@@ -921,7 +1015,7 @@ function loadModule(url, context, nextLoad) {
|
|
|
921
1015
|
if (isFlow(src))
|
|
922
1016
|
return requireFacade(url, file, transformRN(file, src, PROJECT_ROOT, PLATFORM));
|
|
923
1017
|
}
|
|
924
|
-
return
|
|
1018
|
+
return loadAsPublished(url, context, nextLoad);
|
|
925
1019
|
}
|
|
926
1020
|
|
|
927
1021
|
// Configured third-party package: transform any JS/TS/JSX source to CJS, served
|
|
@@ -957,8 +1051,8 @@ function loadModule(url, context, nextLoad) {
|
|
|
957
1051
|
// Only compile what Node cannot run as published — see needsTransform. A file V8
|
|
958
1052
|
// accepts is handed straight back to Node, which is how the toolchain packages a
|
|
959
1053
|
// closure walk drags in stop reaching the Babel preset at all.
|
|
960
|
-
if (!needsTransform(file, src)) return
|
|
1054
|
+
if (!needsTransform(file, src)) return loadAsPublished(url, context, nextLoad);
|
|
961
1055
|
return requireFacade(url, file, transformRN(file, src, PROJECT_ROOT, PLATFORM));
|
|
962
1056
|
}
|
|
963
|
-
return
|
|
1057
|
+
return loadAsPublished(url, context, nextLoad);
|
|
964
1058
|
}
|
package/dist/native/match.mjs
CHANGED
|
@@ -35,10 +35,10 @@ export const NODE_MODULES_PATH = /[\\/]node_modules[\\/]/;
|
|
|
35
35
|
export const REACT_NATIVE_PATH = /[\\/]node_modules[\\/](react-native|@react-native)[\\/]/;
|
|
36
36
|
|
|
37
37
|
/**
|
|
38
|
-
* A directory in the form
|
|
39
|
-
* realpathSync.native. The native call expands Windows 8.3 short
|
|
40
|
-
* rewrites letter case on case-insensitive disks, so a directory it
|
|
41
|
-
* to contain the module ids Node hands the matchers.
|
|
38
|
+
* A directory in Node's spelling, the form its module loader reports paths in:
|
|
39
|
+
* fs.realpathSync, not realpathSync.native. The native call expands Windows 8.3 short
|
|
40
|
+
* names (RUNNER~1) and rewrites letter case on case-insensitive disks, so a directory it
|
|
41
|
+
* returns can fail to contain the module ids Node hands the matchers.
|
|
42
42
|
*/
|
|
43
43
|
function canonicalDir(dir) {
|
|
44
44
|
try {
|
|
@@ -244,6 +244,10 @@ export function packagePatterns(name, projectRoot, dirOf = packageDirOf) {
|
|
|
244
244
|
return patterns;
|
|
245
245
|
}
|
|
246
246
|
|
|
247
|
+
const NODE_MODULES_SEGMENT = "/node_modules/";
|
|
248
|
+
// An npm package name: `name` or `@scope/name`, no further path segments.
|
|
249
|
+
const PACKAGE_NAME = /^(?:@[^/]+\/)?[^/@][^/]*$/;
|
|
250
|
+
|
|
247
251
|
/**
|
|
248
252
|
* A predicate over the same patterns Vitest is given, so what the loader transforms
|
|
249
253
|
* and what Vitest externalizes cannot drift apart.
|
|
@@ -253,10 +257,37 @@ export function packagePatterns(name, projectRoot, dirOf = packageDirOf) {
|
|
|
253
257
|
* Node handed them, which on Windows is backslashes.
|
|
254
258
|
*/
|
|
255
259
|
export function buildPkgMatcher(pkgs, projectRoot, dirOf = packageDirOf) {
|
|
256
|
-
|
|
260
|
+
// The same two rules as packagePatterns, answered by lookup: called on every file Node
|
|
261
|
+
// resolves or loads, a test per package made each check linear in the number of
|
|
262
|
+
// packages (hundreds on an Expo project).
|
|
263
|
+
const names = new Set();
|
|
264
|
+
const anchors = new Set();
|
|
265
|
+
// A name that is not a plain package name (a subpath, say) keeps its pattern.
|
|
266
|
+
const others = [];
|
|
267
|
+
for (const name of pkgs || []) {
|
|
268
|
+
if (PACKAGE_NAME.test(name)) names.add(name);
|
|
269
|
+
else others.push(...packagePatterns(name, undefined));
|
|
270
|
+
if (!projectRoot) continue;
|
|
271
|
+
const dir = dirOf(name, projectRoot);
|
|
272
|
+
if (dir && !containsPath(dir, projectRoot)) anchors.add(toPosix(dir).replace(/\/$/, ""));
|
|
273
|
+
}
|
|
257
274
|
return (file) => {
|
|
258
275
|
const norm = toPosix(file);
|
|
259
|
-
|
|
276
|
+
// `[/\\]node_modules[/\\]<name>[/\\]`: the package named after each node_modules segment.
|
|
277
|
+
for (let at = norm.indexOf(NODE_MODULES_SEGMENT); at !== -1;) {
|
|
278
|
+
const start = at + NODE_MODULES_SEGMENT.length;
|
|
279
|
+
let end = norm.indexOf("/", start);
|
|
280
|
+
if (end !== -1 && norm[start] === "@") end = norm.indexOf("/", end + 1);
|
|
281
|
+
if (end !== -1 && names.has(norm.slice(start, end))) return true;
|
|
282
|
+
at = norm.indexOf(NODE_MODULES_SEGMENT, start - 1);
|
|
283
|
+
}
|
|
284
|
+
// `^<dir>[/\\]`: a resolved directory containing the file, found among its parents.
|
|
285
|
+
if (anchors.size > 0) {
|
|
286
|
+
for (let at = norm.indexOf("/", 1); at !== -1; at = norm.indexOf("/", at + 1)) {
|
|
287
|
+
if (anchors.has(norm.slice(0, at))) return true;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
return others.some((re) => re.test(norm));
|
|
260
291
|
};
|
|
261
292
|
}
|
|
262
293
|
|
|
@@ -271,6 +302,31 @@ export function packageNameOf(specifier) {
|
|
|
271
302
|
return specifier.split("/")[0];
|
|
272
303
|
}
|
|
273
304
|
|
|
305
|
+
/**
|
|
306
|
+
* Whether `file` is Vitest's or Vite's own code (the module runner, Vitest's evaluator):
|
|
307
|
+
* a request it makes is Vitest's, such as its load of a mocked module's original.
|
|
308
|
+
*/
|
|
309
|
+
export function isVitestOwnFile(file) {
|
|
310
|
+
return (
|
|
311
|
+
typeof file === "string" &&
|
|
312
|
+
/[\\/]node_modules[\\/](?:vitest|vite|@vitest[\\/][^\\/]+)[\\/]dist[\\/]/.test(file)
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* The package `file` belongs to: the one after its last `node_modules/`, so a package
|
|
318
|
+
* nested inside another's node_modules is its own. Null outside node_modules.
|
|
319
|
+
*/
|
|
320
|
+
export function owningPackageOfFile(file) {
|
|
321
|
+
const norm = toPosix(file);
|
|
322
|
+
const marker = "/node_modules/";
|
|
323
|
+
const at = norm.lastIndexOf(marker);
|
|
324
|
+
if (at === -1) return null;
|
|
325
|
+
const rest = norm.slice(at + marker.length);
|
|
326
|
+
const pkg = packageNameOf(rest);
|
|
327
|
+
return pkg === rest ? null : pkg;
|
|
328
|
+
}
|
|
329
|
+
|
|
274
330
|
// The leaf module name a subpath import points at ("pkg/lib/Swipeable" or
|
|
275
331
|
// "pkg/Swipeable.ios.js" → "Swipeable"), used to pick the matching export off a
|
|
276
332
|
// preset/RN mock. Returns null when there is no usable leaf (trailing slash).
|
|
@@ -290,7 +346,11 @@ const UTILITY_SUBPATH_LEAVES = new Set(["jest-utils", "jestSetup", "mock", "plug
|
|
|
290
346
|
|
|
291
347
|
export function isUtilitySubpath(specifier) {
|
|
292
348
|
const leaf = subpathLeafOf(specifier);
|
|
293
|
-
|
|
349
|
+
if (leaf !== null && UTILITY_SUBPATH_LEAVES.has(leaf)) return true;
|
|
350
|
+
// A package's `jest/` directory is its test tooling (react-native-worklets'
|
|
351
|
+
// jest/resolver.js, which reanimated's own resolver requires by name).
|
|
352
|
+
const pkg = packageNameOf(specifier);
|
|
353
|
+
return pkg !== specifier && specifier.slice(pkg.length + 1).split("/")[0] === "jest";
|
|
294
354
|
}
|
|
295
355
|
|
|
296
356
|
// The preset package a RESOLVED file belongs to, when the import that reached it came
|
|
@@ -9,11 +9,11 @@
|
|
|
9
9
|
// Anything the worker loaded to bootstrap ITSELF stays; anything a TEST FILE caused
|
|
10
10
|
// to load is dropped and runs again on the next file.
|
|
11
11
|
import Module from "node:module";
|
|
12
|
+
import { nodeDispatch } from "../node-dispatch.mjs";
|
|
12
13
|
import { isRuntimeResidentFile } from "./ownership.mjs";
|
|
13
14
|
import { VitestNativeError } from "../errors.mjs";
|
|
14
15
|
|
|
15
16
|
// Capture before the worker installs registry/preset _load interceptors.
|
|
16
|
-
const nodeLoad = Module._load;
|
|
17
17
|
const CLEANUP_ID = "\0vitest-native:cjs-cache-drain";
|
|
18
18
|
|
|
19
19
|
function cleanupError(detail, cause) {
|
|
@@ -43,23 +43,43 @@ function cleanupError(detail, cause) {
|
|
|
43
43
|
* must run immediately after cache deletion, before the next file starts loading.
|
|
44
44
|
*/
|
|
45
45
|
export function trackCjsResolutions() {
|
|
46
|
-
const
|
|
46
|
+
const dispatch = nodeDispatch();
|
|
47
47
|
const edges = new Map();
|
|
48
48
|
const sentinel = {};
|
|
49
49
|
let count = 0;
|
|
50
|
-
|
|
51
|
-
|
|
50
|
+
// The outermost resolve step (node-dispatch.mjs, RESOLVE_STEPS): it records the file
|
|
51
|
+
// every other step settled on.
|
|
52
|
+
const record = (request, parent, filename) => {
|
|
52
53
|
if (parent && typeof parent.path === "string" && !Module.isBuiltin(filename)) {
|
|
53
54
|
edges.set(`${parent.path}\0${request}`, { request, parentPath: parent.path, filename });
|
|
54
55
|
}
|
|
55
|
-
return filename;
|
|
56
56
|
};
|
|
57
|
-
|
|
57
|
+
const added = dispatch.resolve.add(
|
|
58
|
+
"tracker",
|
|
59
|
+
(next) =>
|
|
60
|
+
function (request, parent, ...rest) {
|
|
61
|
+
const filename = next(request, parent, ...rest);
|
|
62
|
+
record(request, parent, filename);
|
|
63
|
+
return filename;
|
|
64
|
+
},
|
|
65
|
+
);
|
|
66
|
+
if (!added) throw cleanupError("a resolution tracker is already installed in this worker.");
|
|
67
|
+
// Node's relative-resolve cache keys a load by the request `Module._load` received. A
|
|
68
|
+
// resolver installed above the dispatcher (between its entry and this tracker) can
|
|
69
|
+
// hand the resolve steps another request, so each of Node's own loads is recorded
|
|
70
|
+
// under the request it was made with too.
|
|
71
|
+
const stopLoads = dispatch.onNodeLoad(({ request, parent, filename }) =>
|
|
72
|
+
record(request, parent, filename),
|
|
73
|
+
);
|
|
74
|
+
// What resolution was entered through when tracking began. A resolver installed after
|
|
75
|
+
// it could answer a request without reaching the tracker, so a drain refuses to run
|
|
76
|
+
// under one.
|
|
77
|
+
const tracked = Module._resolveFilename;
|
|
58
78
|
return {
|
|
59
79
|
drain() {
|
|
60
80
|
// A later wrapper can short-circuit our tracker, leaving *zero* recorded
|
|
61
81
|
// edges despite stale Node lookups. Check before the empty-map fast path.
|
|
62
|
-
if (Module._resolveFilename !==
|
|
82
|
+
if (Module._resolveFilename !== tracked) {
|
|
63
83
|
throw cleanupError("the tracking resolver was replaced after installation.");
|
|
64
84
|
}
|
|
65
85
|
const stale = [];
|
|
@@ -73,26 +93,30 @@ export function trackCjsResolutions() {
|
|
|
73
93
|
if (Object.hasOwn(Module._cache, CLEANUP_ID)) {
|
|
74
94
|
throw cleanupError("the reserved cleanup cache entry is already in use.");
|
|
75
95
|
}
|
|
76
|
-
const savedResolve = Module._resolveFilename;
|
|
77
96
|
const savedLoad = Module.prototype.load;
|
|
78
97
|
Module._cache[CLEANUP_ID] = { loaded: true, exports: sentinel };
|
|
79
|
-
Module._resolveFilename = () => CLEANUP_ID;
|
|
80
98
|
Module.prototype.load = () => {
|
|
81
99
|
throw cleanupError("cache-drain attempted module execution.");
|
|
82
100
|
};
|
|
83
101
|
try {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
102
|
+
// Node's own loader, with every resolution answering the cleanup entry, so
|
|
103
|
+
// each stale request reaches Node's relative-resolve cache and nothing else.
|
|
104
|
+
dispatch.withResolver(
|
|
105
|
+
() => CLEANUP_ID,
|
|
106
|
+
() => {
|
|
107
|
+
for (const { request, parentPath } of stale) {
|
|
108
|
+
const parent = { path: parentPath, children: [] };
|
|
109
|
+
if (dispatch.nodeLoad.call(Module, request, parent, false) !== sentinel) {
|
|
110
|
+
throw cleanupError("a loader bypassed the cleanup resolver.");
|
|
111
|
+
}
|
|
112
|
+
count++;
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
);
|
|
91
116
|
} catch (error) {
|
|
92
117
|
if (error?.code === "HOT_CJS_CACHE_RESET") throw error;
|
|
93
118
|
throw cleanupError("the cleanup lookup failed.", error);
|
|
94
119
|
} finally {
|
|
95
|
-
Module._resolveFilename = savedResolve;
|
|
96
120
|
Module.prototype.load = savedLoad;
|
|
97
121
|
delete Module._cache[CLEANUP_ID];
|
|
98
122
|
}
|
|
@@ -107,10 +131,11 @@ export function trackCjsResolutions() {
|
|
|
107
131
|
// Used by standalone contract tests; the production tracker lives as long
|
|
108
132
|
// as its worker. Never remove a subsequently installed third-party wrapper.
|
|
109
133
|
dispose() {
|
|
110
|
-
if (Module._resolveFilename !==
|
|
134
|
+
if (Module._resolveFilename !== tracked) {
|
|
111
135
|
throw cleanupError("another resolver wrapped the tracking hook.");
|
|
112
136
|
}
|
|
113
|
-
|
|
137
|
+
dispatch.resolve.remove("tracker");
|
|
138
|
+
stopLoads();
|
|
114
139
|
edges.clear();
|
|
115
140
|
},
|
|
116
141
|
};
|
|
@@ -180,34 +205,11 @@ const VITEST_RUNTIME = [/\/vitest\/dist\//, /vitest-virtual-\w+\/dist/, /@vitest
|
|
|
180
205
|
* own: the fields Vitest's `resetModules(modules, resetMocks = true)` clears
|
|
181
206
|
* (vitest/dist/chunks/utils). With `saved`, each node's state is recorded first, for
|
|
182
207
|
* `restoreEvaluatedModules`.
|
|
183
|
-
*
|
|
184
|
-
* Beyond Vitest's fields, a node that stands in for a factory or redirect mock is
|
|
185
|
-
* invalidated outright. Vitest 4 fetches a module that is mocked that way as a
|
|
186
|
-
* placeholder whose `meta` is empty code plus `mockedModule`, the mock as registered
|
|
187
|
-
* then (startVitestModuleRunner chunk, the transport's `fetchModule`). On a later
|
|
188
|
-
* request, Vitest 4.1's `VitestModuleRunner.cachedRequest` replays that
|
|
189
|
-
* `meta.mockedModule` whenever the CURRENT registry has any mock for the id, and only
|
|
190
|
-
* looks the module up again when it has none. Vitest 5 asks the current registry
|
|
191
|
-
* instead (index chunk, `cachedRequest`). So under the hot runtime, where the graph
|
|
192
|
-
* outlives the file, a file that automocks a module the previous file had mocked with
|
|
193
|
-
* a factory got the previous file's factory. Per-file isolation never sees this: the
|
|
194
|
-
* graph is new for each file. Stock Vitest 4.1 with `isolate: false` leaks the same
|
|
195
|
-
* way, since its `resetModules` keeps `meta` too. `invalidateModule` is the module runner's own reset for
|
|
196
|
-
* a node (vite/module-runner, `EvaluatedModules`): it clears `meta`, so the next
|
|
197
|
-
* request fetches the module again, under the mocks of whoever asks. Dropping only
|
|
198
|
-
* `meta.mockedModule` is not enough: the placeholder's empty code would then be served
|
|
199
|
-
* as the module itself to a file that does not mock it.
|
|
200
|
-
*
|
|
201
|
-
* Only this reset does so. On Vitest 4.1 a factoryless `vi.doMock` that follows a
|
|
202
|
-
* `vi.doMock` with a factory in the same file still gets the factory when nothing
|
|
203
|
-
* between them resets the graph, or only `vi.resetModules()` does, since it keeps
|
|
204
|
-
* `meta`. That is stock Vitest 4.1 behaviour, left as it is.
|
|
205
208
|
*/
|
|
206
209
|
export function resetEvaluatedModules(modules, saved) {
|
|
207
210
|
for (const [id, node] of modules.idToModuleMap) {
|
|
208
211
|
if (VITEST_RUNTIME.some((re) => re.test(id))) continue;
|
|
209
212
|
saved?.set(node, [node.promise, node.exports, node.evaluated, [...node.importers]]);
|
|
210
|
-
if (node.meta && "mockedModule" in node.meta) modules.invalidateModule(node);
|
|
211
213
|
node.promise = undefined;
|
|
212
214
|
node.exports = undefined;
|
|
213
215
|
node.evaluated = false;
|
|
@@ -217,13 +219,6 @@ export function resetEvaluatedModules(modules, saved) {
|
|
|
217
219
|
|
|
218
220
|
/**
|
|
219
221
|
* Put back what `resetEvaluatedModules` saved; nodes evaluated since are reset.
|
|
220
|
-
*
|
|
221
|
-
* A placeholder's `meta` is not put back. Every request fetches `meta` anew
|
|
222
|
-
* (vite/module-runner, `getModuleInformation`): the transport answers a factory or
|
|
223
|
-
* redirect mock with a new placeholder, and only a module with no such mock is served
|
|
224
|
-
* from the node as it stands. So the restored `meta` would only matter on Vitest 4.1
|
|
225
|
-
* once the module is automocked, and there it would replay the factory from before the
|
|
226
|
-
* block (jest-registry-isolate-modules.test.ts).
|
|
227
222
|
*/
|
|
228
223
|
export function restoreEvaluatedModules(modules, saved) {
|
|
229
224
|
resetEvaluatedModules(modules);
|