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
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// The test runner both engines use (wired as `test.runner` unless the project sets its
|
|
2
|
+
// own): Vitest's own TestRunner, with one change. A setup or test file whose import
|
|
3
|
+
// fails because a module did not compile reports where (compile-error.mjs). The hot
|
|
4
|
+
// runtime's runner (runner.mjs) extends this one.
|
|
5
|
+
import * as vitest from "vitest";
|
|
6
|
+
import { withCompileLocation } from "./compile-error.mjs";
|
|
7
|
+
|
|
8
|
+
export default class NativeTestRunner extends vitest.TestRunner {
|
|
9
|
+
async importFile(filepath, source) {
|
|
10
|
+
try {
|
|
11
|
+
return await super.importFile(filepath, source);
|
|
12
|
+
} catch (error) {
|
|
13
|
+
throw withCompileLocation(error, (file) => this.vitestNativeSourceMap(file));
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** The sourcemap Vite's module runner holds for the module compiled from `file`. */
|
|
18
|
+
vitestNativeSourceMap(file) {
|
|
19
|
+
const modules = this.moduleRunner?.evaluatedModules;
|
|
20
|
+
for (const node of modules?.getModulesByFile?.(file) ?? []) {
|
|
21
|
+
const map = modules.getModuleSourceMapById?.(node.id)?.map;
|
|
22
|
+
if (map) return map;
|
|
23
|
+
}
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
// Flow-strips a React Native source file via the project's React Native Babel preset
|
|
2
2
|
// (babel-preset.mjs; the only transformer that lowers RN's `component` syntax). Used by
|
|
3
|
-
// BOTH the loader hook (import) and the require hook, which run
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
// layer shared across threads, workers, runs, and (because content-keyed entries
|
|
3
|
+
// BOTH the loader hook (import) and the require hook, which run on the same worker
|
|
4
|
+
// thread and share this module's in-memory `mem` cache. The disk cache — keyed by
|
|
5
|
+
// CONTENT hash + platform, in a directory versioned by preset + @babel/core versions —
|
|
6
|
+
// is the layer shared across workers, runs, and (because content-keyed entries
|
|
8
7
|
// survive fresh installs and mtime normalization) CI cache restores. The path is part
|
|
9
8
|
// of the key too — Babel output embeds the filename — so restores are valid wherever
|
|
10
9
|
// the checkout path is stable (CI runners use a fixed workspace path).
|
|
@@ -15,6 +14,7 @@
|
|
|
15
14
|
// resolving versions — pure waste when every file is served from disk. App source asks
|
|
16
15
|
// it for its cache key, since only Babel can say which project configuration applies
|
|
17
16
|
// (projectBabelOptions).
|
|
17
|
+
import { projectBabelCaller } from "./babel-caller.mjs";
|
|
18
18
|
import nodeModule, { createRequire } from "node:module";
|
|
19
19
|
import path from "node:path";
|
|
20
20
|
import { fileURLToPath } from "node:url";
|
|
@@ -24,6 +24,7 @@ import crypto from "node:crypto";
|
|
|
24
24
|
import vm from "node:vm";
|
|
25
25
|
import { MessageChannel, receiveMessageOnPort, SHARE_ENV, Worker } from "node:worker_threads";
|
|
26
26
|
import { decorateTransformError } from "./explain.mjs";
|
|
27
|
+
import { relativeToRoot, rootSpellingOf } from "./root-spelling.mjs";
|
|
27
28
|
import {
|
|
28
29
|
handleToolchainRequest,
|
|
29
30
|
IMPORT_META_LOWERING,
|
|
@@ -54,6 +55,7 @@ function canonicalRoot(projectRoot) {
|
|
|
54
55
|
if (hit) return hit;
|
|
55
56
|
let root;
|
|
56
57
|
try {
|
|
58
|
+
// Node's spelling (fs.realpathSync): the transform cache's key, so it must stay stable.
|
|
57
59
|
root = fs.realpathSync(projectRoot);
|
|
58
60
|
} catch {
|
|
59
61
|
root = path.resolve(projectRoot);
|
|
@@ -217,22 +219,9 @@ function ctxFor(projectRoot) {
|
|
|
217
219
|
// as does app source in a project with no Babel config of its own.
|
|
218
220
|
const NODE_MODULES = /[\\/]node_modules[\\/]/;
|
|
219
221
|
|
|
220
|
-
/** The caller
|
|
222
|
+
/** The caller for Node's require of project source: CommonJS out (babel-caller.mjs). */
|
|
221
223
|
function babelJestCaller(ctx, platform) {
|
|
222
|
-
|
|
223
|
-
// Jest's defaults for a CommonJS module without --experimental-vm-modules
|
|
224
|
-
// (jest-runtime 29.7, build/index.js:173-179).
|
|
225
|
-
supportsDynamicImport: false,
|
|
226
|
-
supportsExportNamespaceFrom: false,
|
|
227
|
-
supportsStaticESM: false,
|
|
228
|
-
supportsTopLevelAwait: false,
|
|
229
|
-
};
|
|
230
|
-
// jest-expo's platform presets configure babel-jest with a Metro caller carrying the
|
|
231
|
-
// platform (jest-expo 57, config/getPlatformPreset.js:48-66), which babel-preset-expo
|
|
232
|
-
// reads to inline Platform.OS and process.env.EXPO_OS (build/common.js, getPlatform).
|
|
233
|
-
// The engine runs one platform, so a project with Expo gets that preset's caller.
|
|
234
|
-
if (ctx.hasExpo) return { name: "metro", bundler: "metro", platform, ...flags };
|
|
235
|
-
return { name: "babel-jest", ...flags };
|
|
224
|
+
return projectBabelCaller({ hasExpo: ctx.hasExpo, platform, esm: false });
|
|
236
225
|
}
|
|
237
226
|
|
|
238
227
|
/**
|
|
@@ -242,12 +231,17 @@ function babelJestCaller(ctx, platform) {
|
|
|
242
231
|
* (p95 0.3 ms), after a first call that loads the config's presets and plugins.
|
|
243
232
|
*/
|
|
244
233
|
function projectBabelOptions(ctx, file, platform) {
|
|
245
|
-
if (
|
|
234
|
+
if (NODE_MODULES.test(file)) return null;
|
|
235
|
+
// In whichever spelling of the root the file is under (root-spelling.mjs), which is
|
|
236
|
+
// also the one Babel gets as its root, so it finds the .babelrc files above the file.
|
|
237
|
+
const babelRoot = rootSpellingOf(ctx.root, file);
|
|
238
|
+
if (babelRoot === null || babelRoot === file) return null;
|
|
246
239
|
let partial;
|
|
247
240
|
try {
|
|
248
241
|
partial = callToolchain({
|
|
249
242
|
kind: "config",
|
|
250
243
|
root: ctx.root,
|
|
244
|
+
babelRoot,
|
|
251
245
|
file,
|
|
252
246
|
caller: babelJestCaller(ctx, platform),
|
|
253
247
|
});
|
|
@@ -260,7 +254,7 @@ function projectBabelOptions(ctx, file, platform) {
|
|
|
260
254
|
// compiles that way.
|
|
261
255
|
throw new VitestNativeError(
|
|
262
256
|
"BABEL_IGNORES_FILE",
|
|
263
|
-
`Babel ignores ${
|
|
257
|
+
`Babel ignores ${relativeToRoot(ctx.root, file).split(path.sep).join("/")}: the ` +
|
|
264
258
|
`project's Babel config excludes it through \`ignore\` or \`only\`, so it cannot ` +
|
|
265
259
|
`be compiled for Node's require(). babel-jest fails the same way under Jest. ` +
|
|
266
260
|
`Remove it from the config's \`ignore\`, or add it to \`only\`.`,
|
|
@@ -268,6 +262,7 @@ function projectBabelOptions(ctx, file, platform) {
|
|
|
268
262
|
}
|
|
269
263
|
if (partial.kind === "none") return null;
|
|
270
264
|
return {
|
|
265
|
+
babelRoot,
|
|
271
266
|
// babel-jest keys its cache on the loaded options (getCacheKeyFromConfig,
|
|
272
267
|
// build/index.js:135-165): a function config's output can depend on the
|
|
273
268
|
// environment or the caller, which no file's bytes show. The config files' own
|
|
@@ -948,6 +943,7 @@ export function transformRN(file, src, projectRoot, platform = "ios") {
|
|
|
948
943
|
? {
|
|
949
944
|
kind: "transform",
|
|
950
945
|
root: ctx.root,
|
|
946
|
+
babelRoot: projectConfig.babelRoot,
|
|
951
947
|
file,
|
|
952
948
|
src,
|
|
953
949
|
project: true,
|
package/dist/native/worker.mjs
CHANGED
|
@@ -23,8 +23,9 @@ import { captureModuleBaseline, resetEvaluatedModules } from "./module-reset.mjs
|
|
|
23
23
|
import { registerRntlHooksOnImport } from "./rntl-hooks.mjs";
|
|
24
24
|
import { residentProblems } from "./resident-health.mjs";
|
|
25
25
|
import { enableV8CompileCache } from "./compile-cache.mjs";
|
|
26
|
+
import { installOwnershipLedger } from "./ownership-ledger.mjs";
|
|
26
27
|
import { VitestNativeError } from "../errors.mjs";
|
|
27
|
-
import { markBootContext, requireProjectContext } from "../context.mjs";
|
|
28
|
+
import { activeRegistry, markBootContext, requireProjectContext } from "../context.mjs";
|
|
28
29
|
|
|
29
30
|
if (isMainThread || !parentPort) {
|
|
30
31
|
throw new VitestNativeError("HOT_WORKER_ENV", "hot worker entry must run in node:worker_threads");
|
|
@@ -59,6 +60,9 @@ if (diagnostics) {
|
|
|
59
60
|
const hotEsmGeneration =
|
|
60
61
|
context.hotEsmGeneration !== false && process.env.VITEST_NATIVE_HOT_ESM_GEN !== "0";
|
|
61
62
|
enableV8CompileCache(projectRoot, { skip: hotEsmGeneration });
|
|
63
|
+
// First, so the resident graph is in the ledger: a later Vite evaluation of a file this
|
|
64
|
+
// worker keeps resident is the dual evaluation that outlives every per-file reset.
|
|
65
|
+
installOwnershipLedger({ projectRoot });
|
|
62
66
|
installGlobals({ projectRoot, platform });
|
|
63
67
|
installKnownNativeModules(projectRoot);
|
|
64
68
|
// The registry must be installed BEFORE the preload below, not just by the setup
|
|
@@ -66,8 +70,9 @@ installKnownNativeModules(projectRoot);
|
|
|
66
70
|
// worker keeps resident, and a preload that bypassed the registry would leave the
|
|
67
71
|
// worker holding a second, separate copy of RN's singletons from the one every
|
|
68
72
|
// test file sees.
|
|
69
|
-
|
|
70
|
-
|
|
73
|
+
const registryFile = activeRegistry(context);
|
|
74
|
+
if (registryFile) {
|
|
75
|
+
installRegistry(registryFile, projectRoot, sourceExts, context.libraryRegistry);
|
|
71
76
|
}
|
|
72
77
|
installRequireHooks(
|
|
73
78
|
projectRoot,
|
|
@@ -185,7 +190,7 @@ let moduleRunner = null;
|
|
|
185
190
|
* resolved on the next import. `mocker.reset()` clears the registries but not that
|
|
186
191
|
* queue, so a `vi.doMock` issued after a file's last import was resolved during the
|
|
187
192
|
* NEXT file's first import and mocked it there. Per-file isolation never sees this —
|
|
188
|
-
* the worker, and its queue, go away. The queue is internal (Vitest
|
|
193
|
+
* the worker, and its queue, go away. The queue is internal (Vitest 5 keeps
|
|
189
194
|
* it as `static pendingIds` on BareModuleMocker); if it moves, this is a no-op and
|
|
190
195
|
* tests-native/hot-jest-compat fails by name on the runtime-mock surface.
|
|
191
196
|
*/
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
// One front door for Node's CommonJS loader in a test worker (ADR-0007).
|
|
2
|
+
//
|
|
3
|
+
// The engine intercepts `Module._load` and `Module._resolveFilename` for several
|
|
4
|
+
// reasons: per-file mocks (jest-compat), presets that shadow a package, the
|
|
5
|
+
// precompiled React Native registry, the hooks' Metro-style resolution, the
|
|
6
|
+
// export-condition recovery and the hot runtime's resolution tracker. Each used to
|
|
7
|
+
// wrap Node's function in turn, so the order they were installed in was their
|
|
8
|
+
// precedence, and it was written down nowhere but in that order.
|
|
9
|
+
//
|
|
10
|
+
// Here each hook has one entry, written to `Module` once per worker, when its first
|
|
11
|
+
// step is added, and never replaced, and every interceptor is a named step with a fixed rank. A step receives
|
|
12
|
+
// `next`, the rest of the chain below it, where it used to call the function it had
|
|
13
|
+
// replaced. A request reaches the steps in rank order, outermost first, which is the
|
|
14
|
+
// precedence the stacked wrappers had (LOAD_STEPS, RESOLVE_STEPS). A step is built
|
|
15
|
+
// from its `next`, so a request costs one call per step, as a wrapper did.
|
|
16
|
+
//
|
|
17
|
+
// A loader patched after ours (module-alias, pirates, proxyquire) wraps the entry as
|
|
18
|
+
// it would any `Module._load`; the entry keeps working beneath it.
|
|
19
|
+
import Module from "node:module";
|
|
20
|
+
import { VitestNativeTypeError } from "./errors.mjs";
|
|
21
|
+
|
|
22
|
+
/** `Module._load` steps, outermost first: the precedence of a CommonJS require. */
|
|
23
|
+
export const LOAD_STEPS = Object.freeze({
|
|
24
|
+
/** A library's own test support: its `@jest/globals` (native/library-jest.mjs). */
|
|
25
|
+
librarySupport: 5,
|
|
26
|
+
/** jest-compat's shims for Jest-only packages, such as `@jest/globals` (jest-compat/setup.mjs). */
|
|
27
|
+
jestCompatShims: 7,
|
|
28
|
+
/** jest-compat's per-file `jest.mock` registry (jest-compat/node-registry.mjs). */
|
|
29
|
+
mocks: 10,
|
|
30
|
+
/** A preset's module shadowing a package (native/hooks.mjs). */
|
|
31
|
+
preset: 20,
|
|
32
|
+
/** The precompiled React Native and library registries (native/registry.mjs). */
|
|
33
|
+
registry: 30,
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
/** `Module._resolveFilename` steps, outermost first. */
|
|
37
|
+
export const RESOLVE_STEPS = Object.freeze({
|
|
38
|
+
/** The hot runtime's record of each resolution, for its per-file reset (module-reset.mjs). */
|
|
39
|
+
tracker: 10,
|
|
40
|
+
/** Metro platform resolution, aliases and the resolution memo (native/hooks.mjs). */
|
|
41
|
+
hooks: 20,
|
|
42
|
+
/** A missing `react-native` export target, resolved without it (export-condition-recovery.mjs). */
|
|
43
|
+
recovery: 30,
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// Per worker, not per module instance: under the hot runtime this file can be
|
|
47
|
+
// evaluated more than once in one worker (by Node's loader and by Vite's module
|
|
48
|
+
// runner), and there must still be one dispatcher.
|
|
49
|
+
const DISPATCH = Symbol.for("vitest-native.node-dispatch");
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The chain of named steps for the `Module` function `key`. Its entry is written to
|
|
53
|
+
* `Module` when the first step is added, over the function found there (Node's own,
|
|
54
|
+
* unless something wrapped it earlier), so a hook no step uses is left untouched.
|
|
55
|
+
* `run` is rebuilt when the steps change, so a request costs one call per step.
|
|
56
|
+
*/
|
|
57
|
+
function createChain(key, ranks, install) {
|
|
58
|
+
const steps = [];
|
|
59
|
+
let tail = null;
|
|
60
|
+
const chain = {
|
|
61
|
+
run: null,
|
|
62
|
+
/** When set, answers every request in place of the steps (withResolver). */
|
|
63
|
+
override: null,
|
|
64
|
+
/**
|
|
65
|
+
* When set, wraps Node's own function beneath every step (the load chain's
|
|
66
|
+
* evaluation observer, onNodeEvaluation).
|
|
67
|
+
*/
|
|
68
|
+
wrapTail: null,
|
|
69
|
+
/**
|
|
70
|
+
* When set, the next request from `capture.parent` this hook answers records its
|
|
71
|
+
* file here, once (the resolve chain, for an observed load's own resolution).
|
|
72
|
+
*/
|
|
73
|
+
capture: null,
|
|
74
|
+
/** The function beneath every step: Node's own, as found when the entry was installed. */
|
|
75
|
+
get tail() {
|
|
76
|
+
return tail ?? Module[key];
|
|
77
|
+
},
|
|
78
|
+
/**
|
|
79
|
+
* Add the step named `name`, once: `make(next)` returns the function a request
|
|
80
|
+
* reaches, which calls `next(request, parent, ...rest)` to go on.
|
|
81
|
+
*/
|
|
82
|
+
add(name, make) {
|
|
83
|
+
if (!Object.hasOwn(ranks, name)) {
|
|
84
|
+
throw new VitestNativeTypeError(
|
|
85
|
+
"UNKNOWN_DISPATCH_STEP",
|
|
86
|
+
`unknown Node dispatch step: ${name}`,
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
if (steps.some((step) => step.name === name)) return false;
|
|
90
|
+
chain.install();
|
|
91
|
+
steps.push({ name, rank: ranks[name], make });
|
|
92
|
+
steps.sort((a, b) => a.rank - b.rank);
|
|
93
|
+
compose();
|
|
94
|
+
return true;
|
|
95
|
+
},
|
|
96
|
+
/** Write this hook's entry to `Module`, once. */
|
|
97
|
+
install() {
|
|
98
|
+
if (tail !== null) return;
|
|
99
|
+
tail = Module[key];
|
|
100
|
+
// Node calls these with three arguments (`_load`) or four (`_resolveFilename`);
|
|
101
|
+
// named parameters keep that call free of a rest array.
|
|
102
|
+
const entry = function (request, parent, isMain, options) {
|
|
103
|
+
const run = chain.override ?? chain.run;
|
|
104
|
+
const capture = chain.capture;
|
|
105
|
+
const captured = capture !== null && !capture.done && capture.parent === parent;
|
|
106
|
+
if (captured) capture.done = true;
|
|
107
|
+
const result =
|
|
108
|
+
arguments.length > 4
|
|
109
|
+
? run.apply(this, arguments)
|
|
110
|
+
: run.call(this, request, parent, isMain, options);
|
|
111
|
+
if (captured) {
|
|
112
|
+
capture.filename = result;
|
|
113
|
+
const held = typeof result === "string" ? Module._cache[result] : undefined;
|
|
114
|
+
capture.loadedBefore = held !== undefined && held.loaded === true;
|
|
115
|
+
}
|
|
116
|
+
return result;
|
|
117
|
+
};
|
|
118
|
+
compose();
|
|
119
|
+
install(entry);
|
|
120
|
+
},
|
|
121
|
+
/** Rebuild `run`, after `wrapTail` changes. */
|
|
122
|
+
recompose() {
|
|
123
|
+
if (tail !== null) compose();
|
|
124
|
+
},
|
|
125
|
+
/** Remove the step named `name`, if it is installed. */
|
|
126
|
+
remove(name) {
|
|
127
|
+
const at = steps.findIndex((step) => step.name === name);
|
|
128
|
+
if (at === -1) return false;
|
|
129
|
+
steps.splice(at, 1);
|
|
130
|
+
compose();
|
|
131
|
+
return true;
|
|
132
|
+
},
|
|
133
|
+
/** The names of the steps installed, outermost first. */
|
|
134
|
+
get names() {
|
|
135
|
+
return steps.map((step) => step.name);
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
function compose() {
|
|
139
|
+
// Node's own function is called as Node calls it, on Module.
|
|
140
|
+
let run = tail.bind(Module);
|
|
141
|
+
if (chain.wrapTail !== null) run = chain.wrapTail(run);
|
|
142
|
+
for (let i = steps.length - 1; i >= 0; i--) run = steps[i].make(run);
|
|
143
|
+
chain.run = run;
|
|
144
|
+
}
|
|
145
|
+
return chain;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function createDispatch() {
|
|
149
|
+
// The two writes to Module this file makes, one per hook.
|
|
150
|
+
const load = createChain("_load", LOAD_STEPS, (entry) => {
|
|
151
|
+
Module._load = entry;
|
|
152
|
+
});
|
|
153
|
+
const resolve = createChain("_resolveFilename", RESOLVE_STEPS, (entry) => {
|
|
154
|
+
Module._resolveFilename = entry;
|
|
155
|
+
});
|
|
156
|
+
const evaluations = new Set();
|
|
157
|
+
const loads = new Set();
|
|
158
|
+
// Beneath every load step, while something observes: Node's own load of a request.
|
|
159
|
+
// Node resolves it first, through the resolve chain, which reports the file to this
|
|
160
|
+
// load's capture (from the same parent, once) unless Node's relative-resolve cache
|
|
161
|
+
// already answers it. A module that was not loaded before and is after is one Node
|
|
162
|
+
// evaluated: new, or a placeholder Node's ESM loader cached for a file an `import`
|
|
163
|
+
// reached. One still loading (a circular require) is not reported.
|
|
164
|
+
const observe = (node) =>
|
|
165
|
+
function (request, parent, isMain, options) {
|
|
166
|
+
const capture = { parent, done: false, filename: null, loadedBefore: true };
|
|
167
|
+
resolve.capture = capture;
|
|
168
|
+
let exports;
|
|
169
|
+
try {
|
|
170
|
+
exports = node(request, parent, isMain, options);
|
|
171
|
+
} finally {
|
|
172
|
+
// Node resolves a load before it evaluates it, so a capture a nested load
|
|
173
|
+
// replaced is either taken or never will be: nothing is restored.
|
|
174
|
+
resolve.capture = null;
|
|
175
|
+
}
|
|
176
|
+
const { filename } = capture;
|
|
177
|
+
if (typeof filename !== "string") return exports;
|
|
178
|
+
if (loads.size > 0) {
|
|
179
|
+
report(loads, { request, parent: parent ?? null, filename });
|
|
180
|
+
}
|
|
181
|
+
if (!capture.loadedBefore && evaluations.size > 0) {
|
|
182
|
+
const module = Module._cache[filename];
|
|
183
|
+
if (module !== undefined && module.loaded === true) {
|
|
184
|
+
report(evaluations, {
|
|
185
|
+
filename,
|
|
186
|
+
module,
|
|
187
|
+
parent: typeof parent?.filename === "string" ? parent.filename : null,
|
|
188
|
+
via: filename.endsWith(".json")
|
|
189
|
+
? "json"
|
|
190
|
+
: Object.prototype.toString.call(module.exports) === "[object Module]"
|
|
191
|
+
? "require-esm"
|
|
192
|
+
: "cjs",
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return exports;
|
|
197
|
+
};
|
|
198
|
+
// Every listener hears it; the first to throw makes the require throw, after them all.
|
|
199
|
+
const report = (listeners, event) => {
|
|
200
|
+
let failure;
|
|
201
|
+
for (const listener of listeners) {
|
|
202
|
+
try {
|
|
203
|
+
listener(event);
|
|
204
|
+
} catch (error) {
|
|
205
|
+
failure ??= { error };
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
if (failure !== undefined) throw failure.error;
|
|
209
|
+
};
|
|
210
|
+
const observing = () => {
|
|
211
|
+
const on = evaluations.size > 0 || loads.size > 0;
|
|
212
|
+
if (on === (load.wrapTail !== null)) return;
|
|
213
|
+
load.wrapTail = on ? observe : null;
|
|
214
|
+
if (on) {
|
|
215
|
+
load.install();
|
|
216
|
+
resolve.install();
|
|
217
|
+
}
|
|
218
|
+
load.recompose();
|
|
219
|
+
};
|
|
220
|
+
const subscribe = (set, listener) => {
|
|
221
|
+
set.add(listener);
|
|
222
|
+
observing();
|
|
223
|
+
return () => {
|
|
224
|
+
set.delete(listener);
|
|
225
|
+
observing();
|
|
226
|
+
};
|
|
227
|
+
};
|
|
228
|
+
const channels = new Map();
|
|
229
|
+
return {
|
|
230
|
+
load,
|
|
231
|
+
resolve,
|
|
232
|
+
/**
|
|
233
|
+
* The object named `name`, one per worker, created empty on first use: a channel the
|
|
234
|
+
* engine's code reaches from wherever it runs (Vite's module runner, Node's loaders,
|
|
235
|
+
* the in-thread loader hooks) through this dispatcher, rather than through a global
|
|
236
|
+
* of its own. The ownership ledger's is "ownership".
|
|
237
|
+
*/
|
|
238
|
+
channel(name) {
|
|
239
|
+
let channel = channels.get(name);
|
|
240
|
+
if (channel === undefined) channels.set(name, (channel = {}));
|
|
241
|
+
return channel;
|
|
242
|
+
},
|
|
243
|
+
/**
|
|
244
|
+
* Call `listener({ filename, module, parent, via })` for each module Node's CommonJS
|
|
245
|
+
* loader evaluates through `Module._load`: `filename` is the key Node caches it by
|
|
246
|
+
* (its real path, unless --preserve-symlinks), `parent` the requiring module's
|
|
247
|
+
* filename or null, `via` "cjs", "require-esm" or "json". This includes a file an
|
|
248
|
+
* `import` reached, which Node's ESM loader caches unloaded and evaluates through
|
|
249
|
+
* `Module._load`. Not for what a step answers instead of Node (a mock, a preset, the
|
|
250
|
+
* registry), nor for a module already loaded, nor for a load Node resolves without
|
|
251
|
+
* `Module._resolveFilename` (a resolve hook that short-circuits it). A listener that throws makes
|
|
252
|
+
* the require throw, after every listener has heard it. Returns a function that stops
|
|
253
|
+
* it. Nothing is added to a require while nothing observes.
|
|
254
|
+
*/
|
|
255
|
+
onNodeEvaluation(listener) {
|
|
256
|
+
return subscribe(evaluations, listener);
|
|
257
|
+
},
|
|
258
|
+
/**
|
|
259
|
+
* Call `listener({ request, parent, filename })` for each request Node's own load
|
|
260
|
+
* resolves: `request` as `Module._load` received it and `parent` the requiring
|
|
261
|
+
* module, which is how Node's relative-resolve cache keys it (whatever a resolver
|
|
262
|
+
* wrapped around this dispatcher made of the request), and `filename` the file it
|
|
263
|
+
* resolved. For the hot runtime's cache drain (module-reset.mjs). Returns a function
|
|
264
|
+
* that stops it.
|
|
265
|
+
*/
|
|
266
|
+
onNodeLoad(listener) {
|
|
267
|
+
return subscribe(loads, listener);
|
|
268
|
+
},
|
|
269
|
+
/** Node's `Module._load` beneath every step. */
|
|
270
|
+
get nodeLoad() {
|
|
271
|
+
return load.tail;
|
|
272
|
+
},
|
|
273
|
+
/**
|
|
274
|
+
* Run `body` with every resolution answered by `override` instead of the steps:
|
|
275
|
+
* the hot runtime's cache drain, which must reach Node's relative-resolve cache
|
|
276
|
+
* without executing or resolving anything (module-reset.mjs). Needs a resolve step.
|
|
277
|
+
*/
|
|
278
|
+
withResolver(override, body) {
|
|
279
|
+
const saved = resolve.override;
|
|
280
|
+
resolve.override = override;
|
|
281
|
+
try {
|
|
282
|
+
return body();
|
|
283
|
+
} finally {
|
|
284
|
+
resolve.override = saved;
|
|
285
|
+
}
|
|
286
|
+
},
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** This worker's dispatcher, installed on first use. */
|
|
291
|
+
export function nodeDispatch() {
|
|
292
|
+
return (globalThis[DISPATCH] ??= createDispatch());
|
|
293
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
1
2
|
import path from "node:path";
|
|
2
3
|
import fs from "node:fs";
|
|
3
4
|
import { METRO_ASSET_EXTS } from "./native/assets.mjs";
|
|
@@ -50,6 +51,10 @@ const SELF_TESTING_FROM_MAJOR = { "react-native-mmkv": 3 };
|
|
|
50
51
|
* code is the whole API, current with every SDK, where a replacement covers only what
|
|
51
52
|
* was written into it.
|
|
52
53
|
*
|
|
54
|
+
* - gestureHandler, flashList: the packages run as themselves, with the Jest setup file
|
|
55
|
+
* each ships (LIBRARY_SETUP_FILES) replacing their native layer, as under Jest. The
|
|
56
|
+
* hand-written presets replaced the whole package and lacked most of its API
|
|
57
|
+
* (gesture-handler 3's hook API, its builder methods; tests/preset-library-mocks.test.ts).
|
|
53
58
|
* - expo: expo-constants, expo-font, expo-asset, expo-splash-screen, expo-linking and
|
|
54
59
|
* expo-status-bar run against the engine's Expo runtime (native/globals.mjs) and
|
|
55
60
|
* their own native-module mocks (native/expo-module-mocks.mjs). Measured on an
|
|
@@ -61,7 +66,113 @@ const SELF_TESTING_FROM_MAJOR = { "react-native-mmkv": 3 };
|
|
|
61
66
|
* The mock engine has no real React Native to run them on, so it keeps these presets.
|
|
62
67
|
* `presets: { expo: true }` applies one under the native engine too.
|
|
63
68
|
*/
|
|
64
|
-
const NATIVE_ENGINE_RUNS_PACKAGES = [
|
|
69
|
+
const NATIVE_ENGINE_RUNS_PACKAGES = [
|
|
70
|
+
"expo",
|
|
71
|
+
"gestureHandler",
|
|
72
|
+
"flashList"
|
|
73
|
+
];
|
|
74
|
+
/**
|
|
75
|
+
* The Jest setup files libraries ship for their real JavaScript, which the native engine
|
|
76
|
+
* runs before the project's own setup files for each installed package whose preset
|
|
77
|
+
* steps aside (NATIVE_ENGINE_RUNS_PACKAGES), as a Jest suite lists them in `setupFiles`.
|
|
78
|
+
* Each replaces only the library's native layer: gesture-handler's its native module
|
|
79
|
+
* and host components, flash-list's its layout measurement. They run with Jest's
|
|
80
|
+
* module-registry semantics (native/library-jest.mjs).
|
|
81
|
+
*/
|
|
82
|
+
const LIBRARY_SETUP_FILES = {
|
|
83
|
+
gestureHandler: "react-native-gesture-handler/jestSetup.js",
|
|
84
|
+
flashList: "@shopify/flash-list/jestSetup.js"
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* Whether `pkg`, resolved from `projectRoot`, ships the Jest resolver its JS
|
|
88
|
+
* implementation loads through in a test runner (native/jest-resolvers.mjs): from
|
|
89
|
+
* react-native-worklets 0.9 and react-native-reanimated 4.7. Without it the package's
|
|
90
|
+
* `.native` initializers load and throw ("Native part of Worklets doesn't seem to be
|
|
91
|
+
* initialized"), so its preset keeps applying.
|
|
92
|
+
*/
|
|
93
|
+
function shipsJestResolver(projectRoot, pkg) {
|
|
94
|
+
for (let dir = path.resolve(projectRoot);; dir = path.dirname(dir)) {
|
|
95
|
+
const manifest = path.join(dir, "node_modules", pkg, "package.json");
|
|
96
|
+
if (fs.existsSync(manifest)) return fs.existsSync(path.join(path.dirname(manifest), "jest", "resolver.js"));
|
|
97
|
+
if (path.dirname(dir) === dir) return false;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/** The installed reanimated's directory, from `root`, or null. */
|
|
101
|
+
function reanimatedDir(root) {
|
|
102
|
+
try {
|
|
103
|
+
return path.dirname(createRequire(path.join(root, "package.json")).resolve("react-native-reanimated/package.json"));
|
|
104
|
+
} catch {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
/** Marks a preset module that native/setup.mjs builds as each file is set up. */
|
|
109
|
+
const BUILT_AT_SETUP = Symbol.for("vitest-native.preset-built-at-setup");
|
|
110
|
+
/** react-native-worklets' own mock, which reanimated's own mock runs over. */
|
|
111
|
+
const WORKLETS_MOCK = {
|
|
112
|
+
pkg: "react-native-worklets",
|
|
113
|
+
file: "lib/module/mock.js",
|
|
114
|
+
setsGlobals: true
|
|
115
|
+
};
|
|
116
|
+
/** The installed package's directory, from `root`, when it ships `mock`; else null. */
|
|
117
|
+
function libraryMockDir(mock, root) {
|
|
118
|
+
let byRoot = mockDirs.get(mock);
|
|
119
|
+
if (byRoot === void 0) mockDirs.set(mock, byRoot = /* @__PURE__ */ new Map());
|
|
120
|
+
const key = root;
|
|
121
|
+
let dir = byRoot.get(key);
|
|
122
|
+
if (dir === void 0) {
|
|
123
|
+
dir = null;
|
|
124
|
+
for (let at = path.resolve(root);; at = path.dirname(at)) {
|
|
125
|
+
const candidate = path.join(at, "node_modules", mock.pkg);
|
|
126
|
+
if (fs.existsSync(path.join(candidate, "package.json"))) {
|
|
127
|
+
const real = fs.realpathSync(candidate);
|
|
128
|
+
if ((mock.available ?? ((d) => fs.existsSync(path.join(d, mock.file)) && entryResolves(mock, root)))(real, root)) dir = real;
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
if (path.dirname(at) === at) break;
|
|
132
|
+
}
|
|
133
|
+
byRoot.set(key, dir);
|
|
134
|
+
}
|
|
135
|
+
return dir;
|
|
136
|
+
}
|
|
137
|
+
const mockDirs = /* @__PURE__ */ new WeakMap();
|
|
138
|
+
/** Whether `require` of the mock's entry from `root` resolves (an `exports` map can refuse it). */
|
|
139
|
+
function entryResolves(mock, root) {
|
|
140
|
+
try {
|
|
141
|
+
createRequire(path.join(root, "package.json")).resolve(`${mock.pkg}/${mock.entry ?? mock.file}`);
|
|
142
|
+
return true;
|
|
143
|
+
} catch {
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Whether reanimated's own mock can run for `projectRoot`: it loads the real package,
|
|
149
|
+
* whose JS implementation loads only through reanimated's and worklets' Jest resolvers
|
|
150
|
+
* (reanimated 4.7, worklets 0.9), over worklets' own mock. Before those, the real
|
|
151
|
+
* package's `.native` initializers throw, and the module below serves instead, as under
|
|
152
|
+
* the mock engine.
|
|
153
|
+
*/
|
|
154
|
+
function libraryMockUsable(projectRoot) {
|
|
155
|
+
let usable = mockUsable.get(projectRoot);
|
|
156
|
+
if (usable === void 0) {
|
|
157
|
+
const dir = reanimatedDir(projectRoot);
|
|
158
|
+
usable = dir !== null && shipsJestResolver(projectRoot, "react-native-reanimated") && shipsJestResolver(dir, "react-native-worklets") && libraryMockDir(WORKLETS_MOCK, dir) !== null;
|
|
159
|
+
mockUsable.set(projectRoot, usable);
|
|
160
|
+
}
|
|
161
|
+
return usable;
|
|
162
|
+
}
|
|
163
|
+
const mockUsable = /* @__PURE__ */ new Map();
|
|
164
|
+
/**
|
|
165
|
+
* The entries of the library module graphs the native engine precompiles into the
|
|
166
|
+
* library registry (native/registry.mjs, buildLibraryRegistry), for the presets active
|
|
167
|
+
* under it: each library's own mock that a preset's module is. Without them those
|
|
168
|
+
* packages load file by file.
|
|
169
|
+
*/
|
|
170
|
+
function libraryRegistryEntries(projectRoot, activePresets) {
|
|
171
|
+
const entries = [];
|
|
172
|
+
if (activePresets.includes("reanimated") && libraryMockUsable(projectRoot)) entries.push("react-native-reanimated/mock");
|
|
173
|
+
if (activePresets.includes("worklets") && libraryMockDir(WORKLETS_MOCK, projectRoot)) entries.push(`${WORKLETS_MOCK.pkg}/${WORKLETS_MOCK.file}`);
|
|
174
|
+
return entries;
|
|
175
|
+
}
|
|
65
176
|
/**
|
|
66
177
|
* Whether an auto-detected preset applies under `engine`, given the `presets` option:
|
|
67
178
|
* every one under the mock engine, and under the native engine all but those in
|
|
@@ -179,4 +290,4 @@ const DEFAULT_ASSET_EXTS = [
|
|
|
179
290
|
"woff",
|
|
180
291
|
"woff2"
|
|
181
292
|
];
|
|
182
|
-
export {
|
|
293
|
+
export { PRESET_MODULES as a, libraryMockDir as c, presetForInstalled as d, presetShadowing as f, LIBRARY_SETUP_FILES as i, libraryMockUsable as l, testsItself as m, BUILT_AT_SETUP as n, WORKLETS_MOCK as o, reanimatedDir as p, DEFAULT_ASSET_EXTS as r, autoDetectedPresetApplies as s, AUTO_DETECT_PRESETS as t, libraryRegistryEntries as u };
|