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/hooks.mjs
CHANGED
|
@@ -1,34 +1,48 @@
|
|
|
1
1
|
// Patches Node's CJS loader so RN's internal require() chains are Flow-stripped and
|
|
2
2
|
// native-boundary modules are mocked. The companion loader.mjs handles the import() path.
|
|
3
|
+
import { compileSupport, loadSupport } from "./library-jest.mjs";
|
|
3
4
|
import Module from "node:module";
|
|
4
|
-
import {
|
|
5
|
+
import { nodeDispatch } from "../node-dispatch.mjs";
|
|
6
|
+
import { CONTEXT_ENV, projectContext } from "../context.mjs";
|
|
5
7
|
import { VitestNativeError } from "../errors.mjs";
|
|
6
8
|
import path from "node:path";
|
|
7
9
|
import fs from "node:fs";
|
|
8
10
|
import { heldTransform, transformRN, isFlow, needsTransform } from "./transform.mjs";
|
|
9
11
|
import { boundarySourceFor } from "./boundary.mjs";
|
|
10
12
|
import {
|
|
13
|
+
extensionsFor,
|
|
11
14
|
hasKnownExtension,
|
|
12
15
|
resolvePlatformFile,
|
|
13
16
|
resolveRelativePlatformFile,
|
|
17
|
+
resolveRelativeWithExtensions,
|
|
14
18
|
resolveDeepPackageFile,
|
|
19
|
+
resolvePackageMainField,
|
|
20
|
+
isWholePackageRequest,
|
|
15
21
|
} from "./resolve.mjs";
|
|
16
22
|
import {
|
|
17
23
|
NODE_MODULES_PATH,
|
|
18
24
|
isUtilitySubpath,
|
|
25
|
+
owningPackageOfFile,
|
|
19
26
|
packageNameOf,
|
|
20
27
|
isPackageEntryFile,
|
|
21
28
|
subpathLeafOf,
|
|
22
29
|
} from "./match.mjs";
|
|
23
30
|
import {
|
|
24
|
-
createNativeOwnershipPolicy,
|
|
25
31
|
findNativeOwnershipConflict,
|
|
26
32
|
parseNativeOwnershipManifest,
|
|
33
|
+
workerOwnershipPolicy,
|
|
27
34
|
} from "./ownership.mjs";
|
|
28
35
|
import { explainUntransformedSyntaxError } from "./explain.mjs";
|
|
29
36
|
import { nativeAssetModuleSource } from "./assets.mjs";
|
|
30
37
|
import { expandAlias } from "../jest-compat/aliases.mjs";
|
|
31
38
|
import { noteIfRntlEntryLoaded } from "../rntl-support.mjs";
|
|
39
|
+
import { cjsConditionsWithout } from "./export-condition-recovery.mjs";
|
|
40
|
+
import {
|
|
41
|
+
ownershipResolverInputs,
|
|
42
|
+
ownershipShadowFile,
|
|
43
|
+
workerSharedResolver,
|
|
44
|
+
} from "./shared-resolver.mjs";
|
|
45
|
+
import { libraryExtensionsFor } from "./jest-resolvers.mjs";
|
|
32
46
|
|
|
33
47
|
// Guarded via globalThis, not module scope: under the hot runtime this module
|
|
34
48
|
// can be evaluated twice in one worker (once by the worker entry through Node's
|
|
@@ -311,10 +325,7 @@ export function installRequireHooks(
|
|
|
311
325
|
reactNativeRoots: configuredOwnership?.reactNativeRoots ?? [],
|
|
312
326
|
serverDepsInlineAll: configuredOwnership?.enforcement === "overridden-by-inline-all",
|
|
313
327
|
};
|
|
314
|
-
let ownership =
|
|
315
|
-
...ownershipBase,
|
|
316
|
-
runtimeTransforms: transformPkgs,
|
|
317
|
-
});
|
|
328
|
+
let ownership = workerOwnershipPolicy({ ...ownershipBase, runtimeTransforms: transformPkgs });
|
|
318
329
|
let isExtra = ownership.matchesNodeTransformedFile;
|
|
319
330
|
let isExtraKey = JSON.stringify(transformPkgs);
|
|
320
331
|
// Whether these hooks compile a package file for the project, read at call time so a
|
|
@@ -338,7 +349,7 @@ export function installRequireHooks(
|
|
|
338
349
|
const key = JSON.stringify(pkgs);
|
|
339
350
|
if (key === isExtraKey) return compiles;
|
|
340
351
|
isExtraKey = key;
|
|
341
|
-
ownership =
|
|
352
|
+
ownership = workerOwnershipPolicy({ ...ownershipBase, runtimeTransforms: pkgs });
|
|
342
353
|
isExtra = ownership.matchesNodeTransformedFile;
|
|
343
354
|
return compiles;
|
|
344
355
|
};
|
|
@@ -407,8 +418,16 @@ export function installRequireHooks(
|
|
|
407
418
|
// globalThis.__vitest_native_preset_require), so the two cannot disagree.
|
|
408
419
|
const NO_PRESET = Symbol("no preset");
|
|
409
420
|
function presetExportsFor(request, parent) {
|
|
421
|
+
const fromFile = parent?.filename;
|
|
410
422
|
const mocks = globalThis.__vitest_native_preset_mocks;
|
|
411
423
|
if (!mocks) return NO_PRESET;
|
|
424
|
+
// jest.requireActual of a shadowed package: this one load is the package itself
|
|
425
|
+
// (jest-compat/node-registry.mjs), as Jest's requireActual gives it.
|
|
426
|
+
const registry = globalThis.__vitest_native_jest_registry;
|
|
427
|
+
if (registry !== undefined && registry.presetActual === request) {
|
|
428
|
+
registry.presetActual = undefined;
|
|
429
|
+
return NO_PRESET;
|
|
430
|
+
}
|
|
412
431
|
if (Object.prototype.hasOwnProperty.call(mocks, request)) return mocks[request];
|
|
413
432
|
// Subpath require of a preset package — the real deep entry would load the
|
|
414
433
|
// package's native runtime. Exempt: JSON subpaths (package.json version
|
|
@@ -420,8 +439,12 @@ export function installRequireHooks(
|
|
|
420
439
|
if (reqExt !== "json" && !assetExtSet.has(reqExt) && !isUtilitySubpath(request)) {
|
|
421
440
|
const pkg = packageNameOf(request);
|
|
422
441
|
// Only a subpath whose leaf the mock exports is the mock's (loader.mjs,
|
|
423
|
-
// shadowsSubpath); any other is the real file, as under Jest's jest.mock(pkg).
|
|
424
|
-
|
|
442
|
+
// shadowsSubpath); any other is the real file, as under Jest's jest.mock(pkg). A
|
|
443
|
+
// package's request for one of its own files is the package's business, as a
|
|
444
|
+
// jest.mock of the package name leaves it alone: reanimated's own mock loads the
|
|
445
|
+
// real package, which requires react-native-reanimated/scripts/… by name.
|
|
446
|
+
const fromItself = typeof fromFile === "string" && owningPackageOfFile(fromFile) === pkg;
|
|
447
|
+
if (pkg !== request && !fromItself && Object.prototype.hasOwnProperty.call(mocks, pkg)) {
|
|
425
448
|
if (presetExportsLeaf(pkg, request)) return presetSubpathExports(mocks, pkg, request);
|
|
426
449
|
if (parent !== undefined && isPackageEntry(pkg, request, parent)) return mocks[pkg];
|
|
427
450
|
}
|
|
@@ -433,19 +456,23 @@ export function installRequireHooks(
|
|
|
433
456
|
return shadowed === NO_PRESET ? Module._load(file, null, false) : shadowed;
|
|
434
457
|
};
|
|
435
458
|
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
459
|
+
// A preset beats the registry and Node, and yields to a per-file mock (LOAD_STEPS).
|
|
460
|
+
nodeDispatch().load.add(
|
|
461
|
+
"preset",
|
|
462
|
+
(next) =>
|
|
463
|
+
function (request, parent, ...rest) {
|
|
464
|
+
const shadowed = presetExportsFor(request, parent ?? null);
|
|
465
|
+
if (shadowed !== NO_PRESET) return shadowed;
|
|
466
|
+
const exports = next(request, parent, ...rest);
|
|
467
|
+
// Every require of RNTL's main entry, cached or not, tells the setup file's
|
|
468
|
+
// auto-cleanup that this test file loaded it (see noteIfRntlEntryLoaded). Vite's
|
|
469
|
+
// evaluator reports the file's own imports (rntl-hooks.mjs); this sees the entry
|
|
470
|
+
// reached through a package Node loads, such as a shared test-utils that
|
|
471
|
+
// re-exports RNTL.
|
|
472
|
+
noteIfRntlEntryLoaded(exports);
|
|
473
|
+
return exports;
|
|
474
|
+
},
|
|
475
|
+
);
|
|
449
476
|
|
|
450
477
|
let requireAliases;
|
|
451
478
|
const resolveAliasedRequest = (request, parent, resolveTarget) => {
|
|
@@ -482,6 +509,7 @@ export function installRequireHooks(
|
|
|
482
509
|
key = path.resolve(file);
|
|
483
510
|
if (!preserveSymlinks) {
|
|
484
511
|
try {
|
|
512
|
+
// Node's spelling (fs.realpathSync): the key Module._cache holds the file under (#282).
|
|
485
513
|
key = fs.realpathSync(key);
|
|
486
514
|
} catch {
|
|
487
515
|
// Missing: leave it to the loader to report.
|
|
@@ -549,6 +577,12 @@ export function installRequireHooks(
|
|
|
549
577
|
misses: 0,
|
|
550
578
|
stale: 0,
|
|
551
579
|
});
|
|
580
|
+
// Node's in-thread module hooks resolve a require with `{ conditions }` (Node 26 adds
|
|
581
|
+
// `paths: undefined` to it); any other option that is set (`require.resolve`'s
|
|
582
|
+
// `paths`) is Node's to answer.
|
|
583
|
+
const onlyConditions = (options) =>
|
|
584
|
+
options == null ||
|
|
585
|
+
!Object.keys(options).some((name) => name !== "conditions" && options[name] !== undefined);
|
|
552
586
|
const resolutionKey = (request, parent, [isMain, options]) => {
|
|
553
587
|
if (typeof request !== "string" || typeof parent?.path !== "string" || isMain) return null;
|
|
554
588
|
// A relative request with an extension, and an absolute one, Node answers from
|
|
@@ -557,95 +591,197 @@ export function installRequireHooks(
|
|
|
557
591
|
return null;
|
|
558
592
|
}
|
|
559
593
|
if (Module.isBuiltin(request)) return null;
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
// `paths`) is left to Node.
|
|
563
|
-
let conditions = "";
|
|
564
|
-
if (options != null) {
|
|
565
|
-
const set = (name) => options[name] !== undefined;
|
|
566
|
-
if (Object.keys(options).some((name) => name !== "conditions" && set(name))) return null;
|
|
567
|
-
if (options.conditions != null) conditions = [...options.conditions].join(",");
|
|
568
|
-
}
|
|
594
|
+
if (!onlyConditions(options)) return null;
|
|
595
|
+
const conditions = options?.conditions != null ? [...options.conditions].join(",") : "";
|
|
569
596
|
return `${parent.path}\0${request}\0${conditions}`;
|
|
570
597
|
};
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
598
|
+
// experimental.ownershipModel: bare requests resolve through the resolver Vite and
|
|
599
|
+
// the ESM loader use, built from the context's record, which this realm first checks
|
|
600
|
+
// against the conditions Node applies here. Never built with the flag off. The memo
|
|
601
|
+
// above holds its answers, so it keeps none of its own.
|
|
602
|
+
const ownershipContext = projectContext();
|
|
603
|
+
const sharedInputs = ownershipResolverInputs(
|
|
604
|
+
ownershipContext,
|
|
605
|
+
"CommonJS hooks",
|
|
606
|
+
cjsConditionsWithout("react-native"),
|
|
607
|
+
);
|
|
608
|
+
// The resolution shadow, under diagnostics only: it resolves each distinct request a
|
|
609
|
+
// second time, the old way, to record where the answers differ.
|
|
610
|
+
const shadowFile =
|
|
611
|
+
sharedInputs !== null && ownershipContext.diagnostics === true
|
|
612
|
+
? ownershipShadowFile(process.env[CONTEXT_ENV], ownershipContext.resolverInputsHash)
|
|
613
|
+
: null;
|
|
614
|
+
const sharedResolver =
|
|
615
|
+
sharedInputs === null
|
|
616
|
+
? null
|
|
617
|
+
: workerSharedResolver(sharedInputs, "cjs", { shadowFile, globalFolders: true });
|
|
618
|
+
// A caller passing other conditions than this realm's (none does but a direct
|
|
619
|
+
// `Module._resolveFilename` call) gets Node's own answer for them.
|
|
620
|
+
const recordConditions = new Set(ownershipContext?.resolverInputs?.conditions ?? []);
|
|
621
|
+
const conditionsMatchRecord = (conditions) =>
|
|
622
|
+
conditions == null ||
|
|
623
|
+
(() => {
|
|
624
|
+
const asked = new Set([...conditions].filter((c) => c !== "react-native"));
|
|
625
|
+
return (
|
|
626
|
+
asked.size === recordConditions.size && [...asked].every((c) => recordConditions.has(c))
|
|
627
|
+
);
|
|
628
|
+
})();
|
|
629
|
+
const onlyDefaultOptions = (options, parent) =>
|
|
630
|
+
options == null ||
|
|
631
|
+
Object.keys(options).every(
|
|
632
|
+
(name) =>
|
|
633
|
+
name === "conditions" ||
|
|
634
|
+
(name === "paths" && options.paths === undefined) ||
|
|
635
|
+
(name === "paths" &&
|
|
636
|
+
Array.isArray(options.paths) &&
|
|
637
|
+
Array.isArray(parent?.paths) &&
|
|
638
|
+
options.paths.length === parent.paths.length &&
|
|
639
|
+
options.paths.every((entry, i) => entry === parent.paths[i])),
|
|
640
|
+
);
|
|
641
|
+
nodeDispatch().resolve.add(
|
|
642
|
+
"hooks",
|
|
643
|
+
(next) =>
|
|
644
|
+
function (request, parent, ...rest) {
|
|
645
|
+
let resolved;
|
|
646
|
+
const key = resolutionKey(request, parent, rest);
|
|
647
|
+
if (key !== null) {
|
|
648
|
+
const held = resolutions.get(key);
|
|
649
|
+
// A bare request's hit costs one stat: a file removed since (a nested copy
|
|
650
|
+
// uninstalled, a fixture a test deleted) is resolved again, as Node would, instead
|
|
651
|
+
// of failing to load. A relative one is answered as the platform resolution's own
|
|
652
|
+
// cache would answer it (resolve.mjs), which does not look at the disk again.
|
|
653
|
+
if (
|
|
654
|
+
held !== undefined &&
|
|
655
|
+
(request.startsWith(".") || fs.statSync(held, { throwIfNoEntry: false })?.isFile())
|
|
656
|
+
) {
|
|
657
|
+
resolutionStats.hits++;
|
|
658
|
+
resolved = held;
|
|
659
|
+
} else {
|
|
660
|
+
if (held !== undefined) {
|
|
661
|
+
resolutionStats.stale++;
|
|
662
|
+
resolutions.delete(key);
|
|
663
|
+
// The shared resolver held the same answer: resolve it afresh there too.
|
|
664
|
+
sharedResolver?.forget(request, parent?.filename);
|
|
665
|
+
}
|
|
666
|
+
resolutionStats.misses++;
|
|
667
|
+
}
|
|
591
668
|
}
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
//
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
(
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
669
|
+
// After the memo, so a held answer whose file is gone is resolved afresh here too.
|
|
670
|
+
// `require.resolve(x, { paths })` and the main module are Node's to answer. Not
|
|
671
|
+
// tied to the memo's key: Node 26's module hooks pass a plain require an options
|
|
672
|
+
// object with `paths: undefined` beside its conditions, which is no
|
|
673
|
+
// `require.resolve` option, so this realm still answers it.
|
|
674
|
+
if (
|
|
675
|
+
!resolved &&
|
|
676
|
+
sharedResolver !== null &&
|
|
677
|
+
typeof request === "string" &&
|
|
678
|
+
!request.startsWith(".") &&
|
|
679
|
+
rest[0] !== true &&
|
|
680
|
+
onlyDefaultOptions(rest[1], parent) &&
|
|
681
|
+
conditionsMatchRecord(rest[1]?.conditions)
|
|
682
|
+
) {
|
|
683
|
+
resolved =
|
|
684
|
+
sharedResolver.resolve(request, parent?.filename, (target) =>
|
|
685
|
+
next(target, parent, ...rest),
|
|
686
|
+
) ?? undefined;
|
|
687
|
+
if (resolved !== undefined && shadowFile !== null) {
|
|
688
|
+
// The shadow: what Node alone would have answered, once per distinct request
|
|
689
|
+
// (the memo holds the answer after this). Node failing where the resolver
|
|
690
|
+
// answers (a regex alias) is a delta too.
|
|
691
|
+
let previous;
|
|
692
|
+
try {
|
|
693
|
+
previous = next(request, parent, ...rest);
|
|
694
|
+
} catch (error) {
|
|
695
|
+
previous = `unresolved (${error?.code ?? error})`;
|
|
696
|
+
}
|
|
697
|
+
sharedResolver.shadow(request, parent?.filename, resolved, previous);
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
// Relative, extensionless: Metro's platform order for every parent, app source
|
|
701
|
+
// included (`./PlatformInfo` → `index.native.ts` before `index.ts`).
|
|
702
|
+
if (
|
|
703
|
+
!resolved &&
|
|
704
|
+
parent?.filename &&
|
|
705
|
+
request.startsWith(".") &&
|
|
706
|
+
!hasKnownExtension(request, activeSourceExts, assetExtSet)
|
|
707
|
+
) {
|
|
708
|
+
// A library's own Jest resolver can narrow the extensions for its files
|
|
709
|
+
// (jest-resolvers.mjs: worklets' and reanimated's JS implementations).
|
|
710
|
+
const narrowed = libraryExtensionsFor(
|
|
711
|
+
projectRoot,
|
|
712
|
+
request,
|
|
713
|
+
parent.filename,
|
|
714
|
+
extensionsFor(platform, activeSourceExts),
|
|
715
|
+
);
|
|
716
|
+
resolved = narrowed
|
|
717
|
+
? resolveRelativeWithExtensions(request, parent.filename, narrowed)
|
|
718
|
+
: resolveRelativePlatformFile(request, parent.filename, platform, activeSourceExts);
|
|
719
|
+
if (resolved) resolved = asNodeResolves(resolved);
|
|
720
|
+
}
|
|
721
|
+
// A whole package with a `react-native` field: the file Metro and Vite's import
|
|
722
|
+
// resolve it to, so an import and a require are one module (resolve.mjs). Only a
|
|
723
|
+
// file these hooks can load: TypeScript, or a file they compile; a package they do
|
|
724
|
+
// not compile keeps its `main`, which Node loads as it is.
|
|
725
|
+
if (
|
|
726
|
+
!resolved &&
|
|
727
|
+
parent?.filename &&
|
|
728
|
+
onlyConditions(rest[1]) &&
|
|
729
|
+
isWholePackageRequest(request)
|
|
730
|
+
) {
|
|
731
|
+
const entry = resolvePackageMainField(
|
|
732
|
+
request,
|
|
733
|
+
path.dirname(parent.filename),
|
|
734
|
+
platform,
|
|
735
|
+
activeSourceExts,
|
|
736
|
+
);
|
|
737
|
+
if (entry && (/\.[cm]?tsx?$/.test(entry) || compiles(entry))) {
|
|
738
|
+
resolved = asNodeResolves(entry);
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
if (!resolved) {
|
|
742
|
+
try {
|
|
743
|
+
resolved = next(request, parent, ...rest);
|
|
744
|
+
} catch (err) {
|
|
745
|
+
// The project's aliases (resolve.alias string entries, and tsconfig `paths` when
|
|
746
|
+
// Vite resolves them for imports): a `require('#/lib/x')` in a test reaches here,
|
|
747
|
+
// not Vite. Only for requests from project files — packages never see the
|
|
748
|
+
// project's aliases, under Vite or Metro — and only once Node has failed, so no
|
|
749
|
+
// resolution that worked before can change.
|
|
750
|
+
const aliased = resolveAliasedRequest(request, parent, (target) =>
|
|
751
|
+
next(target, parent, ...rest),
|
|
752
|
+
);
|
|
753
|
+
// RN 0.87's exports map rejects the deep self-references its own Babel
|
|
754
|
+
// preset emits (`react-native/src/private/…`); Metro resolves them via the
|
|
755
|
+
// `react-native-legacy-deep-imports` condition. Mirror Metro by path.
|
|
756
|
+
// From the requiring file first, as Metro does; then from the project, for a
|
|
757
|
+
// parent outside the project's tree — the precompiled registry's cache file
|
|
758
|
+
// when the cache had to fall back to tmpdir.
|
|
759
|
+
const fromDir = parent?.filename ? path.dirname(parent.filename) : projectRoot;
|
|
760
|
+
const deep =
|
|
761
|
+
aliased ??
|
|
762
|
+
resolveDeepPackageFile(request, fromDir, platform, activeSourceExts) ??
|
|
763
|
+
(fromDir === projectRoot
|
|
764
|
+
? null
|
|
765
|
+
: resolveDeepPackageFile(request, projectRoot, platform, activeSourceExts));
|
|
766
|
+
if (deep === null) throw err;
|
|
767
|
+
resolved = asNodeResolves(deep);
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
if (key !== null) resolutions.set(key, resolved);
|
|
771
|
+
// Diagnostics only. The comparison sees Node's resolution and the package
|
|
772
|
+
// manifest, never Vite's module graph, so it cannot tell a package both graphs
|
|
773
|
+
// load from one only Node ever requires — and the ecosystem is full of the
|
|
774
|
+
// latter (Babel's source-map packages, RNTL's own test-renderer). Reported as
|
|
775
|
+
// noise on every test file (#201); kept as an opt-in diagnostic for the silent
|
|
776
|
+
// duplicate-instance failure it was written to surface.
|
|
777
|
+
if (projectContext()?.diagnostics === true) {
|
|
778
|
+
checkResolverAgreement(request, resolved);
|
|
779
|
+
}
|
|
780
|
+
// Every successful resolution (including platform hits) crosses this boundary.
|
|
781
|
+
checkProjectSourceResolvedByNode(resolved, parent, projectDirs(), ownership);
|
|
782
|
+
return resolved;
|
|
783
|
+
},
|
|
784
|
+
);
|
|
649
785
|
|
|
650
786
|
const origJs = Module._extensions[".js"];
|
|
651
787
|
const expoSdks = projectContext()?.expoSdks;
|
|
@@ -661,12 +797,12 @@ export function installRequireHooks(
|
|
|
661
797
|
// registry compiles without the isFlow test below.
|
|
662
798
|
if (rnPath === null && isExtra(norm)) {
|
|
663
799
|
const held = heldTransform(filename, projectRoot, platform);
|
|
664
|
-
if (held !== undefined) return mod
|
|
800
|
+
if (held !== undefined) return compileSupport(mod, held, filename);
|
|
665
801
|
}
|
|
666
802
|
if (rnPath !== null) {
|
|
667
803
|
const src = fs.readFileSync(filename, "utf8");
|
|
668
804
|
if (isFlow(src))
|
|
669
|
-
return mod
|
|
805
|
+
return compileSupport(mod, transformRN(filename, src, projectRoot, platform), filename);
|
|
670
806
|
} else if (isExtra(norm)) {
|
|
671
807
|
// Selected for compiling — but only compile what Node cannot run as published.
|
|
672
808
|
// `isFlow` is not enough here (TS `import type` and JSX slip past it), and
|
|
@@ -674,9 +810,9 @@ export function installRequireHooks(
|
|
|
674
810
|
// needsTransform: V8 answers the question Node is about to ask.
|
|
675
811
|
const src = fs.readFileSync(filename, "utf8");
|
|
676
812
|
if (needsTransform(filename, src)) {
|
|
677
|
-
return mod
|
|
813
|
+
return compileSupport(mod, transformRN(filename, src, projectRoot, platform), filename);
|
|
678
814
|
}
|
|
679
|
-
return
|
|
815
|
+
return loadSupport(mod, filename, origJs);
|
|
680
816
|
}
|
|
681
817
|
if (NODE_MODULES_PATH.test(norm)) {
|
|
682
818
|
// A node_modules package we did NOT transform: when Node's compile throws a
|
|
@@ -684,35 +820,22 @@ export function installRequireHooks(
|
|
|
684
820
|
// real fix (add the package to `transform: [...]`) instead of leaving a
|
|
685
821
|
// bare "Unexpected token '<'" — the single most common migration blocker.
|
|
686
822
|
try {
|
|
687
|
-
return
|
|
823
|
+
return loadSupport(mod, filename, origJs);
|
|
688
824
|
} catch (err) {
|
|
689
825
|
throw explainUntransformedSyntaxError(err, filename) ?? err;
|
|
690
826
|
}
|
|
691
827
|
}
|
|
692
|
-
return
|
|
828
|
+
return loadSupport(mod, filename, origJs);
|
|
693
829
|
};
|
|
694
830
|
|
|
695
|
-
// An `.mjs` file
|
|
696
|
-
//
|
|
697
|
-
//
|
|
698
|
-
// require(esm) does not consult it, so the file, and React Native's Flow source beneath
|
|
699
|
-
// it, ran uncompiled. There the files the loader would compile (React Native's, and
|
|
700
|
-
// packages selected for compiling) are compiled here, as the loader compiles an `.mjs`
|
|
701
|
-
// it is handed (requireFacade in loader.mjs). In-thread hooks see require(esm) and the
|
|
702
|
-
// file's own imports, so Node keeps it there.
|
|
831
|
+
// An `.mjs` file Node's require(esm) loads: the in-thread loader compiles what it must
|
|
832
|
+
// (requireFacade in loader.mjs), and this handler only checks Expo's SDK version, as
|
|
833
|
+
// the `.js` handler above does for CommonJS.
|
|
703
834
|
const origMjs = Module._extensions[".mjs"];
|
|
704
835
|
if (origMjs) {
|
|
705
836
|
Module._extensions[".mjs"] = function (mod, filename) {
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
if (globalThis.__vitest_native_loader_mode !== "thread") {
|
|
709
|
-
return origMjs(mod, filename);
|
|
710
|
-
}
|
|
711
|
-
const rnPath = ownership.reactNativePathFor(filename);
|
|
712
|
-
if (rnPath === null && !isExtra(norm)) return origMjs(mod, filename);
|
|
713
|
-
const boundary = boundarySourceFor(rnPath ?? norm, platform, reactNativeVersion);
|
|
714
|
-
if (boundary != null) return mod._compile(boundary, filename);
|
|
715
|
-
return mod._compile(transformRN(filename, undefined, projectRoot, platform), filename);
|
|
837
|
+
checkLoadedExpoSdk(filename.replace(/\\/g, "/"), expoSdks);
|
|
838
|
+
return origMjs(mod, filename);
|
|
716
839
|
};
|
|
717
840
|
}
|
|
718
841
|
|
|
@@ -733,7 +856,7 @@ export function installRequireHooks(
|
|
|
733
856
|
if (boundary != null) return mod._compile(boundary, filename);
|
|
734
857
|
// No source: transformRN reads the file only when its compiled output is not
|
|
735
858
|
// already held for this file's size and mtime.
|
|
736
|
-
return mod
|
|
859
|
+
return compileSupport(mod, transformRN(filename, undefined, projectRoot, platform), filename);
|
|
737
860
|
};
|
|
738
861
|
}
|
|
739
862
|
return compiles;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// The Jest resolvers React Native libraries ship for their own test runs.
|
|
2
|
+
//
|
|
3
|
+
// react-native-worklets and react-native-reanimated publish `jest/resolver.js`, which a
|
|
4
|
+
// Jest suite names as its `resolver`: for their own files (worklets' every file, and a
|
|
5
|
+
// listed set of reanimated's), it resolves without the `.native` extensions, so the
|
|
6
|
+
// JS implementations load instead of the ones that call into native code. Without it
|
|
7
|
+
// their own mocks fail to load under Jest as well (reanimated's mock imports the real
|
|
8
|
+
// package, whose native initializer throws). The engine asks these resolvers which
|
|
9
|
+
// extensions a request is resolved with and resolves it itself, so the libraries keep
|
|
10
|
+
// deciding which of their files that applies to.
|
|
11
|
+
import { createRequire } from "node:module";
|
|
12
|
+
import path from "node:path";
|
|
13
|
+
import { owningPackageOfFile } from "./match.mjs";
|
|
14
|
+
|
|
15
|
+
// Reanimated's resolver applies worklets' for everything it does not handle itself.
|
|
16
|
+
const RESOLVERS = ["react-native-reanimated/jest/resolver", "react-native-worklets/jest/resolver"];
|
|
17
|
+
|
|
18
|
+
// The packages whose own files these resolvers decide for. The resolvers tell their
|
|
19
|
+
// files apart by substring (`options.basedir.includes('react-native-worklets')`), which
|
|
20
|
+
// also matches a package that merely sits beside them: pnpm names a package's store
|
|
21
|
+
// directory after its peers (`.pnpm/expo-modules-core@58_react-native-worklets@0.13/…`),
|
|
22
|
+
// and `react-native-worklets-core` is a different package. So they are consulted only
|
|
23
|
+
// for files these packages own, and see each file at its package-relative path.
|
|
24
|
+
const OWNERS = new Set(["react-native-reanimated", "react-native-worklets"]);
|
|
25
|
+
|
|
26
|
+
const loaded = new Map();
|
|
27
|
+
const CAPTURED = Symbol("captured");
|
|
28
|
+
|
|
29
|
+
/** Where `id` resolves from `req`, or null when it is not installed in this layout. */
|
|
30
|
+
function installedFile(req, id) {
|
|
31
|
+
try {
|
|
32
|
+
return req.resolve(id);
|
|
33
|
+
} catch {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The installed library resolver files, from `projectRoot`: what decides which files a
|
|
40
|
+
* library graph is built from, so the precompiled library registry is pinned to them.
|
|
41
|
+
*/
|
|
42
|
+
export function libraryResolverFiles(projectRoot) {
|
|
43
|
+
const req = createRequire(path.join(projectRoot, "package.json"));
|
|
44
|
+
return RESOLVERS.map((id) => installedFile(req, id)).filter((file) => file !== null);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The first installed library resolver, from `projectRoot`, or null. */
|
|
48
|
+
function resolverFor(projectRoot) {
|
|
49
|
+
if (loaded.has(projectRoot)) return loaded.get(projectRoot);
|
|
50
|
+
let found = null;
|
|
51
|
+
const req = createRequire(path.join(projectRoot, "package.json"));
|
|
52
|
+
for (const id of RESOLVERS) {
|
|
53
|
+
// By resolved path: by name, a preset shadowing the package would answer it. A
|
|
54
|
+
// resolver that is installed but fails to load is an error, not a fallback.
|
|
55
|
+
const file = installedFile(req, id);
|
|
56
|
+
if (file === null) continue;
|
|
57
|
+
found = req(file);
|
|
58
|
+
if (typeof found === "function") break;
|
|
59
|
+
found = null;
|
|
60
|
+
}
|
|
61
|
+
loaded.set(projectRoot, found);
|
|
62
|
+
return found;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The extensions a library's own Jest resolver resolves `request` from `fromFile`
|
|
67
|
+
* with, when it narrows `extensions`; else null, for the engine's own resolution.
|
|
68
|
+
*/
|
|
69
|
+
export function libraryExtensionsFor(projectRoot, request, fromFile, extensions) {
|
|
70
|
+
const owner = owningPackageOfFile(fromFile);
|
|
71
|
+
if (!owner || !OWNERS.has(owner)) return null;
|
|
72
|
+
const resolver = resolverFor(projectRoot);
|
|
73
|
+
if (!resolver) return null;
|
|
74
|
+
// The directory from the owning package on (`node_modules/<pkg>/…`), without the
|
|
75
|
+
// store path before it. Only the resolver's substring checks read it: the engine
|
|
76
|
+
// resolves the request itself, from the real directory.
|
|
77
|
+
const posixDir = path.dirname(fromFile).split(path.sep).join("/");
|
|
78
|
+
const basedir = posixDir.slice(posixDir.lastIndexOf("/node_modules/"));
|
|
79
|
+
let captured = null;
|
|
80
|
+
try {
|
|
81
|
+
const result = resolver(request, {
|
|
82
|
+
basedir,
|
|
83
|
+
extensions,
|
|
84
|
+
defaultResolver: (_request, options) => {
|
|
85
|
+
captured = options?.extensions ?? extensions;
|
|
86
|
+
return CAPTURED;
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
if (result !== CAPTURED || !captured) return null;
|
|
90
|
+
} catch {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
return captured.length === extensions.length ? null : captured;
|
|
94
|
+
}
|