vitest-native 0.13.0 → 0.14.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/CHANGELOG.md +339 -0
- package/README.md +138 -48
- package/dist/cli.cjs +70 -5
- package/dist/cli.d.cts +2 -3
- package/dist/cli.d.mts +2 -3
- package/dist/cli.mjs +70 -5
- package/dist/{ecosystem-Duw40-mO.mjs → ecosystem-DFbU_MPe.mjs} +389 -18
- package/dist/{ecosystem-CEIF_-jq.cjs → ecosystem-DI1AhK_k.cjs} +408 -25
- package/dist/errors.d.mts +12 -0
- package/dist/helpers.cjs +6 -3
- package/dist/helpers.d.cts +8 -9
- package/dist/helpers.d.mts +8 -9
- package/dist/helpers.mjs +6 -3
- package/dist/{index-CPGq6p95.d.cts → index-BxzW2D7K.d.cts} +33 -9
- package/dist/{index-CmETArCM.d.mts → index-CEbwa4QU.d.mts} +33 -9
- package/dist/index.d.mts +4 -4
- package/dist/index.mjs +537 -118
- package/dist/jest-compat/aliases.d.mts +5 -0
- package/dist/jest-compat/aliases.mjs +65 -0
- package/dist/jest-compat/caller.mjs +53 -0
- package/dist/jest-compat/setup.mjs +72 -36
- package/dist/jest-compat.cjs +1 -3
- package/dist/jest-compat.d.cts +4 -5
- package/dist/jest-compat.d.mts +4 -5
- package/dist/jest-compat.mjs +1 -1
- package/dist/matchers.d.cts +9 -9
- package/dist/matchers.d.mts +9 -9
- package/dist/memory-6EPHrJA1.cjs +144 -0
- package/dist/memory-FYbP0_gg.mjs +126 -0
- package/dist/native/boundary.mjs +13 -2
- package/dist/native/bounded-process.mjs +165 -0
- package/dist/native/export-condition-recovery-preload.mjs +5 -0
- package/dist/native/export-condition-recovery.mjs +195 -0
- package/dist/native/globals.mjs +133 -5
- package/dist/native/hooks.mjs +131 -71
- package/dist/native/loader.mjs +41 -28
- package/dist/native/match.d.mts +3 -0
- package/dist/native/match.mjs +38 -2
- package/dist/native/metro-profile-compiler.mjs +234 -0
- package/dist/native/metro-profile.mjs +115 -0
- package/dist/native/module-reset.mjs +111 -6
- package/dist/native/ownership.d.mts +105 -0
- package/dist/native/ownership.mjs +474 -0
- package/dist/native/registry-compiler.mjs +43 -0
- package/dist/native/registry-process.mjs +124 -0
- package/dist/native/registry.mjs +139 -32
- package/dist/native/reset.mjs +305 -135
- package/dist/native/resolve.d.mts +20 -3
- package/dist/native/resolve.mjs +32 -12
- package/dist/native/rntl-hooks.mjs +76 -0
- package/dist/native/setup.mjs +81 -32
- package/dist/native/state-manifest.d.mts +22 -0
- package/dist/native/state-manifest.mjs +136 -0
- package/dist/native/transform.mjs +29 -8
- package/dist/native/worker.mjs +52 -4
- package/dist/pool-B4yesGhI.cjs +121 -0
- package/dist/pool-DjpOjOf3.mjs +119 -0
- package/dist/{presets-CoNf4ByH.cjs → presets-BYHmi7-W.cjs} +117 -18
- package/dist/{presets-DGProTaA.mjs → presets-DC-eM5IA.mjs} +117 -18
- package/dist/presets.d.mts +1 -1
- package/dist/presets.mjs +1 -1
- package/dist/rntl-matchers.d.ts +58 -12
- package/dist/serializer.cjs +4 -2
- package/dist/serializer.d.cts +3 -4
- package/dist/serializer.d.mts +3 -4
- package/dist/serializer.mjs +4 -2
- package/dist/setup.mjs +61 -50
- package/dist/worker-vitest-BGPo0ek6.mjs +49 -0
- package/dist/worker-vitest-BfiQB8ND.cjs +62 -0
- package/docs/hot-default-design.md +175 -88
- package/docs/migrating-from-jest.md +12 -5
- package/docs/migrating-from-vitest-react-native.md +4 -4
- package/docs/release-readiness.md +39 -13
- package/docs/versioning.md +19 -18
- package/package.json +42 -31
- package/dist/pool-BU-1ecIF.cjs +0 -122
- package/dist/pool-Bcxn9a3I.mjs +0 -118
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vitest-native",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0-rc.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Run real React Native tests under Vitest. One install, zero config.",
|
|
6
6
|
"license": "MIT",
|
|
@@ -98,7 +98,11 @@
|
|
|
98
98
|
},
|
|
99
99
|
"files": [
|
|
100
100
|
"dist",
|
|
101
|
-
"docs",
|
|
101
|
+
"docs/hot-default-design.md",
|
|
102
|
+
"docs/migrating-from-jest.md",
|
|
103
|
+
"docs/migrating-from-vitest-react-native.md",
|
|
104
|
+
"docs/release-readiness.md",
|
|
105
|
+
"docs/versioning.md",
|
|
102
106
|
"LICENSE",
|
|
103
107
|
"CHANGELOG.md"
|
|
104
108
|
],
|
|
@@ -136,30 +140,30 @@
|
|
|
136
140
|
"@babel/core": "^7.29.7",
|
|
137
141
|
"@gorhom/bottom-sheet": "5.2.14",
|
|
138
142
|
"@react-native-community/netinfo": "12.0.1",
|
|
139
|
-
"@react-native-vector-icons/common": "13.0.
|
|
140
|
-
"@react-native-vector-icons/material-icons": "13.1.
|
|
141
|
-
"@react-native/babel-preset": "0.87.
|
|
142
|
-
"@react-navigation/native": "7.
|
|
143
|
-
"@react-navigation/native-stack": "7.
|
|
143
|
+
"@react-native-vector-icons/common": "13.0.3",
|
|
144
|
+
"@react-native-vector-icons/material-icons": "13.1.4",
|
|
145
|
+
"@react-native/babel-preset": "0.87.1",
|
|
146
|
+
"@react-navigation/native": "7.4.1",
|
|
147
|
+
"@react-navigation/native-stack": "7.19.2",
|
|
144
148
|
"@shopify/flash-list": "2.3.2",
|
|
145
|
-
"@testing-library/react-native": "14.0.
|
|
146
|
-
"@types/node": "25.
|
|
147
|
-
"@types/react": "19.
|
|
148
|
-
"@vitest/coverage-v8": "
|
|
149
|
-
"expo-constants": "
|
|
150
|
-
"expo-status-bar": "
|
|
149
|
+
"@testing-library/react-native": "14.0.1",
|
|
150
|
+
"@types/node": "25.9.8",
|
|
151
|
+
"@types/react": "19.3.0",
|
|
152
|
+
"@vitest/coverage-v8": "5.0.1",
|
|
153
|
+
"expo-constants": "57.0.19",
|
|
154
|
+
"expo-status-bar": "57.0.1",
|
|
151
155
|
"ext-platform-lib": "file:./tests-native/fixtures/ext-platform-lib",
|
|
152
156
|
"ext-rn-named-lib": "file:./tests-native/fixtures/ext-rn-named-lib",
|
|
153
157
|
"generic-mainfield-lib": "file:./tests-native/fixtures/generic-mainfield-lib",
|
|
154
|
-
"metro-resolver": "0.87.
|
|
155
|
-
"react": "^19.
|
|
158
|
+
"metro-resolver": "0.87.1",
|
|
159
|
+
"react": "^19.3.0",
|
|
156
160
|
"react-native": "0.87.0",
|
|
157
|
-
"react-native-gesture-handler": "3.
|
|
158
|
-
"react-native-keyboard-controller": "1.22.
|
|
159
|
-
"react-native-reanimated": "4.
|
|
160
|
-
"react-native-safe-area-context": "5.
|
|
161
|
-
"react-native-worklets": "0.
|
|
162
|
-
"react-test-renderer": "^19.
|
|
161
|
+
"react-native-gesture-handler": "3.3.0",
|
|
162
|
+
"react-native-keyboard-controller": "1.22.5",
|
|
163
|
+
"react-native-reanimated": "4.7.0",
|
|
164
|
+
"react-native-safe-area-context": "5.10.0",
|
|
165
|
+
"react-native-worklets": "0.13.0",
|
|
166
|
+
"react-test-renderer": "^19.3.0",
|
|
163
167
|
"resident-singleton": "file:./tests-native/fixtures/resident-singleton",
|
|
164
168
|
"rn-condition-cjs-lib": "file:./tests-native/fixtures/rn-condition-cjs-lib",
|
|
165
169
|
"rn-condition-lib": "file:./tests-native/fixtures/rn-condition-lib",
|
|
@@ -167,12 +171,13 @@
|
|
|
167
171
|
"rn-mainfield-lib": "file:./tests-native/fixtures/rn-mainfield-lib",
|
|
168
172
|
"rn-platform-flow-lib": "file:./tests-native/fixtures/rn-platform-flow-lib",
|
|
169
173
|
"rn-singleton-lib": "file:./tests-native/fixtures/rn-singleton-lib",
|
|
170
|
-
"test-renderer": "^1.
|
|
171
|
-
"tsdown": "0.
|
|
172
|
-
"typescript": "
|
|
174
|
+
"test-renderer": "^1.3.0",
|
|
175
|
+
"tsdown": "0.23.0",
|
|
176
|
+
"typescript": "7.0.2",
|
|
177
|
+
"typescript-6": "npm:typescript@6.0.3",
|
|
173
178
|
"untranspiled-jsx-lib": "file:./tests-native/fixtures/untranspiled-jsx-lib",
|
|
174
|
-
"vite": "^8.
|
|
175
|
-
"vitest": "
|
|
179
|
+
"vite": "^8.3.0",
|
|
180
|
+
"vitest": "5.0.1"
|
|
176
181
|
},
|
|
177
182
|
"scripts": {
|
|
178
183
|
"build": "bunx --bun tsdown && node scripts/copy-static-types.mjs",
|
|
@@ -183,7 +188,8 @@
|
|
|
183
188
|
"test:native:android": "vitest run --config tests-native/vitest.android.config.mts",
|
|
184
189
|
"test:native:advice": "vitest run --config tests-native/vitest.advice.config.mts",
|
|
185
190
|
"test:native:hot": "vitest run --config tests-native/vitest.hot.config.mts",
|
|
186
|
-
"test:native:hot:isolation": "vitest run --config tests-native/vitest.hot-isolation.config.mts",
|
|
191
|
+
"test:native:hot:isolation": "vitest run --config tests-native/vitest.hot-isolation.config.mts && vitest run --config tests-native/vitest.hot-user-setup.config.mts && vitest run --config tests-native/vitest.hot-jest-compat.config.mts --mode stock && vitest run --config tests-native/vitest.hot-jest-compat.config.mts --mode hot",
|
|
192
|
+
"test:native:hot:state-manifest": "node scripts/test-hot-state-manifest.mjs",
|
|
187
193
|
"test:native:hot:soak": "node scripts/test-hot-soak.mjs",
|
|
188
194
|
"test:consumers": "node scripts/test-consumers.mjs",
|
|
189
195
|
"lint": "oxlint --deny-warnings src/ tests/ tests-native/ crosscheck/ consumer-tests/ scripts/",
|
|
@@ -194,15 +200,20 @@
|
|
|
194
200
|
"check:exports": "node scripts/check-exports.mjs",
|
|
195
201
|
"typecheck": "tsc --noEmit",
|
|
196
202
|
"clean": "rm -rf dist",
|
|
197
|
-
"prepublishOnly": "bun run build && bun run check:size && bun run test && bun run test:native && bun run test:native:forks && bun run test:native:navigation && bun run test:native:android && bun run test:native:advice && bun run test:native:hot && bun run test:native:hot:isolation && bun run test:native:hot:soak && bun run validate:hot-parity && bun run fidelity:check && bun run test:consumers && bun run lint && bun run typecheck",
|
|
203
|
+
"prepublishOnly": "bun run build && bun run check:size && bun run test && bun run test:native && bun run test:native:forks && bun run test:native:navigation && bun run test:native:android && bun run test:native:advice && bun run test:native:hot && bun run test:native:hot:isolation && bun run test:native:hot:state-manifest && bun run test:native:hot:soak && bun run validate:hot-parity && bun run validate:registry-process && bun run fidelity:check && bun run test:consumers && bun run lint && bun run typecheck",
|
|
198
204
|
"crosscheck": "node scripts/crosscheck.mjs",
|
|
199
205
|
"fidelity:report": "node scripts/fidelity-report.mjs",
|
|
200
206
|
"fidelity:check": "node scripts/fidelity-report.mjs --check && node scripts/fidelity-matrix.mjs --check",
|
|
201
|
-
"validate:hot-parity": "node validation/idiomatic/scale/run-compare.mjs"
|
|
207
|
+
"validate:hot-parity": "node validation/idiomatic/scale/run-compare.mjs",
|
|
208
|
+
"validate:coverage-parity": "node validation/module-isolation/run-coverage.mjs",
|
|
209
|
+
"validate:hot-auto": "node validation/hot-auto/run.mjs",
|
|
210
|
+
"validate:vitest-semantics": "node validation/vitest-semantics/run.mjs",
|
|
211
|
+
"validate:registry-process": "node validation/module-isolation/run-registry-process.mjs",
|
|
212
|
+
"validate:metro-profile": "node --expose-gc validation/module-isolation/run-metro-profile.mjs"
|
|
202
213
|
},
|
|
203
214
|
"dependencies": {
|
|
204
|
-
"flow-remove-types": "^2.
|
|
205
|
-
"magic-string": "^1.
|
|
215
|
+
"flow-remove-types": "^2.332.0",
|
|
216
|
+
"magic-string": "^1.4.2"
|
|
206
217
|
},
|
|
207
218
|
"bin": {
|
|
208
219
|
"vitest-native": "./dist/cli.mjs"
|
package/dist/pool-BU-1ecIF.cjs
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
const require_rolldown_runtime = require("./rolldown-runtime-C0BPl7ul.cjs");
|
|
2
|
-
const require_errors = require("./errors-B3lJK46O.cjs");
|
|
3
|
-
let node_path = require("node:path");
|
|
4
|
-
node_path = require_rolldown_runtime.__toESM(node_path, 1);
|
|
5
|
-
let node_module = require("node:module");
|
|
6
|
-
let node_os = require("node:os");
|
|
7
|
-
node_os = require_rolldown_runtime.__toESM(node_os, 1);
|
|
8
|
-
let vitest_node = require("vitest/node");
|
|
9
|
-
//#region src/native/pool.ts
|
|
10
|
-
function deepEqual(a, b) {
|
|
11
|
-
if (a === b) return true;
|
|
12
|
-
if (a == null || b == null || typeof a !== "object" || typeof b !== "object") return false;
|
|
13
|
-
const ka = Object.keys(a);
|
|
14
|
-
const kb = Object.keys(b);
|
|
15
|
-
if (ka.length !== kb.length) return false;
|
|
16
|
-
return ka.every((k) => kb.includes(k) && deepEqual(a[k], b[k]));
|
|
17
|
-
}
|
|
18
|
-
var NativePoolWorker = class extends vitest_node.ThreadsPoolWorker {
|
|
19
|
-
name = "vitest-native";
|
|
20
|
-
reportMemory;
|
|
21
|
-
entrypoint;
|
|
22
|
-
environment;
|
|
23
|
-
recycleAfterFiles;
|
|
24
|
-
memoryLimit;
|
|
25
|
-
diagnostics;
|
|
26
|
-
filesRun = 0;
|
|
27
|
-
lastHeapUsed = 0;
|
|
28
|
-
memoryListenerAttached = false;
|
|
29
|
-
batchWarningShown = false;
|
|
30
|
-
constructor(options, native) {
|
|
31
|
-
super(options);
|
|
32
|
-
this.entrypoint = node_path.default.resolve(native.workerEntry);
|
|
33
|
-
this.environment = options.environment;
|
|
34
|
-
this.recycleAfterFiles = native.recycleAfterFiles ?? 0;
|
|
35
|
-
this.memoryLimit = native.memoryLimit ?? 0;
|
|
36
|
-
this.diagnostics = native.diagnostics ?? false;
|
|
37
|
-
this.reportMemory = this.memoryLimit > 0;
|
|
38
|
-
}
|
|
39
|
-
async start() {
|
|
40
|
-
await super.start();
|
|
41
|
-
if (this.reportMemory && !this.memoryListenerAttached) {
|
|
42
|
-
this.memoryListenerAttached = true;
|
|
43
|
-
this.on("message", (message) => {
|
|
44
|
-
if (message?.__vitest_worker_response__ && typeof message.usedMemory === "number") this.lastHeapUsed = message.usedMemory;
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
send(message) {
|
|
49
|
-
if (message.type === "run" || message.type === "collect") {
|
|
50
|
-
if (!this.batchWarningShown && message.context.files.length > 1 && (this.recycleAfterFiles > 0 || this.memoryLimit > 0)) {
|
|
51
|
-
this.batchWarningShown = true;
|
|
52
|
-
console.warn(`[vitest-native] hotRuntime recycling (memoryLimit/recycleAfterFiles) is INACTIVE here: Vitest batched ${message.context.files.length} files into one task, which happens in single-worker mode (maxWorkers: 1) — so a worker can never be retired between files and the memory bound cannot be enforced. Run with maxWorkers >= 2 (or remove maxWorkers/fileParallelism: false) so each file is its own task and recycling can fire.`);
|
|
53
|
-
}
|
|
54
|
-
this.filesRun += message.context.files.length;
|
|
55
|
-
}
|
|
56
|
-
super.send(message);
|
|
57
|
-
}
|
|
58
|
-
canReuse(task) {
|
|
59
|
-
if (this.recycleAfterFiles > 0 && this.filesRun >= this.recycleAfterFiles) return false;
|
|
60
|
-
if (this.memoryLimit > 0 && this.lastHeapUsed >= this.memoryLimit) return false;
|
|
61
|
-
const env = task.context.environment;
|
|
62
|
-
return env.name === this.environment.name && deepEqual(env.options, this.environment.options);
|
|
63
|
-
}
|
|
64
|
-
};
|
|
65
|
-
const HOT_MEMORY_MIN_BYTES = 805306368;
|
|
66
|
-
const HOT_MEMORY_MAX_BYTES = 1610612736;
|
|
67
|
-
const HOT_MEMORY_FRACTION = .25;
|
|
68
|
-
/**
|
|
69
|
-
* Default per-worker hot `memoryLimit` (bytes): `clamp(totalmem * 0.25, 768MB, 1.5GB)`.
|
|
70
|
-
* Applied only when hot is enabled and neither `memoryLimit` nor
|
|
71
|
-
* `recycleAfterFiles` was set explicitly. `totalmem` is injectable for tests.
|
|
72
|
-
*/
|
|
73
|
-
function defaultHotMemoryLimit(totalmem = node_os.default.totalmem()) {
|
|
74
|
-
const budget = Math.floor(totalmem * HOT_MEMORY_FRACTION);
|
|
75
|
-
return Math.min(HOT_MEMORY_MAX_BYTES, Math.max(HOT_MEMORY_MIN_BYTES, budget));
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* Fail fast when the hot worker would load a different Vitest VERSION from the
|
|
79
|
-
* project's.
|
|
80
|
-
*
|
|
81
|
-
* The worker entry ships inside this package, so its `import 'vitest/worker'`
|
|
82
|
-
* resolves from THIS package's location rather than the project's. Where a monorepo
|
|
83
|
-
* has more than one Vitest install — a linked package, a hoisted `node_modules`,
|
|
84
|
-
* mixed versions across workspaces — the two can differ, and a version difference is
|
|
85
|
-
* invisible at runtime: the start handshake succeeds, the run request is accepted,
|
|
86
|
-
* and no result is ever reported. Vitest then prints "No test files found" with no
|
|
87
|
-
* error at all, and on some paths still exits 0 — a green run that tested nothing.
|
|
88
|
-
*
|
|
89
|
-
* Only a VERSION difference is an error. Two installs of the same version are two
|
|
90
|
-
* module registries but identical code, and they talk to each other perfectly well;
|
|
91
|
-
* failing on the paths alone would block working monorepos where the same version is
|
|
92
|
-
* simply installed twice.
|
|
93
|
-
*/
|
|
94
|
-
function assertWorkerVitestMatchesProject(workerEntry, projectRoot) {
|
|
95
|
-
const resolveVitest = (from) => {
|
|
96
|
-
try {
|
|
97
|
-
const require_ = (0, node_module.createRequire)(from);
|
|
98
|
-
const pkgPath = require_.resolve("vitest/package.json");
|
|
99
|
-
return {
|
|
100
|
-
path: pkgPath,
|
|
101
|
-
version: require_(pkgPath).version
|
|
102
|
-
};
|
|
103
|
-
} catch {
|
|
104
|
-
return null;
|
|
105
|
-
}
|
|
106
|
-
};
|
|
107
|
-
const worker = resolveVitest(workerEntry);
|
|
108
|
-
const project = resolveVitest(node_path.default.join(projectRoot, "package.json"));
|
|
109
|
-
if (worker === null || project === null || worker.version === project.version) return;
|
|
110
|
-
throw new require_errors.VitestNativeError("HOT_RUNTIME_UNAVAILABLE", `'hotRuntime' cannot run: its worker would load vitest@${worker.version}, but this run is driven by vitest@${project.version}. They talk over Vitest's worker protocol, and a version mismatch reports no results at all rather than failing.\n worker would load: ${worker.path}\n project resolves: ${project.path}\nThis happens when vitest-native and vitest resolve to different node_modules trees (linked packages, hoisted monorepo installs, mixed Vitest versions). Install one Vitest version reachable from vitest-native, or set 'hotRuntime: false'.`);
|
|
111
|
-
}
|
|
112
|
-
/** Pool initializer for `test.pool` — keeps RN-hot workers alive across files. */
|
|
113
|
-
function nativePool(options) {
|
|
114
|
-
assertWorkerVitestMatchesProject(node_path.default.resolve(options.workerEntry), options.projectRoot);
|
|
115
|
-
return {
|
|
116
|
-
name: "vitest-native",
|
|
117
|
-
createPoolWorker: (poolOptions) => new NativePoolWorker(poolOptions, options)
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
//#endregion
|
|
121
|
-
exports.defaultHotMemoryLimit = defaultHotMemoryLimit;
|
|
122
|
-
exports.nativePool = nativePool;
|
package/dist/pool-Bcxn9a3I.mjs
DELETED
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
import { t as VitestNativeError } from "./errors-CxoBUfVJ.mjs";
|
|
2
|
-
import { createRequire } from "node:module";
|
|
3
|
-
import path from "node:path";
|
|
4
|
-
import os from "node:os";
|
|
5
|
-
import { ThreadsPoolWorker } from "vitest/node";
|
|
6
|
-
//#region src/native/pool.ts
|
|
7
|
-
function deepEqual(a, b) {
|
|
8
|
-
if (a === b) return true;
|
|
9
|
-
if (a == null || b == null || typeof a !== "object" || typeof b !== "object") return false;
|
|
10
|
-
const ka = Object.keys(a);
|
|
11
|
-
const kb = Object.keys(b);
|
|
12
|
-
if (ka.length !== kb.length) return false;
|
|
13
|
-
return ka.every((k) => kb.includes(k) && deepEqual(a[k], b[k]));
|
|
14
|
-
}
|
|
15
|
-
var NativePoolWorker = class extends ThreadsPoolWorker {
|
|
16
|
-
name = "vitest-native";
|
|
17
|
-
reportMemory;
|
|
18
|
-
entrypoint;
|
|
19
|
-
environment;
|
|
20
|
-
recycleAfterFiles;
|
|
21
|
-
memoryLimit;
|
|
22
|
-
diagnostics;
|
|
23
|
-
filesRun = 0;
|
|
24
|
-
lastHeapUsed = 0;
|
|
25
|
-
memoryListenerAttached = false;
|
|
26
|
-
batchWarningShown = false;
|
|
27
|
-
constructor(options, native) {
|
|
28
|
-
super(options);
|
|
29
|
-
this.entrypoint = path.resolve(native.workerEntry);
|
|
30
|
-
this.environment = options.environment;
|
|
31
|
-
this.recycleAfterFiles = native.recycleAfterFiles ?? 0;
|
|
32
|
-
this.memoryLimit = native.memoryLimit ?? 0;
|
|
33
|
-
this.diagnostics = native.diagnostics ?? false;
|
|
34
|
-
this.reportMemory = this.memoryLimit > 0;
|
|
35
|
-
}
|
|
36
|
-
async start() {
|
|
37
|
-
await super.start();
|
|
38
|
-
if (this.reportMemory && !this.memoryListenerAttached) {
|
|
39
|
-
this.memoryListenerAttached = true;
|
|
40
|
-
this.on("message", (message) => {
|
|
41
|
-
if (message?.__vitest_worker_response__ && typeof message.usedMemory === "number") this.lastHeapUsed = message.usedMemory;
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
send(message) {
|
|
46
|
-
if (message.type === "run" || message.type === "collect") {
|
|
47
|
-
if (!this.batchWarningShown && message.context.files.length > 1 && (this.recycleAfterFiles > 0 || this.memoryLimit > 0)) {
|
|
48
|
-
this.batchWarningShown = true;
|
|
49
|
-
console.warn(`[vitest-native] hotRuntime recycling (memoryLimit/recycleAfterFiles) is INACTIVE here: Vitest batched ${message.context.files.length} files into one task, which happens in single-worker mode (maxWorkers: 1) — so a worker can never be retired between files and the memory bound cannot be enforced. Run with maxWorkers >= 2 (or remove maxWorkers/fileParallelism: false) so each file is its own task and recycling can fire.`);
|
|
50
|
-
}
|
|
51
|
-
this.filesRun += message.context.files.length;
|
|
52
|
-
}
|
|
53
|
-
super.send(message);
|
|
54
|
-
}
|
|
55
|
-
canReuse(task) {
|
|
56
|
-
if (this.recycleAfterFiles > 0 && this.filesRun >= this.recycleAfterFiles) return false;
|
|
57
|
-
if (this.memoryLimit > 0 && this.lastHeapUsed >= this.memoryLimit) return false;
|
|
58
|
-
const env = task.context.environment;
|
|
59
|
-
return env.name === this.environment.name && deepEqual(env.options, this.environment.options);
|
|
60
|
-
}
|
|
61
|
-
};
|
|
62
|
-
const HOT_MEMORY_MIN_BYTES = 805306368;
|
|
63
|
-
const HOT_MEMORY_MAX_BYTES = 1610612736;
|
|
64
|
-
const HOT_MEMORY_FRACTION = .25;
|
|
65
|
-
/**
|
|
66
|
-
* Default per-worker hot `memoryLimit` (bytes): `clamp(totalmem * 0.25, 768MB, 1.5GB)`.
|
|
67
|
-
* Applied only when hot is enabled and neither `memoryLimit` nor
|
|
68
|
-
* `recycleAfterFiles` was set explicitly. `totalmem` is injectable for tests.
|
|
69
|
-
*/
|
|
70
|
-
function defaultHotMemoryLimit(totalmem = os.totalmem()) {
|
|
71
|
-
const budget = Math.floor(totalmem * HOT_MEMORY_FRACTION);
|
|
72
|
-
return Math.min(HOT_MEMORY_MAX_BYTES, Math.max(HOT_MEMORY_MIN_BYTES, budget));
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Fail fast when the hot worker would load a different Vitest VERSION from the
|
|
76
|
-
* project's.
|
|
77
|
-
*
|
|
78
|
-
* The worker entry ships inside this package, so its `import 'vitest/worker'`
|
|
79
|
-
* resolves from THIS package's location rather than the project's. Where a monorepo
|
|
80
|
-
* has more than one Vitest install — a linked package, a hoisted `node_modules`,
|
|
81
|
-
* mixed versions across workspaces — the two can differ, and a version difference is
|
|
82
|
-
* invisible at runtime: the start handshake succeeds, the run request is accepted,
|
|
83
|
-
* and no result is ever reported. Vitest then prints "No test files found" with no
|
|
84
|
-
* error at all, and on some paths still exits 0 — a green run that tested nothing.
|
|
85
|
-
*
|
|
86
|
-
* Only a VERSION difference is an error. Two installs of the same version are two
|
|
87
|
-
* module registries but identical code, and they talk to each other perfectly well;
|
|
88
|
-
* failing on the paths alone would block working monorepos where the same version is
|
|
89
|
-
* simply installed twice.
|
|
90
|
-
*/
|
|
91
|
-
function assertWorkerVitestMatchesProject(workerEntry, projectRoot) {
|
|
92
|
-
const resolveVitest = (from) => {
|
|
93
|
-
try {
|
|
94
|
-
const require_ = createRequire(from);
|
|
95
|
-
const pkgPath = require_.resolve("vitest/package.json");
|
|
96
|
-
return {
|
|
97
|
-
path: pkgPath,
|
|
98
|
-
version: require_(pkgPath).version
|
|
99
|
-
};
|
|
100
|
-
} catch {
|
|
101
|
-
return null;
|
|
102
|
-
}
|
|
103
|
-
};
|
|
104
|
-
const worker = resolveVitest(workerEntry);
|
|
105
|
-
const project = resolveVitest(path.join(projectRoot, "package.json"));
|
|
106
|
-
if (worker === null || project === null || worker.version === project.version) return;
|
|
107
|
-
throw new VitestNativeError("HOT_RUNTIME_UNAVAILABLE", `'hotRuntime' cannot run: its worker would load vitest@${worker.version}, but this run is driven by vitest@${project.version}. They talk over Vitest's worker protocol, and a version mismatch reports no results at all rather than failing.\n worker would load: ${worker.path}\n project resolves: ${project.path}\nThis happens when vitest-native and vitest resolve to different node_modules trees (linked packages, hoisted monorepo installs, mixed Vitest versions). Install one Vitest version reachable from vitest-native, or set 'hotRuntime: false'.`);
|
|
108
|
-
}
|
|
109
|
-
/** Pool initializer for `test.pool` — keeps RN-hot workers alive across files. */
|
|
110
|
-
function nativePool(options) {
|
|
111
|
-
assertWorkerVitestMatchesProject(path.resolve(options.workerEntry), options.projectRoot);
|
|
112
|
-
return {
|
|
113
|
-
name: "vitest-native",
|
|
114
|
-
createPoolWorker: (poolOptions) => new NativePoolWorker(poolOptions, options)
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
//#endregion
|
|
118
|
-
export { defaultHotMemoryLimit, nativePool };
|