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
|
@@ -26,6 +26,7 @@ import path from "node:path";
|
|
|
26
26
|
function cachedModule(file) {
|
|
27
27
|
if (Module._cache[file]) return Module._cache[file];
|
|
28
28
|
try {
|
|
29
|
+
// Node's spelling (fs.realpathSync): the key Module._cache holds the file under.
|
|
29
30
|
return Module._cache[fs.realpathSync(file)] ?? null;
|
|
30
31
|
} catch {
|
|
31
32
|
return null;
|
|
@@ -19,6 +19,13 @@ export declare function extensionsFor(
|
|
|
19
19
|
* First existing platform variant of an extensionless absolute base path, or its
|
|
20
20
|
* directory index, or null when nothing matches.
|
|
21
21
|
*/
|
|
22
|
+
/** The platform variant of a package's extensionless subpath, or null (#573). */
|
|
23
|
+
export declare function platformSubpathFile(
|
|
24
|
+
request: string,
|
|
25
|
+
resolved: string,
|
|
26
|
+
platform: string,
|
|
27
|
+
sourceExts?: readonly string[],
|
|
28
|
+
): string | null;
|
|
22
29
|
export declare function resolvePlatformFile(
|
|
23
30
|
absBase: string,
|
|
24
31
|
platform?: "ios" | "android",
|
package/dist/native/resolve.mjs
CHANGED
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
// then .native, then generic TS/JS variants, and fall back to a directory index.
|
|
3
3
|
// Shared by the require hook and the loader.
|
|
4
4
|
import fs from "node:fs";
|
|
5
|
+
import Module from "node:module";
|
|
5
6
|
import path from "node:path";
|
|
7
|
+
import { packageNameOf } from "./match.mjs";
|
|
6
8
|
|
|
7
9
|
/**
|
|
8
10
|
* Metro's default `sourceExts`, in its order. Kept as data rather than spelled out
|
|
@@ -180,6 +182,34 @@ export function resolvePlatformFile(absBase, platform = "ios", sourceExts = METR
|
|
|
180
182
|
return resolved;
|
|
181
183
|
}
|
|
182
184
|
|
|
185
|
+
/**
|
|
186
|
+
* The platform variant of a package's extensionless subpath, given the file a resolver
|
|
187
|
+
* found for it, or null to keep that file. `react-native/Libraries/Utilities/BackHandler`
|
|
188
|
+
* resolves through React Native's `exports` map ("./Libraries/*": "./Libraries/*.js")
|
|
189
|
+
* to BackHandler.js, a shim that re-exports `./BackHandler` so deep imports keep their
|
|
190
|
+
* platform file. React Native's own index requires BackHandler.ios.js, and Jest's React
|
|
191
|
+
* Native preset resolves the subpath to it, so `jest.mock`/`vi.mock` of the subpath
|
|
192
|
+
* replaces the module `react-native` exports (#573). Only when the file found is the
|
|
193
|
+
* one the subpath names (`<sub>.<ext>` or `<sub>/index.<ext>` in the package), so an
|
|
194
|
+
* `exports` target elsewhere is kept. The result is spelled as `resolved` is: with
|
|
195
|
+
* backslashes when it has them, else with forward slashes.
|
|
196
|
+
*/
|
|
197
|
+
export function platformSubpathFile(request, resolved, platform, sourceExts) {
|
|
198
|
+
if (request.startsWith(".") || path.isAbsolute(request) || path.extname(request)) return null;
|
|
199
|
+
const name = packageNameOf(request);
|
|
200
|
+
if (request.length <= name.length + 1) return null;
|
|
201
|
+
const file = resolved.replace(/\\/g, "/");
|
|
202
|
+
const stem = file.slice(0, file.length - path.extname(file).length);
|
|
203
|
+
const tail = `/${request}`;
|
|
204
|
+
if (!stem.endsWith(tail) && !stem.endsWith(`${tail}/index`)) return null;
|
|
205
|
+
const hit = resolvePlatformFile(stem, platform, sourceExts);
|
|
206
|
+
if (hit === null || hit.replace(/\\/g, "/") === file) return null;
|
|
207
|
+
// In the spelling it was given: Node's (`C:\…`) for jest-compat, which keys on Node's
|
|
208
|
+
// real path, and Vite's (`C:/…`) for resolveId, whose ids use forward slashes.
|
|
209
|
+
const variant = hit.replace(/\\/g, "/");
|
|
210
|
+
return resolved.includes("\\") ? variant.replaceAll("/", "\\") : variant;
|
|
211
|
+
}
|
|
212
|
+
|
|
183
213
|
/**
|
|
184
214
|
* `resolvePlatformFile` for a relative, extensionless request as written in `fromFile`.
|
|
185
215
|
* `path.resolve` drops a trailing separator, and with it the request's meaning: Node
|
|
@@ -232,7 +262,32 @@ function isFile(file) {
|
|
|
232
262
|
}
|
|
233
263
|
|
|
234
264
|
function scanPlatformFile(absBase, platform, sourceExts) {
|
|
235
|
-
|
|
265
|
+
return scanWithExtensions(absBase, extensionsFor(platform, sourceExts));
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* `resolveRelativePlatformFile` with an explicit extension list: a library's own Jest
|
|
270
|
+
* resolver can narrow the list for its files (jest-resolvers.mjs).
|
|
271
|
+
*/
|
|
272
|
+
export function resolveRelativeWithExtensions(request, fromFile, extensions) {
|
|
273
|
+
const base = path.resolve(path.dirname(fromFile), request);
|
|
274
|
+
const key =
|
|
275
|
+
"\0ext\0" +
|
|
276
|
+
extensions.join("\0") +
|
|
277
|
+
"\0" +
|
|
278
|
+
base +
|
|
279
|
+
(TRAILING_SEPARATOR.test(request) ? path.sep : "");
|
|
280
|
+
const cached = resolveCache.get(key);
|
|
281
|
+
if (cached !== undefined) return cached;
|
|
282
|
+
const resolved = scanWithExtensions(
|
|
283
|
+
TRAILING_SEPARATOR.test(request) ? base + path.sep : base,
|
|
284
|
+
extensions,
|
|
285
|
+
);
|
|
286
|
+
resolveCache.set(key, resolved);
|
|
287
|
+
return resolved;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
function scanWithExtensions(absBase, extensions) {
|
|
236
291
|
// metro-resolver resolveModulePath (src/resolve.js:316-350): a path ending in a
|
|
237
292
|
// separator skips the file lookup and goes straight to the directory.
|
|
238
293
|
const dirOnly = TRAILING_SEPARATOR.test(absBase);
|
|
@@ -294,15 +349,75 @@ export function resolveDeepPackageFile(request, fromDir, platform, sourceExts =
|
|
|
294
349
|
const pkgDir = findPackageDir(fromDir, pkg);
|
|
295
350
|
if (!pkgDir) return null;
|
|
296
351
|
const base = path.join(pkgDir, rest);
|
|
297
|
-
|
|
298
|
-
if (fs.statSync(base).isFile()) return base;
|
|
299
|
-
} catch {}
|
|
352
|
+
if (fs.statSync(base, { throwIfNoEntry: false })?.isFile()) return base;
|
|
300
353
|
// `Foo.js` may exist only as a platform variant, and extensionless specifiers
|
|
301
354
|
// need the full Metro candidate list either way.
|
|
302
355
|
const stem = /\.[a-z0-9]+$/i.test(base) ? base.replace(/\.js$/, "") : base;
|
|
303
356
|
return resolvePlatformFile(stem, platform, sourceExts);
|
|
304
357
|
}
|
|
305
358
|
|
|
359
|
+
/**
|
|
360
|
+
* Whether `request` names a whole installed package whose `react-native` field the
|
|
361
|
+
* engine follows (resolvePackageMainField): not a path, a subpath, a builtin, a subpath
|
|
362
|
+
* import (`#x`), or React Native itself, which its own registry serves.
|
|
363
|
+
*/
|
|
364
|
+
export function isWholePackageRequest(request) {
|
|
365
|
+
return (
|
|
366
|
+
!request.startsWith(".") &&
|
|
367
|
+
!request.startsWith("#") &&
|
|
368
|
+
!path.isAbsolute(request) &&
|
|
369
|
+
packageNameOf(request) === request &&
|
|
370
|
+
request !== "react-native" &&
|
|
371
|
+
!request.startsWith("@react-native/") &&
|
|
372
|
+
!Module.isBuiltin(request)
|
|
373
|
+
);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* The entry a bare `request` for a whole package resolves to through its `react-native`
|
|
378
|
+
* field, as Metro resolves it and as Vite does for an `import` under the native engine
|
|
379
|
+
* (native/apply.ts `resolve.mainFields`), or null. Node's `require` reads `main`, so a
|
|
380
|
+
* package naming a different file in each was loaded twice, from two files: the test's
|
|
381
|
+
* import got one instance and a library's require another (react-native-worklets:
|
|
382
|
+
* `src/index.ts` and `lib/module/index.js`). Only for a
|
|
383
|
+
* package without an `exports` map, which Node and Vite already read alike through the
|
|
384
|
+
* `react-native` condition.
|
|
385
|
+
*/
|
|
386
|
+
export function resolvePackageMainField(
|
|
387
|
+
request,
|
|
388
|
+
fromDir,
|
|
389
|
+
platform,
|
|
390
|
+
sourceExts = METRO_SOURCE_EXTS,
|
|
391
|
+
) {
|
|
392
|
+
// Every bare require reaches here, so both lookups are cached: which directory holds
|
|
393
|
+
// the package from `fromDir`, and what that package.json says. Neither changes within
|
|
394
|
+
// a worker's life.
|
|
395
|
+
const dirKey = `${fromDir}\0${request}`;
|
|
396
|
+
let pkgDir = packageDirCache.get(dirKey);
|
|
397
|
+
if (pkgDir === undefined) {
|
|
398
|
+
pkgDir = findPackageDir(fromDir, request);
|
|
399
|
+
packageDirCache.set(dirKey, pkgDir);
|
|
400
|
+
}
|
|
401
|
+
if (!pkgDir) return null;
|
|
402
|
+
let manifest = manifestCache.get(pkgDir);
|
|
403
|
+
if (manifest === undefined) {
|
|
404
|
+
try {
|
|
405
|
+
manifest = JSON.parse(fs.readFileSync(path.join(pkgDir, "package.json"), "utf8"));
|
|
406
|
+
} catch {
|
|
407
|
+
manifest = null;
|
|
408
|
+
}
|
|
409
|
+
manifestCache.set(pkgDir, manifest);
|
|
410
|
+
}
|
|
411
|
+
if (!manifest) return null;
|
|
412
|
+
if (manifest.exports !== undefined || typeof manifest["react-native"] !== "string") return null;
|
|
413
|
+
const target = path.join(pkgDir, manifest["react-native"]);
|
|
414
|
+
if (fs.statSync(target, { throwIfNoEntry: false })?.isFile()) return target;
|
|
415
|
+
return resolvePlatformFile(target.replace(/\.[cm]?[jt]sx?$/, ""), platform, sourceExts);
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
const packageDirCache = new Map();
|
|
419
|
+
const manifestCache = new Map();
|
|
420
|
+
|
|
306
421
|
function findPackageDir(startDir, pkg) {
|
|
307
422
|
let dir = path.resolve(startDir);
|
|
308
423
|
for (;;) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// The RNTL adapter: what React Native Testing Library needs from its environment
|
|
2
|
-
// and does not get under Vitest, for each supported major (
|
|
2
|
+
// and does not get under Vitest, for each supported major (13, 14). Two parts:
|
|
3
3
|
// 1. per-file hooks under the hot runtime (registerRntlHooksOnImport);
|
|
4
4
|
// 2. a `jest` for RNTL's fake-timer support (installRntlJest).
|
|
5
5
|
//
|
|
@@ -33,13 +33,13 @@
|
|
|
33
33
|
// second time in that one file — harmless, as cleanup is idempotent and the act
|
|
34
34
|
// environment pair restores in order.
|
|
35
35
|
//
|
|
36
|
-
// runExternalModule is Vitest's module-evaluator method in Vitest
|
|
36
|
+
// runExternalModule is Vitest's module-evaluator method in Vitest 5; if it goes
|
|
37
37
|
// away, nothing is wrapped and tests-native/hot-jest-compat fails on the RNTL surface.
|
|
38
38
|
import fs from "node:fs";
|
|
39
39
|
import Module from "node:module";
|
|
40
40
|
import { fileURLToPath } from "node:url";
|
|
41
41
|
import vm from "node:vm";
|
|
42
|
-
import { noteRntlEntryImported, RNTL_ENTRY } from "../rntl-support.mjs";
|
|
42
|
+
import { noteRntlEntryImported, RNTL_DONT_CLEANUP, RNTL_ENTRY } from "../rntl-support.mjs";
|
|
43
43
|
|
|
44
44
|
const WRAPPED = Symbol.for("vitest-native.rntl-hooks");
|
|
45
45
|
|
|
@@ -50,13 +50,17 @@ function entryFile(id) {
|
|
|
50
50
|
} catch {
|
|
51
51
|
return null;
|
|
52
52
|
}
|
|
53
|
-
|
|
53
|
+
// RNTL's entry, or its dont-cleanup-after-each module: a resident copy of either is run
|
|
54
|
+
// again when a file imports it, as under per-file isolation, so the entry registers its
|
|
55
|
+
// hooks and the opt-out sets RNTL_SKIP_AUTO_CLEANUP for this file too (#527).
|
|
56
|
+
return RNTL_ENTRY.test(file) || RNTL_DONT_CLEANUP.test(file) ? file : null;
|
|
54
57
|
}
|
|
55
58
|
|
|
56
59
|
// The key Node's module cache holds `file` under, if it is cached.
|
|
57
60
|
function cachedEntry(file) {
|
|
58
61
|
if (Module._cache[file]) return file;
|
|
59
62
|
try {
|
|
63
|
+
// Node's spelling (fs.realpathSync): the key Module._cache holds the file under.
|
|
60
64
|
const real = fs.realpathSync(file);
|
|
61
65
|
return Module._cache[real] ? real : null;
|
|
62
66
|
} catch {
|
|
@@ -96,7 +100,7 @@ export function registerRntlHooksOnImport(evaluator) {
|
|
|
96
100
|
// --- 2. RNTL's fake-timer `jest` ---
|
|
97
101
|
//
|
|
98
102
|
// RNTL drives fake timers only through a free `jest` identifier. Every supported major
|
|
99
|
-
// (
|
|
103
|
+
// (13.3, 14.x) reads it in these three files, which its entry loads:
|
|
100
104
|
//
|
|
101
105
|
// helpers/timers.js `typeof jest`, then `setTimeout._isMockFunction` (legacy)
|
|
102
106
|
// or `setTimeout.clock` + `jest.getRealSystemTime()`
|
|
@@ -14,21 +14,11 @@
|
|
|
14
14
|
// it to decide where string children may live, so without RCTVirtualText a
|
|
15
15
|
// nested <Text> throws "Text strings must be rendered within a <Text>
|
|
16
16
|
// component". Adding the name to the list fixes both.
|
|
17
|
-
// - RNTL
|
|
18
|
-
//
|
|
19
|
-
//
|
|
20
|
-
//
|
|
21
|
-
//
|
|
22
|
-
// so replacing the export reaches all of them.
|
|
23
|
-
//
|
|
24
|
-
// Known gap, RNTL 12: role queries. Its isAccessibilityElement
|
|
25
|
-
// (build/helpers/accessibility.js) does not call isHostText; it compares the type
|
|
26
|
-
// with getHostComponentNames().text directly. So on RNTL 12 a nested
|
|
27
|
-
// `<Text accessibilityRole="link">` is found by getByText but not by
|
|
28
|
-
// getByRole("link"); RNTL 13 and 14 go through isHostText and find it. This is
|
|
29
|
-
// deliberately left unpatched: replacing RNTL's exports is a stopgap, and RNTL
|
|
30
|
-
// handling is to move into a per-major RNTL adapter rather than gain more patches
|
|
31
|
-
// here.
|
|
17
|
+
// - RNTL 13 (build/) exports only the predicate isHostText, which tests a private
|
|
18
|
+
// ['Text', 'RCTText']. Every caller of it in that build reads it through the
|
|
19
|
+
// module's exports object at call time, as Babel's CommonJS output does —
|
|
20
|
+
// `(0, _hostComponentNames.isHostText)(node)` — so replacing the export reaches
|
|
21
|
+
// all of them.
|
|
32
22
|
|
|
33
23
|
const VIRTUAL_TEXT = "RCTVirtualText";
|
|
34
24
|
const WRAPPED = Symbol.for("vitest-native.rntl-virtual-text-host");
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** The distinct spellings of `root`: as resolved, its JS real path, its native real path. */
|
|
2
|
+
export declare function rootSpellings(root: string): string[];
|
|
3
|
+
/** The spelling of `root` that `file` is under (or is), or null when it is outside it. */
|
|
4
|
+
export declare function rootSpellingOf(root: string, file: string): string | null;
|
|
5
|
+
/** `file` relative to `root`, in whichever spelling of the root it is under. */
|
|
6
|
+
export declare function relativeToRoot(root: string, file: string): string;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// Which spelling of the project root a file path is under.
|
|
2
|
+
//
|
|
3
|
+
// One directory has several spellings, and files arrive in more than one. Vite's ids come
|
|
4
|
+
// from the native real path, which expands a Windows 8.3 short name (RUNNER~1) and gives
|
|
5
|
+
// the on-disk letter case. A file Node reaches from a path built on the root as given
|
|
6
|
+
// (createRequire of the project's package.json, a test's temporary directory) keeps the
|
|
7
|
+
// root's spelling, and the JS realpath keeps it too. A check of "is this file under the
|
|
8
|
+
// root" against one spelling misses the files in the others: on Windows, app source that
|
|
9
|
+
// Node loaded skipped the project's Babel config. Babel's own root has to be spelled as
|
|
10
|
+
// the file is, too, or it does not find the .babelrc files above it.
|
|
11
|
+
import fs from "node:fs";
|
|
12
|
+
import path from "node:path";
|
|
13
|
+
|
|
14
|
+
// Windows paths compare without case (a drive letter is `c:` or `C:` as typed).
|
|
15
|
+
const fold = process.platform === "win32" ? (p) => p.toLowerCase() : (p) => p;
|
|
16
|
+
|
|
17
|
+
const spellings = new Map();
|
|
18
|
+
|
|
19
|
+
const natives = new Map();
|
|
20
|
+
|
|
21
|
+
/** The distinct spellings of `root`: as resolved, its JS real path, its native real path. */
|
|
22
|
+
export function rootSpellings(root) {
|
|
23
|
+
let found = spellings.get(root);
|
|
24
|
+
if (found === undefined) {
|
|
25
|
+
const resolved = path.resolve(root);
|
|
26
|
+
const native = realpathOr(fs.realpathSync.native, resolved);
|
|
27
|
+
found = [...new Set([resolved, realpathOr(fs.realpathSync, resolved), native])];
|
|
28
|
+
spellings.set(root, found);
|
|
29
|
+
natives.set(root, native);
|
|
30
|
+
}
|
|
31
|
+
return found;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The spelling of `root` that `file` is under (or is), or null when it is outside it. A
|
|
36
|
+
* file spelled unlike any of the root's own (a long name under a root given short, or the
|
|
37
|
+
* other way round) is under the root when its native real path is; the spelling returned
|
|
38
|
+
* is then the file's own leading directories, checked to be the root. Either way it is
|
|
39
|
+
* the root as the file spells it.
|
|
40
|
+
*/
|
|
41
|
+
export function rootSpellingOf(root, file) {
|
|
42
|
+
const spellingsOfRoot = rootSpellings(root);
|
|
43
|
+
for (const spelling of spellingsOfRoot) {
|
|
44
|
+
// The file's own characters: on Windows the match ignores case, and Babel compares
|
|
45
|
+
// its root with the file name case-sensitively.
|
|
46
|
+
if (under(file, spelling)) return file.slice(0, spelling.length);
|
|
47
|
+
}
|
|
48
|
+
const nativeRoot = natives.get(root);
|
|
49
|
+
if (!under(realpathOr(fs.realpathSync.native, file), nativeRoot)) return null;
|
|
50
|
+
const depth = nativeRoot.split(path.sep).length;
|
|
51
|
+
const own = file.split(path.sep).slice(0, depth).join(path.sep);
|
|
52
|
+
return realpathOr(fs.realpathSync.native, own) === nativeRoot ? own : null;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function under(file, dir) {
|
|
56
|
+
const target = fold(file);
|
|
57
|
+
const prefix = fold(dir);
|
|
58
|
+
return (
|
|
59
|
+
target === prefix || target.startsWith(prefix.endsWith(path.sep) ? prefix : prefix + path.sep)
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** `file` relative to `root`, in whichever spelling of the root it is under. */
|
|
64
|
+
export function relativeToRoot(root, file) {
|
|
65
|
+
const spelling = rootSpellingOf(root, file);
|
|
66
|
+
return spelling === null
|
|
67
|
+
? path.relative(root, file)
|
|
68
|
+
: file.slice(spelling.length).replace(/^[\\/]/, "");
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function realpathOr(realpath, file) {
|
|
72
|
+
try {
|
|
73
|
+
return realpath(file);
|
|
74
|
+
} catch {
|
|
75
|
+
return file;
|
|
76
|
+
}
|
|
77
|
+
}
|
package/dist/native/runner.mjs
CHANGED
|
@@ -8,30 +8,13 @@
|
|
|
8
8
|
// (resident-library lazy init — must be preserved across files, it never
|
|
9
9
|
// re-runs) and test-phase state (pollution the next file's reset removes).
|
|
10
10
|
// See reset.mjs for the full attribution model.
|
|
11
|
-
// vitest >=4.1 exports TestRunner from the main entry; 4.0.x only exposes it as
|
|
12
|
-
// VitestTestRunner via the "vitest/runners" subpath, which vitest 5 REMOVED.
|
|
13
|
-
// Prefer the main entry, and reach for the old subpath only when the main export
|
|
14
|
-
// is absent.
|
|
15
|
-
//
|
|
16
|
-
// The fallback specifier is computed rather than written literally, because a
|
|
17
|
-
// literal is resolved when this module is TRANSFORMED, not when the branch runs.
|
|
18
|
-
// Against vitest 5 that resolution fails ("./runners" is not exported), and the
|
|
19
|
-
// failure is silent in the worst way: the run reports unhandled errors, executes
|
|
20
|
-
// no tests, and still exits 0. A computed specifier keeps the branch invisible to
|
|
21
|
-
// the resolver, so 4.0.x keeps its fallback and 5 never looks for it.
|
|
22
11
|
import { setTimeout as delay } from "node:timers/promises";
|
|
23
|
-
import * as vitest from "vitest";
|
|
24
12
|
import { requireProjectContext } from "../context.mjs";
|
|
25
13
|
import { VitestNativeError } from "../errors.mjs";
|
|
26
14
|
import { describeProblems, residentProblems } from "./resident-health.mjs";
|
|
15
|
+
import NativeTestRunner from "./test-runner.mjs";
|
|
27
16
|
|
|
28
|
-
|
|
29
|
-
if (!TestRunner) {
|
|
30
|
-
const legacyRunners = ["vitest", "runners"].join("/");
|
|
31
|
-
({ VitestTestRunner: TestRunner } = await import(/* @vite-ignore */ legacyRunners));
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export default class NativeHotRunner extends TestRunner {
|
|
17
|
+
export default class NativeHotRunner extends NativeTestRunner {
|
|
35
18
|
// Fires once per test file, before its setup files and its own modules are
|
|
36
19
|
// imported — the point where Vitest resets the module graph when isolation is on.
|
|
37
20
|
// The hot pool runs with isolation off so the worker survives, so the reset happens
|
package/dist/native/setup.mjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
// Native-engine setup file (injected into test.setupFiles by the plugin). Installs
|
|
2
2
|
// globals, registers the ESM loader hook, installs the CJS require hooks, and
|
|
3
3
|
// builds any third-party preset mocks the project uses.
|
|
4
|
-
import { createRequire,
|
|
4
|
+
import { createRequire, registerHooks } from "node:module";
|
|
5
5
|
import * as nativeLoader from "./loader.mjs";
|
|
6
6
|
import path from "node:path";
|
|
7
|
-
import { afterEach, expect, vi } from "vitest";
|
|
7
|
+
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, test, vi } from "vitest";
|
|
8
8
|
import { installGlobals, installErrorUtils, installExpoRuntime } from "./globals.mjs";
|
|
9
9
|
import { installRequireHooks } from "./hooks.mjs";
|
|
10
10
|
import { installRntlJest } from "./rntl-hooks.mjs";
|
|
@@ -12,14 +12,20 @@ import { installRegistry } from "./registry.mjs";
|
|
|
12
12
|
import { enableV8CompileCache } from "./compile-cache.mjs";
|
|
13
13
|
import * as presetFactories from "../presets.mjs";
|
|
14
14
|
import { animatedMatchers } from "../matchers.mjs";
|
|
15
|
-
import { serializer as rnSerializer } from "../serializer.mjs";
|
|
16
15
|
import { VitestNativeError } from "../errors.mjs";
|
|
17
|
-
import {
|
|
16
|
+
import {
|
|
17
|
+
CONTEXT_ENV,
|
|
18
|
+
activeRegistry,
|
|
19
|
+
assertBootContext,
|
|
20
|
+
requireProjectContext,
|
|
21
|
+
} from "../context.mjs";
|
|
18
22
|
import { importPresetModule, presetModulePaths } from "../preset-modules.mjs";
|
|
19
23
|
import { installNitroProxy } from "./nitro.mjs";
|
|
20
24
|
import { createExpoNativeModuleResolver } from "./expo-native-modules.mjs";
|
|
25
|
+
import { beginTestFile, installLibraryJest } from "./library-jest.mjs";
|
|
21
26
|
import { cacheRootFor } from "./transform.mjs";
|
|
22
27
|
import { installKnownNativeModules } from "./native-modules.mjs";
|
|
28
|
+
import { OWNERSHIP_META_KEY, installOwnershipLedger } from "./ownership-ledger.mjs";
|
|
23
29
|
import { installRntlAutoCleanup } from "../rntl-support.mjs";
|
|
24
30
|
import { registerVirtualTextHost } from "./rntl-text-host.mjs";
|
|
25
31
|
import { shareProjectModules } from "../jest-compat/node-registry.mjs";
|
|
@@ -163,6 +169,45 @@ const nodeTransformPkgs = [...new Set([...transformPkgs, ...Object.keys(presetEx
|
|
|
163
169
|
// imports react-native, after this setup runs) so its bytecode is cached to disk
|
|
164
170
|
// and reused on the next file/worker/run. Covers the stock (non-hot) path.
|
|
165
171
|
enableV8CompileCache(projectRoot);
|
|
172
|
+
// Before anything else loads through Node: the guard records every evaluation from here
|
|
173
|
+
// on, on both sides (docs/ownership-guard.md). Once per worker; the hot worker installs
|
|
174
|
+
// it at boot instead.
|
|
175
|
+
const ownershipLedger = installOwnershipLedger({ projectRoot });
|
|
176
|
+
// Every dual evaluation this file caused, once its tests are done: one a library
|
|
177
|
+
// swallowed (`try { require(x) } catch {}` turns the error into "x is missing") still
|
|
178
|
+
// fails the file in error mode, and the reporter prints them all at the end of the run
|
|
179
|
+
// whatever the reporter or `silent` setting (ownership-reporter in plugin.ts).
|
|
180
|
+
const ownershipStart = ownershipLedger.conflicts.length;
|
|
181
|
+
// No declared parameters: Vitest 5 parses a hook's first parameter as a fixture
|
|
182
|
+
// pattern and passes the file suite second, Vitest 4 passes it first.
|
|
183
|
+
afterAll(function () {
|
|
184
|
+
// oxlint-disable-next-line prefer-rest-params -- a rest parameter is parsed as a fixture too
|
|
185
|
+
const file = [...arguments].find((arg) => arg && typeof arg === "object" && "tasks" in arg);
|
|
186
|
+
const found = ownershipLedger.conflicts.slice(ownershipStart);
|
|
187
|
+
if (file && typeof file === "object") {
|
|
188
|
+
file.meta ??= {};
|
|
189
|
+
file.meta[OWNERSHIP_META_KEY] = {
|
|
190
|
+
mode: ownershipLedger.mode,
|
|
191
|
+
conflicts: found.map(({ file: f, first, second, route, cause, via }) => ({
|
|
192
|
+
file: f,
|
|
193
|
+
first,
|
|
194
|
+
second,
|
|
195
|
+
route,
|
|
196
|
+
cause,
|
|
197
|
+
via,
|
|
198
|
+
})),
|
|
199
|
+
allowUsed: [...ownershipLedger.allowUsed],
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
if (ownershipLedger.mode === "error" && found.length > 0) {
|
|
203
|
+
throw new VitestNativeError(
|
|
204
|
+
"SOURCE_EVALUATED_TWICE",
|
|
205
|
+
`${found.length} file(s) were evaluated by both Node and Vite during this test file ` +
|
|
206
|
+
`(ownership: 'error'), including any whose error a try/catch swallowed:\n` +
|
|
207
|
+
found.map((c) => c.message).join("\n\n"),
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
});
|
|
166
211
|
installGlobals({ projectRoot, platform });
|
|
167
212
|
// Before React Native loads: its lookups ask the boundary which modules exist.
|
|
168
213
|
installKnownNativeModules(projectRoot);
|
|
@@ -186,41 +231,34 @@ installRntlAutoCleanup(afterEach, {
|
|
|
186
231
|
Boolean(globalThis.__vitest_native_hot_reset) ||
|
|
187
232
|
globalThis.__vitest_worker__?.config?.isolate === false,
|
|
188
233
|
});
|
|
189
|
-
//
|
|
234
|
+
// Install the loader hooks once per WORKER, not per file: under the hot runtime this setup
|
|
190
235
|
// file re-evaluates per test file in a persistent worker, and re-registering
|
|
191
236
|
// would stack a new loader-hook layer on every file. (installGlobals and
|
|
192
237
|
// installRequireHooks are internally guarded the same way.)
|
|
193
238
|
/**
|
|
194
|
-
* Install the ESM loader hooks (loader.mjs).
|
|
195
|
-
*
|
|
196
|
-
*
|
|
197
|
-
* of an externalized module blocks this thread on a synchronous cross-thread request
|
|
198
|
-
* (Node's makeSyncRequest). Profiled on a 105-file production suite, that wait was 18%
|
|
199
|
-
* of all worker CPU time. `module.registerHooks()` (Node 22.15+, 23.5+) runs the same
|
|
200
|
-
* hooks synchronously on this thread instead, so it is used where Node's in-thread hooks
|
|
201
|
-
* are reliable (inThreadHooksReliable in loader.mjs); every other Node keeps
|
|
202
|
-
* `register()`. VITEST_NATIVE_LOADER_THREAD=1 forces the threaded hooks.
|
|
239
|
+
* Install the ESM loader hooks (loader.mjs) on this thread with `module.registerHooks()`,
|
|
240
|
+
* which runs them synchronously beside the require hooks. Every supported Node has
|
|
241
|
+
* reliable in-thread hooks (engines.node starts at 22.22.3 and 24.12 for that reason).
|
|
203
242
|
*
|
|
204
243
|
* In-thread hooks also run for `require()`. CommonJS is already handled by the require
|
|
205
|
-
* hooks (hooks.mjs), so those calls go straight to Node's default resolution
|
|
206
|
-
* did when only `import` reached the threaded loader.
|
|
244
|
+
* hooks (hooks.mjs), so those calls go straight to Node's default resolution.
|
|
207
245
|
*/
|
|
208
246
|
function installLoaderHooks(data) {
|
|
209
|
-
if (
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
247
|
+
if (typeof registerHooks !== "function") {
|
|
248
|
+
// A runtime that reports a supported Node version without Node's in-thread hooks
|
|
249
|
+
// (Bun reports Node 26) cannot run the native engine's loader.
|
|
250
|
+
throw new VitestNativeError(
|
|
251
|
+
"UNSUPPORTED_NODE",
|
|
252
|
+
`This runtime reports Node ${process.versions.node} but has no \`module.registerHooks\`, ` +
|
|
253
|
+
`which the native engine's loader needs. Run Vitest with Node 22.22.3 or later on Node 22, ` +
|
|
254
|
+
`24.12.0 or later on Node 24, or Node 26 and later.`,
|
|
255
|
+
);
|
|
217
256
|
}
|
|
218
257
|
nativeLoader.initialize({
|
|
219
258
|
...data,
|
|
220
259
|
inThread: true,
|
|
221
260
|
hotGeneration: globalThis.__vitest_native_hot_generation,
|
|
222
261
|
});
|
|
223
|
-
globalThis.__vitest_native_loader_mode = "in-thread";
|
|
224
262
|
const fromRequire = nativeLoader.isRequireContext;
|
|
225
263
|
registerHooks({
|
|
226
264
|
resolve: (specifier, context, nextResolve) =>
|
|
@@ -239,8 +277,7 @@ if (!globalThis.__vitest_native_loader_registered) {
|
|
|
239
277
|
// for the whole run because Node's ESM registry cannot be invalidated. Only
|
|
240
278
|
// meaningful under the hot runtime — `__vitest_native_hot_reset` is installed by
|
|
241
279
|
// the hot worker before this setup file runs, and nothing bumps the counter
|
|
242
|
-
// otherwise.
|
|
243
|
-
// own thread. Set VITEST_NATIVE_HOT_ESM_GEN=0 to opt out (and to mutation-test the
|
|
280
|
+
// otherwise. Set VITEST_NATIVE_HOT_ESM_GEN=0 to opt out (and to mutation-test the
|
|
244
281
|
// isolation gate that covers this).
|
|
245
282
|
if (
|
|
246
283
|
globalThis.__vitest_native_hot_reset &&
|
|
@@ -266,12 +303,21 @@ if (!globalThis.__vitest_native_loader_registered) {
|
|
|
266
303
|
});
|
|
267
304
|
}
|
|
268
305
|
// Serve React Native from the precompiled registry when the plugin produced one
|
|
269
|
-
// (see registry.mjs)
|
|
306
|
+
// (see registry.mjs), or when the one it is compiling in the background has landed
|
|
307
|
+
// (activeRegistry). Installed BEFORE the require hooks so their preset-mock
|
|
270
308
|
// redirect wraps this one and keeps precedence. Any module the registry cannot
|
|
271
309
|
// serve — a computed require, a package in `transform` — falls through to the
|
|
272
310
|
// hooks below, so this is purely a faster path to the same modules.
|
|
273
|
-
|
|
274
|
-
|
|
311
|
+
// A registry still compiling in the background (activeRegistry) has no library registry
|
|
312
|
+
// yet: runContext.libraryRegistry is set only beside a registry the run started with.
|
|
313
|
+
const registryFile = activeRegistry(runContext);
|
|
314
|
+
if (registryFile) {
|
|
315
|
+
const installed = installRegistry(
|
|
316
|
+
registryFile,
|
|
317
|
+
projectRoot,
|
|
318
|
+
sourceExts,
|
|
319
|
+
runContext.libraryRegistry,
|
|
320
|
+
);
|
|
275
321
|
if (diagnostics) {
|
|
276
322
|
console.log(
|
|
277
323
|
`[vitest-native] (native) precompiled RN registry ${installed ? "installed" : "unavailable; using per-file module loading"}`,
|
|
@@ -293,6 +339,23 @@ installErrorUtils(projectRoot);
|
|
|
293
339
|
// Before any test or user setup file imports RNTL: its fake-timer support reads a
|
|
294
340
|
// `jest` of its own (see rntl-hooks.mjs).
|
|
295
341
|
installRntlJest(vi);
|
|
342
|
+
// Libraries' own test support (their jestSetup and mock files) runs with a `jest` of
|
|
343
|
+
// its own, with Jest's module-registry semantics (library-jest.mjs).
|
|
344
|
+
installLibraryJest(vi, { expect, describe, it, test, beforeEach, afterEach, beforeAll, afterAll });
|
|
345
|
+
// Each test file keeps only what a still-loaded library support module registered,
|
|
346
|
+
// under every pool: the hot runtime and `isolate: false` reuse a worker.
|
|
347
|
+
beginTestFile();
|
|
348
|
+
// The setup files of libraries that run as themselves (LIBRARY_SETUP_FILES in
|
|
349
|
+
// preset-map.ts), ahead of the project's, as a Jest suite lists them. Run afresh for
|
|
350
|
+
// every test file, so each file's mocks are new ones, as under Jest.
|
|
351
|
+
for (const request of runContext.librarySetupFiles ?? []) {
|
|
352
|
+
const load = createRequire(path.join(projectRoot, "package.json"));
|
|
353
|
+
// Not installed in this project: nothing to run.
|
|
354
|
+
const file = resolvedOrNull(load, request);
|
|
355
|
+
if (file === null) continue;
|
|
356
|
+
delete load.cache[file];
|
|
357
|
+
load(request);
|
|
358
|
+
}
|
|
296
359
|
// Through Vitest, so a TypeScript preset module loads. Its export lists already
|
|
297
360
|
// reached the ESM loader from the plugin; a module the plugin did not read would be
|
|
298
361
|
// served by the real package there.
|
|
@@ -413,6 +476,17 @@ g.__vitest_native_control = {
|
|
|
413
476
|
},
|
|
414
477
|
};
|
|
415
478
|
|
|
479
|
+
// A library's own mock whose module body sets up globals of the test environment
|
|
480
|
+
// (worklets' sets requestAnimationFrame) is built now, as a Jest suite installs it in
|
|
481
|
+
// its setup, and after the control above, which tells it this is the native engine.
|
|
482
|
+
// Built later, inside a test that installed fake timers, it would replace the faked
|
|
483
|
+
// requestAnimationFrame while the faked cancelAnimationFrame stays.
|
|
484
|
+
for (const { pkg, mod } of presetDefs) {
|
|
485
|
+
if (mod[Symbol.for("vitest-native.preset-built-at-setup")] === true) {
|
|
486
|
+
void g.__vitest_native_preset_mocks[pkg];
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
|
|
416
490
|
// --- RNTL built-in matchers (toBeOnTheScreen, toBeDisabled, toHaveStyle, …) ---
|
|
417
491
|
// The mock engine's setup registers these; the native engine must too, or
|
|
418
492
|
// `engine:'native'` users have no jest-native/RNTL matchers (and a jest-compat
|
|
@@ -453,15 +527,13 @@ try {
|
|
|
453
527
|
// --- RNTL: register RCTVirtualText as a text host ---
|
|
454
528
|
// Real RN renders a NESTED <Text> as the host "RCTVirtualText", which RNTL's text
|
|
455
529
|
// hosts omit. On RNTL 14 that makes a nested <Text> throw "Text strings must be
|
|
456
|
-
// rendered within a <Text> component"; on RNTL
|
|
457
|
-
//
|
|
458
|
-
//
|
|
530
|
+
// rendered within a <Text> component"; on RNTL 13 the inner text is not a text
|
|
531
|
+
// element, so getByText cannot find it. rntl-text-host.mjs explains both and patches
|
|
532
|
+
// whichever shape the installed RNTL has (dist/ is 14, build/ is 13).
|
|
459
533
|
//
|
|
460
534
|
// Requiring the module here adds no evaluation-order hazard (see the hot-reset
|
|
461
|
-
// block): on 13 and 14 it is a leaf
|
|
462
|
-
//
|
|
463
|
-
// required it and react-native is loaded by now, so this is a cache hit. `req` is
|
|
464
|
-
// the project-root require the matchers use, so both patch the same RNTL copy.
|
|
535
|
+
// block): on 13 and 14 it is a leaf. `req` is the project-root require the matchers
|
|
536
|
+
// use, so both patch the same RNTL copy.
|
|
465
537
|
try {
|
|
466
538
|
let registered = null;
|
|
467
539
|
let lastError;
|
|
@@ -493,7 +565,7 @@ try {
|
|
|
493
565
|
}
|
|
494
566
|
|
|
495
567
|
expect.extend(animatedMatchers);
|
|
496
|
-
|
|
568
|
+
// Snapshots print with Vitest's own ReactTestComponent plugin, as under Jest (src/setup.ts).
|
|
497
569
|
|
|
498
570
|
// One instance of a project module per test file: Node's `require` of a project file the
|
|
499
571
|
// test already imported gets Vite's instance. Last, after React Native and the setup's
|
|
@@ -506,3 +578,12 @@ shareProjectModules();
|
|
|
506
578
|
// AppContainer mounts) to render null, so LogBoxStateSubscription never mounts
|
|
507
579
|
// and never schedules its out-of-act setState. See boundary.mjs + the regression
|
|
508
580
|
// test tests-native/logbox-act.test.tsx.
|
|
581
|
+
|
|
582
|
+
/** Where `load` resolves `request`, or null when it is not installed. */
|
|
583
|
+
function resolvedOrNull(load, request) {
|
|
584
|
+
try {
|
|
585
|
+
return load.resolve(request);
|
|
586
|
+
} catch {
|
|
587
|
+
return null;
|
|
588
|
+
}
|
|
589
|
+
}
|