vitest-native 0.4.1 → 0.6.0
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/CHANGELOG.md +98 -1
- package/README.md +270 -180
- package/dist/helpers.cjs +13 -0
- package/dist/helpers.mjs +13 -0
- package/dist/{index-DEB7OFaJ.d.cts → index-DGNLmGjq.d.mts} +19 -12
- package/dist/{index-CJIhukN_.d.mts → index-Dr3WvKWI.d.cts} +21 -10
- package/dist/index.cjs +106 -39
- package/dist/index.d.cts +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +103 -36
- package/dist/jest-compat.cjs +1 -1
- package/dist/native/boundary.mjs +82 -32
- package/dist/native/globals.mjs +1 -0
- package/dist/native/hooks.mjs +35 -7
- package/dist/native/loader.mjs +18 -5
- package/dist/native/reset.mjs +79 -43
- package/dist/native/resolve.mjs +25 -7
- package/dist/native/runner.mjs +13 -2
- package/dist/native/setup.mjs +95 -4
- package/dist/native/transform.mjs +15 -9
- package/dist/native/worker.mjs +20 -3
- package/dist/{pool-oPTZiSXU.mjs → pool-B7LBfcMQ.mjs} +10 -1
- package/dist/{pool-B4mWN3N5.cjs → pool-p54QV1dQ.cjs} +11 -2
- package/dist/{preset-map-OJm5IWuL.mjs → preset-map-C1UwEald.mjs} +3 -1
- package/dist/{preset-map-C3jxnkiX.cjs → preset-map-CNBOeqAu.cjs} +3 -1
- package/dist/{presets-2I68Ipyl.cjs → presets-BPVVlfI2.cjs} +93 -23
- package/dist/{presets-DOGXQzYT.mjs → presets-DVLDrPC-.mjs} +86 -22
- package/dist/presets.cjs +2 -1
- package/dist/presets.d.cts +2 -2
- package/dist/presets.d.mts +2 -2
- package/dist/presets.mjs +2 -2
- package/dist/setup.cjs +71 -13
- package/dist/setup.mjs +69 -11
- package/docs/migrating-from-jest.md +6 -4
- package/docs/migrating-from-vitest-react-native.md +6 -3
- package/docs/release-readiness.md +52 -0
- package/package.json +31 -20
package/dist/native/reset.mjs
CHANGED
|
@@ -5,49 +5,71 @@
|
|
|
5
5
|
// source, setup files. What it can NOT touch is state living in the worker's
|
|
6
6
|
// resident Node require cache: React Native itself (externalized by design),
|
|
7
7
|
// other externalized CJS deps, and our boundary mocks. This module covers that
|
|
8
|
-
// gap with
|
|
8
|
+
// gap with a boot-baseline + import-attribution model:
|
|
9
9
|
//
|
|
10
|
-
// THE ATTRIBUTION PROBLEM:
|
|
11
|
-
//
|
|
12
|
-
//
|
|
13
|
-
//
|
|
14
|
-
//
|
|
15
|
-
// State created during the TEST phase, by contrast, is pollution to remove.
|
|
10
|
+
// THE ATTRIBUTION PROBLEM: top-level code in an app/test module re-runs for
|
|
11
|
+
// every file and must be cleaned, while a resident externalized dependency
|
|
12
|
+
// initializes only once and may need to retain its process-wide state. A
|
|
13
|
+
// blanket "bless everything created during import" policy confuses the two
|
|
14
|
+
// and leaks app globals, env mutations, and listeners across files.
|
|
16
15
|
//
|
|
17
16
|
// The split is observable: runner.mjs calls bless() from onBeforeRunFiles —
|
|
18
17
|
// after the test module (and its resident deps) finished importing, before
|
|
19
|
-
// any test runs.
|
|
20
|
-
//
|
|
21
|
-
// NEXT file's setup.
|
|
22
|
-
//
|
|
23
|
-
//
|
|
18
|
+
// any test runs. Listener call sites inside node_modules are treated as
|
|
19
|
+
// resident import state; listeners created by app/test modules stay tracked
|
|
20
|
+
// and are removed at the NEXT file's setup. Globals and process.env always
|
|
21
|
+
// return to the worker boot baseline, with an explicit preserveGlobals escape
|
|
22
|
+
// hatch for external libraries that intentionally publish a global registry.
|
|
23
|
+
// If bless() never fires (consumer overrode `runner`), attribution-dependent
|
|
24
|
+
// teardowns stay disarmed — fail-open rather than guessing.
|
|
24
25
|
//
|
|
25
26
|
// Covered surfaces:
|
|
26
27
|
// 1. RN event listeners — every NativeEventEmitter (AppState, Appearance,
|
|
27
28
|
// Keyboard, …) delegates to the RCTDeviceEventEmitter singleton, so one
|
|
28
29
|
// wrapped addListener tracks the whole RN JS event surface. Test-phase
|
|
29
|
-
// subscriptions are removed via their own public subscription.remove()
|
|
30
|
-
// import-phase subscriptions are blessed.
|
|
31
|
-
// 2. RN module state with known mutation APIs (Dimensions
|
|
32
|
-
// from a boot-time snapshot (value-restore: no attribution needed).
|
|
33
|
-
// 2b. process.env — restored to the
|
|
34
|
-
// 3.
|
|
35
|
-
// afterEach (registered during the first file) never re-registers for
|
|
36
|
-
// later files; cleanup() is called explicitly.
|
|
30
|
+
// subscriptions are removed via their own public subscription.remove().
|
|
31
|
+
// Only import-phase subscriptions owned by node_modules are blessed.
|
|
32
|
+
// 2. RN module state with known mutation APIs (Dimensions, Appearance) —
|
|
33
|
+
// restored from a boot-time snapshot (value-restore: no attribution needed).
|
|
34
|
+
// 2b. process.env — restored to the worker boot snapshot.
|
|
35
|
+
// 3. Vitest timers/global/env stubs — restored by setup.mjs before this reset.
|
|
37
36
|
// 4. Boundary/preset mocks that registered callbacks in
|
|
38
37
|
// globalThis.__vitest_native_resets (none are stateful today; the
|
|
39
38
|
// registry is the extension point).
|
|
40
|
-
// 5. globalThis keys added
|
|
41
|
-
//
|
|
42
|
-
//
|
|
43
|
-
//
|
|
39
|
+
// 5. globalThis keys added by a file — deleted. The baseline starts at the
|
|
40
|
+
// first per-file call (after Vitest injected its per-batch globals) and
|
|
41
|
+
// never grows implicitly. Mutations of pre-existing keys are not restored
|
|
42
|
+
// (documented limitation).
|
|
44
43
|
import { createRequire } from "node:module";
|
|
45
44
|
import path from "node:path";
|
|
45
|
+
import { fileURLToPath } from "node:url";
|
|
46
46
|
|
|
47
47
|
// Keys owned by the harness or this plugin — never deleted by the globals diff.
|
|
48
|
-
const
|
|
49
|
-
// Vitest
|
|
50
|
-
const ENV_PRESERVED =
|
|
48
|
+
const HARNESS_GLOBALS = /^(__vitest_native_|__VITEST|__coverage__|__VITE)/;
|
|
49
|
+
// Vitest updates these scheduler-owned values between tasks.
|
|
50
|
+
const ENV_PRESERVED = new Set(["VITEST_POOL_ID", "VITEST_WORKER_ID"]);
|
|
51
|
+
const RESET_FILE = fileURLToPath(import.meta.url).replaceAll("\\", "/");
|
|
52
|
+
const DEFAULT_PRESERVED_GLOBALS = ["__STORYBOOK_ADDONS_PREVIEW"];
|
|
53
|
+
|
|
54
|
+
function isResidentImportListener(stack, projectRoot) {
|
|
55
|
+
const root = projectRoot.replaceAll("\\", "/").replace(/\/$/, "");
|
|
56
|
+
let sawExternalFrame = false;
|
|
57
|
+
|
|
58
|
+
for (const rawLine of stack.split("\n").slice(1)) {
|
|
59
|
+
const line = rawLine.replaceAll("\\", "/");
|
|
60
|
+
if (line.includes(RESET_FILE)) continue;
|
|
61
|
+
if (!line.includes(`${root}/`)) continue;
|
|
62
|
+
if (line.includes("/node_modules/")) {
|
|
63
|
+
sawExternalFrame = true;
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
// A project-owned frame means this listener was created by app/test code,
|
|
67
|
+
// even when the call passed through React Native internals.
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return sawExternalFrame;
|
|
72
|
+
}
|
|
51
73
|
|
|
52
74
|
/**
|
|
53
75
|
* Called once at hot-worker boot, AFTER react-native has been preloaded and its
|
|
@@ -56,17 +78,20 @@ const ENV_PRESERVED = /^VITEST_/;
|
|
|
56
78
|
* hotReset is invoked by setup.mjs at the top of every file; bless by
|
|
57
79
|
* runner.mjs between a file's import phase and its first test.
|
|
58
80
|
*/
|
|
59
|
-
export function installHotReset({ projectRoot, diagnostics }) {
|
|
81
|
+
export function installHotReset({ projectRoot, diagnostics, preserveGlobals = [] }) {
|
|
60
82
|
const req = createRequire(path.join(projectRoot, "package.json"));
|
|
61
83
|
const RN = req("react-native");
|
|
84
|
+
const explicitlyPreserved = new Set([...DEFAULT_PRESERVED_GLOBALS, ...preserveGlobals]);
|
|
62
85
|
|
|
63
86
|
// --- (1) Track listeners added to the RCTDeviceEventEmitter singleton ---
|
|
64
|
-
const tracked = new
|
|
87
|
+
const tracked = new Map();
|
|
65
88
|
const emitter = RN.DeviceEventEmitter;
|
|
66
89
|
const origAddListener = emitter.addListener.bind(emitter);
|
|
67
90
|
emitter.addListener = (type, listener, context) => {
|
|
68
91
|
const sub = origAddListener(type, listener, context);
|
|
69
|
-
tracked.
|
|
92
|
+
tracked.set(sub, {
|
|
93
|
+
residentImport: isResidentImportListener(new Error().stack || "", projectRoot),
|
|
94
|
+
});
|
|
70
95
|
return sub;
|
|
71
96
|
};
|
|
72
97
|
|
|
@@ -78,12 +103,16 @@ export function installHotReset({ projectRoot, diagnostics }) {
|
|
|
78
103
|
screen: { ...RN.Dimensions.get("screen") },
|
|
79
104
|
};
|
|
80
105
|
} catch {}
|
|
106
|
+
let colorScheme = null;
|
|
107
|
+
try {
|
|
108
|
+
colorScheme = RN.Appearance.getColorScheme?.() ?? null;
|
|
109
|
+
} catch {}
|
|
81
110
|
|
|
82
|
-
// --- (2b) process.env snapshot
|
|
83
|
-
|
|
111
|
+
// --- (2b) Fixed process.env worker-boot snapshot ---
|
|
112
|
+
const envBaseline = { ...process.env };
|
|
84
113
|
|
|
85
|
-
// --- (5) globalThis baseline: starts at the first per-file call
|
|
86
|
-
//
|
|
114
|
+
// --- (5) globalThis baseline: starts at the first per-file call and remains
|
|
115
|
+
// fixed. Explicitly preserved keys may join it at an import boundary. ---
|
|
87
116
|
let globalBaseline = null;
|
|
88
117
|
|
|
89
118
|
// Attribution-dependent teardowns run only once bless() has fired at least
|
|
@@ -93,11 +122,15 @@ export function installHotReset({ projectRoot, diagnostics }) {
|
|
|
93
122
|
function bless() {
|
|
94
123
|
armed = true;
|
|
95
124
|
if (globalBaseline) {
|
|
96
|
-
for (const key of
|
|
125
|
+
for (const key of explicitlyPreserved) {
|
|
126
|
+
if (Object.hasOwn(globalThis, key)) globalBaseline.add(key);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
// Resident external dependencies do not re-run, so retain only listeners
|
|
130
|
+
// whose import-time call stack belongs exclusively to node_modules.
|
|
131
|
+
for (const [sub, record] of tracked) {
|
|
132
|
+
if (record.residentImport) tracked.delete(sub);
|
|
97
133
|
}
|
|
98
|
-
envBaseline = { ...process.env };
|
|
99
|
-
// Import-phase subscriptions are legitimate resident state — stop tracking.
|
|
100
|
-
tracked.clear();
|
|
101
134
|
}
|
|
102
135
|
|
|
103
136
|
function hotReset() {
|
|
@@ -120,6 +153,9 @@ export function installHotReset({ projectRoot, diagnostics }) {
|
|
|
120
153
|
RN.Dimensions.set(dims);
|
|
121
154
|
} catch {}
|
|
122
155
|
}
|
|
156
|
+
try {
|
|
157
|
+
RN.Appearance.setColorScheme?.(colorScheme);
|
|
158
|
+
} catch {}
|
|
123
159
|
|
|
124
160
|
// (4) Boundary/preset mock reset callbacks.
|
|
125
161
|
const resets = globalThis.__vitest_native_resets;
|
|
@@ -134,28 +170,28 @@ export function installHotReset({ projectRoot, diagnostics }) {
|
|
|
134
170
|
if (!armed) return; // no bless yet → cannot attribute; fail open
|
|
135
171
|
|
|
136
172
|
// (1) Remove the previous file's test-phase RN event listeners.
|
|
137
|
-
for (const sub of tracked) {
|
|
173
|
+
for (const sub of tracked.keys()) {
|
|
138
174
|
try {
|
|
139
175
|
sub.remove();
|
|
140
176
|
} catch {}
|
|
141
177
|
}
|
|
142
178
|
tracked.clear();
|
|
143
179
|
|
|
144
|
-
// (2b) Restore process.env to the
|
|
180
|
+
// (2b) Restore process.env to the worker boot snapshot.
|
|
145
181
|
for (const key of Object.keys(process.env)) {
|
|
146
|
-
if (ENV_PRESERVED.
|
|
182
|
+
if (ENV_PRESERVED.has(key)) continue;
|
|
147
183
|
if (!(key in envBaseline)) delete process.env[key];
|
|
148
184
|
else if (process.env[key] !== envBaseline[key]) process.env[key] = envBaseline[key];
|
|
149
185
|
}
|
|
150
186
|
for (const key of Object.keys(envBaseline)) {
|
|
151
|
-
if (!(key in process.env) && !ENV_PRESERVED.
|
|
187
|
+
if (!(key in process.env) && !ENV_PRESERVED.has(key)) process.env[key] = envBaseline[key];
|
|
152
188
|
}
|
|
153
189
|
|
|
154
190
|
// (5) Delete globals added during the previous test phase.
|
|
155
191
|
const deleted = [];
|
|
156
192
|
for (const key of Reflect.ownKeys(globalThis)) {
|
|
157
193
|
if (globalBaseline.has(key)) continue;
|
|
158
|
-
if (typeof key === "string" &&
|
|
194
|
+
if (typeof key === "string" && HARNESS_GLOBALS.test(key)) continue;
|
|
159
195
|
try {
|
|
160
196
|
delete globalThis[key];
|
|
161
197
|
if (diagnostics) deleted.push(String(key));
|
package/dist/native/resolve.mjs
CHANGED
|
@@ -1,20 +1,38 @@
|
|
|
1
|
-
// Metro-style platform-extension resolution: prefer
|
|
2
|
-
// and fall back to a directory index.
|
|
1
|
+
// Metro-style platform-extension resolution: prefer the configured platform,
|
|
2
|
+
// then .native, then generic TS/JS variants, and fall back to a directory index.
|
|
3
|
+
// Shared by the require hook and the loader.
|
|
3
4
|
import fs from "node:fs";
|
|
4
5
|
import path from "node:path";
|
|
5
6
|
|
|
6
|
-
|
|
7
|
+
function extensionsFor(platform) {
|
|
8
|
+
const suffix = platform === "android" ? "android" : "ios";
|
|
9
|
+
return [
|
|
10
|
+
`.${suffix}.tsx`,
|
|
11
|
+
`.${suffix}.ts`,
|
|
12
|
+
`.${suffix}.jsx`,
|
|
13
|
+
`.${suffix}.js`,
|
|
14
|
+
".native.tsx",
|
|
15
|
+
".native.ts",
|
|
16
|
+
".native.jsx",
|
|
17
|
+
".native.js",
|
|
18
|
+
".tsx",
|
|
19
|
+
".ts",
|
|
20
|
+
".jsx",
|
|
21
|
+
".js",
|
|
22
|
+
];
|
|
23
|
+
}
|
|
7
24
|
|
|
8
25
|
/**
|
|
9
26
|
* Given an absolute base path with no extension (e.g. ".../Foo"), return the
|
|
10
|
-
* first existing platform variant (".../Foo.ios.
|
|
27
|
+
* first existing platform variant (".../Foo.ios.tsx", etc.) or directory index,
|
|
11
28
|
* or null if none exist.
|
|
12
29
|
*/
|
|
13
|
-
export function resolvePlatformFile(absBase) {
|
|
14
|
-
|
|
30
|
+
export function resolvePlatformFile(absBase, platform = "ios") {
|
|
31
|
+
const extensions = extensionsFor(platform);
|
|
32
|
+
for (const ext of extensions) {
|
|
15
33
|
if (fs.existsSync(absBase + ext)) return absBase + ext;
|
|
16
34
|
}
|
|
17
|
-
for (const ext of
|
|
35
|
+
for (const ext of extensions) {
|
|
18
36
|
const idx = path.join(absBase, "index" + ext);
|
|
19
37
|
if (fs.existsSync(idx)) return idx;
|
|
20
38
|
}
|
package/dist/native/runner.mjs
CHANGED
|
@@ -8,9 +8,20 @@
|
|
|
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
|
-
|
|
11
|
+
// vitest >=4.1 exports TestRunner from the main entry; <4.1 only exposes it as
|
|
12
|
+
// VitestTestRunner via the (now-deprecated) "vitest/runners" subpath. Prefer the
|
|
13
|
+
// main entry so 4.1+ doesn't print a deprecation warning, and fall back for
|
|
14
|
+
// 4.0.x — the deprecated path is imported ONLY when the main export is absent,
|
|
15
|
+
// so 4.1+ never triggers the warning. A namespace import (not a named one) keeps
|
|
16
|
+
// a missing export as `undefined` instead of an ESM link error on 4.0.x.
|
|
17
|
+
import * as vitest from "vitest";
|
|
12
18
|
|
|
13
|
-
|
|
19
|
+
let TestRunner = vitest.TestRunner;
|
|
20
|
+
if (!TestRunner) {
|
|
21
|
+
({ VitestTestRunner: TestRunner } = await import("vitest/runners"));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export default class NativeHotRunner extends TestRunner {
|
|
14
25
|
async onBeforeRunFiles(files) {
|
|
15
26
|
globalThis.__vitest_native_hot_bless?.();
|
|
16
27
|
return super.onBeforeRunFiles?.(files);
|
package/dist/native/setup.mjs
CHANGED
|
@@ -7,6 +7,8 @@ import { expect, vi } from "vitest";
|
|
|
7
7
|
import { installGlobals } from "./globals.mjs";
|
|
8
8
|
import { installRequireHooks } from "./hooks.mjs";
|
|
9
9
|
import * as presetFactories from "../presets.mjs";
|
|
10
|
+
import { animatedMatchers } from "../matchers.mjs";
|
|
11
|
+
import { serializer as rnSerializer } from "../serializer.mjs";
|
|
10
12
|
|
|
11
13
|
// Hot runtime: surgical reset of state left by the PREVIOUS file. Setup files
|
|
12
14
|
// are force-inlined by Vitest, so this body re-runs per test file even when the
|
|
@@ -35,12 +37,20 @@ if (globalThis.__vitest_native_hot_reset) {
|
|
|
35
37
|
|
|
36
38
|
const projectRoot = process.env.VITEST_NATIVE_PROJECT_ROOT || process.cwd();
|
|
37
39
|
const diagnostics = process.env.VITEST_NATIVE_DIAGNOSTICS === "true";
|
|
40
|
+
const platform = process.env.VITEST_NATIVE_PLATFORM === "android" ? "android" : "ios";
|
|
41
|
+
const reactNativeVersion = process.env.VITEST_NATIVE_RN_VERSION || "0.0.0";
|
|
38
42
|
// Extra node_modules packages to transform (from the plugin's `transform` option).
|
|
39
43
|
let transformPkgs = [];
|
|
40
44
|
try {
|
|
41
45
|
if (process.env.VITEST_NATIVE_TRANSFORM)
|
|
42
46
|
transformPkgs = JSON.parse(process.env.VITEST_NATIVE_TRANSFORM);
|
|
43
47
|
} catch {}
|
|
48
|
+
// Asset extensions the Node require-hook should stub (matches the Vite graph).
|
|
49
|
+
let assetExts = [];
|
|
50
|
+
try {
|
|
51
|
+
if (process.env.VITEST_NATIVE_ASSET_EXTS)
|
|
52
|
+
assetExts = JSON.parse(process.env.VITEST_NATIVE_ASSET_EXTS);
|
|
53
|
+
} catch {}
|
|
44
54
|
|
|
45
55
|
// --- Third-party preset mocks ---
|
|
46
56
|
//
|
|
@@ -79,6 +89,16 @@ for (const name of presetNames) {
|
|
|
79
89
|
}
|
|
80
90
|
|
|
81
91
|
installGlobals();
|
|
92
|
+
// RNTL 13+ auto-registers matchers through the global expect when imported.
|
|
93
|
+
// Expose Vitest's expect only when the consumer has not enabled globals.
|
|
94
|
+
if (typeof globalThis.expect === "undefined") {
|
|
95
|
+
Object.defineProperty(globalThis, "expect", {
|
|
96
|
+
configurable: true,
|
|
97
|
+
enumerable: false,
|
|
98
|
+
value: expect,
|
|
99
|
+
writable: true,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
82
102
|
// register() once per WORKER, not per file: under the hot runtime this setup
|
|
83
103
|
// file re-evaluates per test file in a persistent worker, and re-registering
|
|
84
104
|
// would stack a new loader-hook layer on every file. (installGlobals and
|
|
@@ -86,10 +106,10 @@ installGlobals();
|
|
|
86
106
|
if (!globalThis.__vitest_native_loader_registered) {
|
|
87
107
|
globalThis.__vitest_native_loader_registered = true;
|
|
88
108
|
register("./loader.mjs", import.meta.url, {
|
|
89
|
-
data: { projectRoot, transformPkgs, presetExports },
|
|
109
|
+
data: { projectRoot, platform, reactNativeVersion, transformPkgs, presetExports },
|
|
90
110
|
});
|
|
91
111
|
}
|
|
92
|
-
installRequireHooks(projectRoot, transformPkgs);
|
|
112
|
+
installRequireHooks(projectRoot, transformPkgs, platform, reactNativeVersion, assetExts);
|
|
93
113
|
|
|
94
114
|
// Build the mock objects now that the require hooks are installed (preset
|
|
95
115
|
// factories may lazily resolve react-native at render time).
|
|
@@ -102,14 +122,82 @@ for (const { pkg, mod, presetName } of presetDefs) {
|
|
|
102
122
|
}
|
|
103
123
|
}
|
|
104
124
|
|
|
125
|
+
// --- Shared test-helper control surface ---
|
|
126
|
+
//
|
|
127
|
+
// Helpers should manipulate the real RN modules where that is coherent. Platform
|
|
128
|
+
// cannot be switched after module resolution: Platform.ios/Platform.android and
|
|
129
|
+
// every platform-specific import have already been selected, so setPlatform()
|
|
130
|
+
// reports a clear configuration error instead of creating a split-brain graph.
|
|
131
|
+
const req = createRequire(path.join(projectRoot, "package.json"));
|
|
132
|
+
const RN = req("react-native");
|
|
133
|
+
const initialDimensions = {
|
|
134
|
+
window: { ...RN.Dimensions.get("window") },
|
|
135
|
+
screen: { ...RN.Dimensions.get("screen") },
|
|
136
|
+
};
|
|
137
|
+
const initialColorScheme = RN.Appearance.getColorScheme?.() ?? "light";
|
|
138
|
+
|
|
139
|
+
function emitColorScheme(colorScheme) {
|
|
140
|
+
RN.Appearance.setColorScheme?.(colorScheme);
|
|
141
|
+
RN.DeviceEventEmitter.emit?.("appearanceChanged", { colorScheme });
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
g.__vitest_native_control = {
|
|
145
|
+
engine: "native",
|
|
146
|
+
setPlatform() {
|
|
147
|
+
throw new Error(
|
|
148
|
+
`[vitest-native] setPlatform() is only available with engine:'mock'. ` +
|
|
149
|
+
`The native engine selects platform files when the module graph loads. ` +
|
|
150
|
+
`Use reactNative({ platform: 'ios' | 'android' }) or separate Vitest projects.`,
|
|
151
|
+
);
|
|
152
|
+
},
|
|
153
|
+
setDimensions(dims) {
|
|
154
|
+
const next = { ...RN.Dimensions.get("window"), ...dims };
|
|
155
|
+
RN.Dimensions.set({ window: next, screen: next });
|
|
156
|
+
},
|
|
157
|
+
setColorScheme(colorScheme) {
|
|
158
|
+
emitColorScheme(colorScheme ?? "light");
|
|
159
|
+
},
|
|
160
|
+
mockNativeModule(name, implementation) {
|
|
161
|
+
g.__vitest_native_module_mocks[name] = implementation;
|
|
162
|
+
},
|
|
163
|
+
resetAllMocks() {
|
|
164
|
+
RN.Dimensions.set({
|
|
165
|
+
window: { ...initialDimensions.window },
|
|
166
|
+
screen: { ...initialDimensions.screen },
|
|
167
|
+
});
|
|
168
|
+
emitColorScheme(initialColorScheme);
|
|
169
|
+
for (const name of Object.keys(g.__vitest_native_module_mocks)) {
|
|
170
|
+
delete g.__vitest_native_module_mocks[name];
|
|
171
|
+
}
|
|
172
|
+
for (const presetMock of Object.values(g.__vitest_native_preset_mocks || {})) {
|
|
173
|
+
presetMock?._reset?.();
|
|
174
|
+
presetMock?._resetStore?.();
|
|
175
|
+
}
|
|
176
|
+
vi.clearAllMocks();
|
|
177
|
+
},
|
|
178
|
+
};
|
|
179
|
+
|
|
105
180
|
// --- RNTL built-in matchers (toBeOnTheScreen, toBeDisabled, toHaveStyle, …) ---
|
|
106
181
|
// The mock engine's setup registers these; the native engine must too, or
|
|
107
182
|
// `engine:'native'` users have no jest-native/RNTL matchers (and a jest-compat
|
|
108
183
|
// migration is worse off — jestCompatAliases no-ops its extend-expect on the
|
|
109
184
|
// promise that vitest-native registers them). Caught by the differential cross-check.
|
|
110
185
|
try {
|
|
111
|
-
|
|
112
|
-
|
|
186
|
+
let matchers = null;
|
|
187
|
+
let lastError;
|
|
188
|
+
for (const moduleId of [
|
|
189
|
+
"@testing-library/react-native/matchers",
|
|
190
|
+
"@testing-library/react-native/build/matchers",
|
|
191
|
+
"@testing-library/react-native/dist/matchers",
|
|
192
|
+
]) {
|
|
193
|
+
try {
|
|
194
|
+
matchers = req(moduleId);
|
|
195
|
+
break;
|
|
196
|
+
} catch (error) {
|
|
197
|
+
lastError = error;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
if (!matchers) throw lastError;
|
|
113
201
|
const fns = {};
|
|
114
202
|
for (const [k, v] of Object.entries(matchers || {})) {
|
|
115
203
|
if (typeof v === "function" && k !== "__esModule") fns[k] = v;
|
|
@@ -126,6 +214,9 @@ try {
|
|
|
126
214
|
}
|
|
127
215
|
}
|
|
128
216
|
|
|
217
|
+
expect.extend(animatedMatchers);
|
|
218
|
+
expect.addSnapshotSerializer(rnSerializer);
|
|
219
|
+
|
|
129
220
|
// NOTE: the cosmetic React "update to LogBoxStateSubscription not wrapped in
|
|
130
221
|
// act()" warning (which used to appear on every interaction) is fixed at the
|
|
131
222
|
// source — the native boundary stubs LogBoxNotificationContainer (the dev UI
|
|
@@ -13,6 +13,7 @@ let _preset;
|
|
|
13
13
|
let _cacheDir;
|
|
14
14
|
let _writeSeq = 0;
|
|
15
15
|
const mem = new Map();
|
|
16
|
+
const TRANSFORM_CACHE_VERSION = 2;
|
|
16
17
|
|
|
17
18
|
function init(projectRoot) {
|
|
18
19
|
if (_babel) return;
|
|
@@ -28,7 +29,11 @@ function init(projectRoot) {
|
|
|
28
29
|
);
|
|
29
30
|
}
|
|
30
31
|
const presetVersion = req("@react-native/babel-preset/package.json").version;
|
|
31
|
-
_cacheDir = path.join(
|
|
32
|
+
_cacheDir = path.join(
|
|
33
|
+
os.tmpdir(),
|
|
34
|
+
"vitest-native-cache",
|
|
35
|
+
`${presetVersion}-v${TRANSFORM_CACHE_VERSION}`,
|
|
36
|
+
);
|
|
32
37
|
fs.mkdirSync(_cacheDir, { recursive: true });
|
|
33
38
|
}
|
|
34
39
|
|
|
@@ -38,29 +43,30 @@ export function isFlow(src) {
|
|
|
38
43
|
}
|
|
39
44
|
|
|
40
45
|
/** Transform an RN source file to runnable CJS. Cached in-memory + on disk. */
|
|
41
|
-
export function transformRN(file, src, projectRoot) {
|
|
46
|
+
export function transformRN(file, src, projectRoot, platform = "ios") {
|
|
42
47
|
init(projectRoot);
|
|
43
|
-
const
|
|
48
|
+
const st = fs.statSync(file);
|
|
49
|
+
const memKey = `${platform}\0${file}\0${st.mtimeMs}\0${st.size}`;
|
|
50
|
+
const memHit = mem.get(memKey);
|
|
44
51
|
if (memHit !== undefined) return memHit;
|
|
45
52
|
|
|
46
|
-
const st = fs.statSync(file);
|
|
47
53
|
const key = crypto
|
|
48
54
|
.createHash("sha1")
|
|
49
|
-
.update(file + ":" + st.mtimeMs + ":" + st.size)
|
|
55
|
+
.update(platform + ":" + file + ":" + st.mtimeMs + ":" + st.size)
|
|
50
56
|
.digest("hex");
|
|
51
57
|
const cachePath = path.join(_cacheDir, key + ".js");
|
|
52
58
|
try {
|
|
53
59
|
const cached = fs.readFileSync(cachePath, "utf8");
|
|
54
|
-
mem.set(
|
|
60
|
+
mem.set(memKey, cached);
|
|
55
61
|
return cached;
|
|
56
62
|
} catch {}
|
|
57
63
|
|
|
58
64
|
const out = _babel.transformSync(src, {
|
|
59
65
|
filename: file,
|
|
60
|
-
presets: [_preset],
|
|
66
|
+
presets: [[_preset, { disableStaticViewConfigsCodegen: true }]],
|
|
61
67
|
babelrc: false,
|
|
62
68
|
configFile: false,
|
|
63
|
-
caller: { name: "metro", bundler: "metro", platform
|
|
69
|
+
caller: { name: "metro", bundler: "metro", platform, supportsStaticESM: false },
|
|
64
70
|
}).code;
|
|
65
71
|
// Atomic write: multiple worker threads may transform the same RN file
|
|
66
72
|
// concurrently on a cold cache. Write to a unique temp file then rename
|
|
@@ -74,6 +80,6 @@ export function transformRN(file, src, projectRoot) {
|
|
|
74
80
|
fs.rmSync(tmp, { force: true });
|
|
75
81
|
} catch {}
|
|
76
82
|
}
|
|
77
|
-
mem.set(
|
|
83
|
+
mem.set(memKey, out);
|
|
78
84
|
return out;
|
|
79
85
|
}
|
package/dist/native/worker.mjs
CHANGED
|
@@ -23,11 +23,23 @@ if (isMainThread || !parentPort) {
|
|
|
23
23
|
|
|
24
24
|
const projectRoot = process.env.VITEST_NATIVE_PROJECT_ROOT || process.cwd();
|
|
25
25
|
const diagnostics = process.env.VITEST_NATIVE_DIAGNOSTICS === "true";
|
|
26
|
+
const platform = process.env.VITEST_NATIVE_PLATFORM === "android" ? "android" : "ios";
|
|
27
|
+
const reactNativeVersion = process.env.VITEST_NATIVE_RN_VERSION || "0.0.0";
|
|
26
28
|
let transformPkgs = [];
|
|
29
|
+
let preserveGlobals = [];
|
|
30
|
+
let assetExts = [];
|
|
27
31
|
try {
|
|
28
32
|
if (process.env.VITEST_NATIVE_TRANSFORM)
|
|
29
33
|
transformPkgs = JSON.parse(process.env.VITEST_NATIVE_TRANSFORM);
|
|
30
34
|
} catch {}
|
|
35
|
+
try {
|
|
36
|
+
if (process.env.VITEST_NATIVE_ASSET_EXTS)
|
|
37
|
+
assetExts = JSON.parse(process.env.VITEST_NATIVE_ASSET_EXTS);
|
|
38
|
+
} catch {}
|
|
39
|
+
try {
|
|
40
|
+
if (process.env.VITEST_NATIVE_HOT_PRESERVE_GLOBALS)
|
|
41
|
+
preserveGlobals = JSON.parse(process.env.VITEST_NATIVE_HOT_PRESERVE_GLOBALS);
|
|
42
|
+
} catch {}
|
|
31
43
|
|
|
32
44
|
if (diagnostics) {
|
|
33
45
|
// One line per worker boot: under a hot pool, N files sharing a worker print
|
|
@@ -41,7 +53,7 @@ if (diagnostics) {
|
|
|
41
53
|
// listener tracking in reset.mjs, so RN's own boot state is preserved across
|
|
42
54
|
// per-file resets rather than wrongly torn down with test pollution.
|
|
43
55
|
installGlobals();
|
|
44
|
-
installRequireHooks(projectRoot, transformPkgs);
|
|
56
|
+
installRequireHooks(projectRoot, transformPkgs, platform, reactNativeVersion, assetExts);
|
|
45
57
|
try {
|
|
46
58
|
const req = createRequire(path.join(projectRoot, "package.json"));
|
|
47
59
|
const RN = req("react-native");
|
|
@@ -56,7 +68,6 @@ try {
|
|
|
56
68
|
"Keyboard",
|
|
57
69
|
"Linking",
|
|
58
70
|
"I18nManager",
|
|
59
|
-
"InteractionManager",
|
|
60
71
|
"PixelRatio",
|
|
61
72
|
"StyleSheet",
|
|
62
73
|
]) {
|
|
@@ -64,13 +75,19 @@ try {
|
|
|
64
75
|
void RN[name];
|
|
65
76
|
} catch {}
|
|
66
77
|
}
|
|
78
|
+
// Appearance initializes its bridge listener only when getColorScheme() is
|
|
79
|
+
// first called. Do that before listener tracking starts so the resident RN
|
|
80
|
+
// listener is treated as worker boot state instead of test-file pollution.
|
|
81
|
+
try {
|
|
82
|
+
RN.Appearance.getColorScheme?.();
|
|
83
|
+
} catch {}
|
|
67
84
|
} catch (error) {
|
|
68
85
|
throw new Error(
|
|
69
86
|
`[vitest-native] hot worker failed to preload react-native from ${projectRoot}: ${error?.message}`,
|
|
70
87
|
{ cause: error },
|
|
71
88
|
);
|
|
72
89
|
}
|
|
73
|
-
const { hotReset, bless } = installHotReset({ projectRoot, diagnostics });
|
|
90
|
+
const { hotReset, bless } = installHotReset({ projectRoot, diagnostics, preserveGlobals });
|
|
74
91
|
globalThis.__vitest_native_hot_reset = hotReset; // invoked by setup.mjs per file
|
|
75
92
|
globalThis.__vitest_native_hot_bless = bless; // invoked by runner.mjs after each import phase
|
|
76
93
|
|
|
@@ -16,15 +16,18 @@ var NativePoolWorker = class extends ThreadsPoolWorker {
|
|
|
16
16
|
environment;
|
|
17
17
|
recycleAfterFiles;
|
|
18
18
|
memoryLimit;
|
|
19
|
+
diagnostics;
|
|
19
20
|
filesRun = 0;
|
|
20
21
|
lastHeapUsed = 0;
|
|
21
22
|
memoryListenerAttached = false;
|
|
23
|
+
batchWarningShown = false;
|
|
22
24
|
constructor(options, native) {
|
|
23
25
|
super(options);
|
|
24
26
|
this.entrypoint = path.resolve(native.workerEntry);
|
|
25
27
|
this.environment = options.environment;
|
|
26
28
|
this.recycleAfterFiles = native.recycleAfterFiles ?? 0;
|
|
27
29
|
this.memoryLimit = native.memoryLimit ?? 0;
|
|
30
|
+
this.diagnostics = native.diagnostics ?? false;
|
|
28
31
|
this.reportMemory = this.memoryLimit > 0;
|
|
29
32
|
}
|
|
30
33
|
async start() {
|
|
@@ -37,7 +40,13 @@ var NativePoolWorker = class extends ThreadsPoolWorker {
|
|
|
37
40
|
}
|
|
38
41
|
}
|
|
39
42
|
send(message) {
|
|
40
|
-
if (message.type === "run" || message.type === "collect")
|
|
43
|
+
if (message.type === "run" || message.type === "collect") {
|
|
44
|
+
if (this.diagnostics && !this.batchWarningShown && message.context.files.length > 1 && (this.recycleAfterFiles > 0 || this.memoryLimit > 0)) {
|
|
45
|
+
this.batchWarningShown = true;
|
|
46
|
+
console.warn(`[vitest-native] hot worker received a ${message.context.files.length}-file Vitest task; recycling thresholds apply after the task completes. Use more than one worker when strict per-file retirement is required.`);
|
|
47
|
+
}
|
|
48
|
+
this.filesRun += message.context.files.length;
|
|
49
|
+
}
|
|
41
50
|
super.send(message);
|
|
42
51
|
}
|
|
43
52
|
canReuse(task) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const require_chunk = require("./chunk-CZWwpsFl.cjs");
|
|
2
2
|
let node_path = require("node:path");
|
|
3
|
-
node_path = require_chunk.__toESM(node_path);
|
|
3
|
+
node_path = require_chunk.__toESM(node_path, 1);
|
|
4
4
|
let vitest_node = require("vitest/node");
|
|
5
5
|
//#region src/native/pool.ts
|
|
6
6
|
function deepEqual(a, b) {
|
|
@@ -18,15 +18,18 @@ var NativePoolWorker = class extends vitest_node.ThreadsPoolWorker {
|
|
|
18
18
|
environment;
|
|
19
19
|
recycleAfterFiles;
|
|
20
20
|
memoryLimit;
|
|
21
|
+
diagnostics;
|
|
21
22
|
filesRun = 0;
|
|
22
23
|
lastHeapUsed = 0;
|
|
23
24
|
memoryListenerAttached = false;
|
|
25
|
+
batchWarningShown = false;
|
|
24
26
|
constructor(options, native) {
|
|
25
27
|
super(options);
|
|
26
28
|
this.entrypoint = node_path.default.resolve(native.workerEntry);
|
|
27
29
|
this.environment = options.environment;
|
|
28
30
|
this.recycleAfterFiles = native.recycleAfterFiles ?? 0;
|
|
29
31
|
this.memoryLimit = native.memoryLimit ?? 0;
|
|
32
|
+
this.diagnostics = native.diagnostics ?? false;
|
|
30
33
|
this.reportMemory = this.memoryLimit > 0;
|
|
31
34
|
}
|
|
32
35
|
async start() {
|
|
@@ -39,7 +42,13 @@ var NativePoolWorker = class extends vitest_node.ThreadsPoolWorker {
|
|
|
39
42
|
}
|
|
40
43
|
}
|
|
41
44
|
send(message) {
|
|
42
|
-
if (message.type === "run" || message.type === "collect")
|
|
45
|
+
if (message.type === "run" || message.type === "collect") {
|
|
46
|
+
if (this.diagnostics && !this.batchWarningShown && message.context.files.length > 1 && (this.recycleAfterFiles > 0 || this.memoryLimit > 0)) {
|
|
47
|
+
this.batchWarningShown = true;
|
|
48
|
+
console.warn(`[vitest-native] hot worker received a ${message.context.files.length}-file Vitest task; recycling thresholds apply after the task completes. Use more than one worker when strict per-file retirement is required.`);
|
|
49
|
+
}
|
|
50
|
+
this.filesRun += message.context.files.length;
|
|
51
|
+
}
|
|
43
52
|
super.send(message);
|
|
44
53
|
}
|
|
45
54
|
canReuse(task) {
|
|
@@ -18,7 +18,9 @@ const AUTO_DETECT_PRESETS = {
|
|
|
18
18
|
"react-native-device-info": "deviceInfo",
|
|
19
19
|
"react-native-mmkv": "mmkv",
|
|
20
20
|
"react-native-svg": "svg",
|
|
21
|
-
"react-native-webview": "webview"
|
|
21
|
+
"react-native-webview": "webview",
|
|
22
|
+
"@react-native-vector-icons/common": "vectorIcons",
|
|
23
|
+
"react-native-vector-icons": "vectorIcons"
|
|
22
24
|
};
|
|
23
25
|
//#endregion
|
|
24
26
|
export { AUTO_DETECT_PRESETS as t };
|
|
@@ -18,7 +18,9 @@ const AUTO_DETECT_PRESETS = {
|
|
|
18
18
|
"react-native-device-info": "deviceInfo",
|
|
19
19
|
"react-native-mmkv": "mmkv",
|
|
20
20
|
"react-native-svg": "svg",
|
|
21
|
-
"react-native-webview": "webview"
|
|
21
|
+
"react-native-webview": "webview",
|
|
22
|
+
"@react-native-vector-icons/common": "vectorIcons",
|
|
23
|
+
"react-native-vector-icons": "vectorIcons"
|
|
22
24
|
};
|
|
23
25
|
//#endregion
|
|
24
26
|
Object.defineProperty(exports, "AUTO_DETECT_PRESETS", {
|