vitest-native 0.14.0-rc.9 → 0.14.1
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 +94 -961
- package/dist/cli.d.mts +1 -3
- package/dist/cli.mjs +286 -97
- package/dist/context.d.mts +45 -0
- package/dist/context.mjs +243 -0
- package/dist/errors.d.mts +3 -0
- package/dist/helpers.cjs +53 -4
- package/dist/helpers.d.cts +1 -3
- package/dist/helpers.d.mts +1 -3
- package/dist/helpers.mjs +2 -3
- package/dist/host-BAAAlSkO.mjs +1028 -0
- package/dist/{index-CzQazgp9.d.cts → index-aNBCTfA5.d.mts} +35 -55
- package/dist/index.d.mts +1 -3
- package/dist/index.mjs +463 -595
- package/dist/jest-compat/error-equality.mjs +127 -0
- package/dist/jest-compat/jest-object.mjs +149 -4
- package/dist/jest-compat/node-registry.mjs +233 -58
- package/dist/jest-compat/setup.mjs +28 -4
- package/dist/jest-compat.cjs +227 -52
- package/dist/jest-compat.d.cts +5 -6
- package/dist/jest-compat.d.mts +5 -6
- package/dist/jest-compat.mjs +204 -52
- package/dist/matchers.cjs +1 -2
- package/dist/matchers.d.cts +0 -2
- package/dist/matchers.d.mts +0 -2
- package/dist/matchers.mjs +1 -2
- package/dist/{memory-FYbP0_gg.mjs → memory-Couw0Esx.mjs} +31 -6
- package/dist/native/assets.d.mts +2 -0
- package/dist/native/assets.mjs +34 -0
- package/dist/native/babel-toolchain.mjs +190 -0
- package/dist/native/babel-worker.mjs +47 -0
- package/dist/native/boundary.mjs +23 -8
- package/dist/native/compile-cache.mjs +21 -9
- package/dist/native/coverage-v8.mjs +287 -0
- package/dist/native/esm-interop.mjs +216 -0
- package/dist/native/expo-native-modules.mjs +7 -1
- package/dist/native/globals.mjs +104 -6
- package/dist/native/hooks.mjs +278 -61
- package/dist/native/loader.mjs +390 -23
- package/dist/native/match.d.mts +24 -2
- package/dist/native/match.mjs +117 -12
- package/dist/native/metro-profile-compiler.mjs +2 -31
- package/dist/native/module-reset.mjs +37 -5
- package/dist/native/native-modules.d.mts +2 -3
- package/dist/native/native-modules.mjs +22 -21
- package/dist/native/ownership.d.mts +10 -0
- package/dist/native/ownership.mjs +96 -9
- package/dist/native/registry-compiler.mjs +6 -0
- package/dist/native/registry.mjs +138 -119
- package/dist/native/resident-health.mjs +133 -0
- package/dist/native/resolve.d.mts +6 -0
- package/dist/native/resolve.mjs +119 -9
- package/dist/native/rntl-hooks.mjs +157 -6
- package/dist/native/rntl-text-host.mjs +64 -0
- package/dist/native/runner.mjs +73 -0
- package/dist/native/setup.mjs +96 -70
- package/dist/native/state-manifest.mjs +14 -8
- package/dist/native/transform.d.mts +2 -0
- package/dist/native/transform.mjs +476 -59
- package/dist/native/worker.mjs +65 -36
- package/dist/{pool-DjpOjOf3.mjs → pool-CFSpNfyw.mjs} +37 -11
- package/dist/{preset-map-BlBriTA0.mjs → preset-map-CogjYmSw.mjs} +4 -19
- package/dist/preset-modules.mjs +2 -11
- package/dist/{presets--zrEWQC2.mjs → presets-CXDyg51l.mjs} +65 -82
- package/dist/presets.d.mts +1 -1
- package/dist/presets.mjs +1 -1
- package/dist/report-error.d.mts +20 -0
- package/dist/report-error.mjs +145 -0
- package/dist/rntl-support.d.mts +12 -0
- package/dist/rntl-support.mjs +189 -0
- package/dist/{rolldown-runtime-BhDjJH2R.mjs → rolldown-runtime-D70C1uOS.mjs} +1 -2
- package/dist/serializer.cjs +1 -2
- package/dist/serializer.d.cts +1 -3
- package/dist/serializer.d.mts +1 -3
- package/dist/serializer.mjs +1 -2
- package/dist/setup.mjs +95 -156
- package/dist/{worker-vitest-BGPo0ek6.mjs → worker-vitest-CILPFeRX.mjs} +2 -3
- package/docs/migrating-from-jest.md +82 -16
- package/docs/release-readiness.md +1 -1
- package/package.json +119 -102
- package/CHANGELOG.md +0 -2491
- package/dist/cli.cjs +0 -1951
- package/dist/cli.d.cts +0 -3
- package/dist/ecosystem-D6e1OVOg.mjs +0 -1019
- package/dist/ecosystem-MFwkbnaC.cjs +0 -1099
- package/dist/errors-B3lJK46O.cjs +0 -75
- package/dist/errors-CxoBUfVJ.mjs +0 -64
- package/dist/index-xw43lBlv.d.mts +0 -481
- package/dist/memory-6EPHrJA1.cjs +0 -144
- package/dist/pool-B4yesGhI.cjs +0 -121
- package/dist/preset-map-jSFsNy1Q.cjs +0 -241
- package/dist/preset-modules-BoLkaeEN.cjs +0 -53
- package/dist/preset-modules-CG8-G82P.mjs +0 -36
- package/dist/presets-9f83H0WW.cjs +0 -3939
- package/dist/rolldown-runtime-C0BPl7ul.cjs +0 -43
- package/dist/worker-vitest-BfiQB8ND.cjs +0 -62
- package/docs/hot-default-design.md +0 -216
package/dist/cli.mjs
CHANGED
|
@@ -1,28 +1,15 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
3
|
-
import { i as autoDetectedPresetApplies, n as DEFAULT_ASSET_EXTS, o as presetShadowing, r as PRESET_MODULES, s as testsItself, t as AUTO_DETECT_PRESETS } from "./preset-map-
|
|
4
|
-
import { n as formatHotMemoryPlan, t as createHotMemoryPlan } from "./memory-
|
|
2
|
+
import { c as detectEcosystemPackages, d as fsModuleCacheOnByDefault, g as unsupportedExpoSdkMessage, h as installedExpoVersion, i as ownVitestPackage, l as detectEngine, o as usesVitePlus, p as PEER_REQUIREMENTS, r as installedViteVersion, v as validatePeerDependency } from "./host-BAAAlSkO.mjs";
|
|
3
|
+
import { i as autoDetectedPresetApplies, n as DEFAULT_ASSET_EXTS, o as presetShadowing, r as PRESET_MODULES, s as testsItself, t as AUTO_DETECT_PRESETS } from "./preset-map-CogjYmSw.mjs";
|
|
4
|
+
import { n as formatHotMemoryPlan, t as createHotMemoryPlan } from "./memory-Couw0Esx.mjs";
|
|
5
5
|
import { createRequire } from "node:module";
|
|
6
|
-
import
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
7
7
|
import path from "node:path";
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
"react-native.dimensions",
|
|
14
|
-
"react-native.appearance",
|
|
15
|
-
"react-native.event-listeners",
|
|
16
|
-
"process.env",
|
|
17
|
-
"process.listeners",
|
|
18
|
-
"global.descriptors",
|
|
19
|
-
"console.descriptors",
|
|
20
|
-
"react-native.error-utils",
|
|
21
|
-
"expo.runtime",
|
|
22
|
-
"jest-compat.node-mocks"
|
|
23
|
-
]);
|
|
24
|
-
//#endregion
|
|
25
|
-
//#region src/cli/manifest.ts
|
|
8
|
+
import fs from "node:fs";
|
|
9
|
+
import { createNativeOwnershipPolicy, formatNativeOwnershipManifest } from "./native/ownership.mjs";
|
|
10
|
+
import { resolveTransformPreset } from "./native/babel-preset.mjs";
|
|
11
|
+
import { HOT_STATE_MANIFEST_ENTRIES } from "./native/state-manifest.mjs";
|
|
12
|
+
// src/cli/manifest.ts
|
|
26
13
|
/**
|
|
27
14
|
* Installed-package facts for the CLI, read from disk.
|
|
28
15
|
*
|
|
@@ -72,8 +59,7 @@ function activePresets(root, disabled = [], engine = "native") {
|
|
|
72
59
|
}
|
|
73
60
|
return [...active];
|
|
74
61
|
}
|
|
75
|
-
|
|
76
|
-
//#region src/cli/babel-config.ts
|
|
62
|
+
// src/cli/babel-config.ts
|
|
77
63
|
/**
|
|
78
64
|
* The project's Babel configuration, as `migrate` and `doctor` need to see it.
|
|
79
65
|
*
|
|
@@ -99,18 +85,18 @@ const RELATIVE_FILES = [
|
|
|
99
85
|
".babelrc.json"
|
|
100
86
|
];
|
|
101
87
|
/**
|
|
102
|
-
* @babel/core's `standardizeName
|
|
103
|
-
*
|
|
88
|
+
* @babel/core's `standardizeName` (lib/config/files/plugins.js), rule for rule except that a name
|
|
89
|
+
* starting with `.` is taken as a relative path: `foo` →
|
|
90
|
+
* `babel-plugin-foo`, `@babel/foo` → `@babel/plugin-foo` (`@babel/plugin-foo` unchanged),
|
|
91
|
+
* `@scope/foo` → `@scope/babel-plugin-foo`, `@scope` → `@scope/babel-plugin`, `module:foo` →
|
|
92
|
+
* `foo`. A relative or absolute path is returned as written.
|
|
104
93
|
*/
|
|
105
94
|
function normalizeBabelName(name, kind = "plugin") {
|
|
106
|
-
const prefix = `babel-${kind}`;
|
|
107
|
-
if (name.startsWith("module:")) return name.slice(7);
|
|
108
95
|
if (name.startsWith(".") || path.isAbsolute(name)) return name;
|
|
109
|
-
|
|
110
|
-
const
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
return `${prefix}-${name}`;
|
|
96
|
+
const prefix = new RegExp(`^(?!@|module:|[^/]+/|babel-${kind}-)`);
|
|
97
|
+
const babelOrg = new RegExp(`^(@babel/)(?!${kind}-|[^/]+/)`);
|
|
98
|
+
const otherOrg = new RegExp(`^(@(?!babel/)[^/]+/)(?![^/]*babel-${kind}(?:-|/|$)|[^/]+/)`);
|
|
99
|
+
return name.replace(prefix, `babel-${kind}-`).replace(babelOrg, `$1${kind}-`).replace(otherOrg, `$1babel-${kind}-`).replace(/^(@(?!babel$)[^/]+)$/, `$1/babel-${kind}`).replace(/^module:/, "");
|
|
114
100
|
}
|
|
115
101
|
function entryOf(item, kind) {
|
|
116
102
|
const [target, options] = Array.isArray(item) ? item : [item, void 0];
|
|
@@ -176,6 +162,54 @@ function entriesOf(config, key) {
|
|
|
176
162
|
...array(config.overrides)
|
|
177
163
|
].flatMap((s) => array(s?.[key]));
|
|
178
164
|
}
|
|
165
|
+
/** Truthy in the sense Babel's `lazy` option is: `true`, a list of names, or a predicate. */
|
|
166
|
+
const enablesLazy = (value) => value === true || typeof value === "function" || Array.isArray(value) && value.length > 0;
|
|
167
|
+
/** The option bags babel-preset-expo merges for a native platform (its getOptions). */
|
|
168
|
+
function expoNativeOptions(options) {
|
|
169
|
+
if (!options || typeof options !== "object") return [];
|
|
170
|
+
const o = options;
|
|
171
|
+
return [
|
|
172
|
+
o,
|
|
173
|
+
o.native,
|
|
174
|
+
o.ios,
|
|
175
|
+
o.android
|
|
176
|
+
].filter((bag) => !!bag && typeof bag === "object");
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Settings that make babel-jest compile `import` into a `require()` run on first use, so a
|
|
180
|
+
* module a test never touches is never evaluated under Jest. Under vitest-native project
|
|
181
|
+
* source is compiled by Vite and its imports are evaluated when the file loads.
|
|
182
|
+
*
|
|
183
|
+
* - babel-preset-expo `lazyImports` (top level or under `native`/`ios`/`android`), passed on as
|
|
184
|
+
* - @react-native/babel-preset `lazyImportExportTransform`, which sets
|
|
185
|
+
* - @babel/plugin-transform-modules-commonjs `lazy`.
|
|
186
|
+
* - An inline-requires plugin (babel-preset-fbjs's, Metro's), which does the same for `require`.
|
|
187
|
+
*
|
|
188
|
+
* With no setting, @react-native/babel-preset makes only a fixed list of React Native's own
|
|
189
|
+
* modules lazy, which tests do not notice, so that is not reported.
|
|
190
|
+
*/
|
|
191
|
+
function lazyImportSettings(presets, plugins) {
|
|
192
|
+
const found = [];
|
|
193
|
+
for (const preset of presets) if (preset.name === "babel-preset-expo") {
|
|
194
|
+
if (expoNativeOptions(preset.options).some((bag) => enablesLazy(bag.lazyImports))) found.push(`${preset.written} lazyImports`);
|
|
195
|
+
} else if (/^(@react-native\/babel-preset|metro-react-native-babel-preset)$/.test(preset.name)) {
|
|
196
|
+
const options = preset.options ?? {};
|
|
197
|
+
if (enablesLazy(options.lazyImportExportTransform)) found.push(`${preset.written} lazyImportExportTransform`);
|
|
198
|
+
}
|
|
199
|
+
for (const plugin of plugins) if (plugin.name === "@babel/plugin-transform-modules-commonjs") {
|
|
200
|
+
const options = plugin.options ?? {};
|
|
201
|
+
if (enablesLazy(options.lazy)) found.push(`${plugin.written} lazy`);
|
|
202
|
+
} else if (/inline-requires/.test(plugin.name)) found.push(plugin.written);
|
|
203
|
+
return [...new Set(found)];
|
|
204
|
+
}
|
|
205
|
+
/** For a config that could only be read as text: the preset options, by name (quoted or not). */
|
|
206
|
+
function lazyImportSettingsInText(text) {
|
|
207
|
+
const found = [];
|
|
208
|
+
if (/\blazyImports["']?\s*:\s*(?!(?:false|null|undefined)\b)\S/.test(text)) found.push("lazyImports");
|
|
209
|
+
if (/\blazyImportExportTransform["']?\s*:\s*(?!(?:false|null|undefined)\b)\S/.test(text)) found.push("lazyImportExportTransform");
|
|
210
|
+
if (/inline-requires/.test(text)) found.push("inline requires");
|
|
211
|
+
return found;
|
|
212
|
+
}
|
|
179
213
|
function readBabelConfig(root) {
|
|
180
214
|
const found = [];
|
|
181
215
|
for (const group of [PROJECT_FILES, RELATIVE_FILES]) {
|
|
@@ -200,18 +234,21 @@ function readBabelConfig(root) {
|
|
|
200
234
|
source: null,
|
|
201
235
|
evaluated: false,
|
|
202
236
|
plugins: [],
|
|
203
|
-
presets: []
|
|
237
|
+
presets: [],
|
|
238
|
+
lazyImports: []
|
|
204
239
|
};
|
|
205
240
|
const plugins = [];
|
|
206
|
-
const
|
|
241
|
+
const presetEntries = [];
|
|
242
|
+
const lazyInText = [];
|
|
207
243
|
let evaluated = true;
|
|
208
244
|
for (const { raw, text } of found) {
|
|
209
245
|
if (raw && typeof raw === "object") {
|
|
210
246
|
plugins.push(...entriesOf(raw, "plugins").map((p) => entryOf(p, "plugin")));
|
|
211
|
-
|
|
247
|
+
presetEntries.push(...entriesOf(raw, "presets").map((p) => entryOf(p, "preset")));
|
|
212
248
|
continue;
|
|
213
249
|
}
|
|
214
250
|
evaluated = false;
|
|
251
|
+
lazyInText.push(...lazyImportSettingsInText(text));
|
|
215
252
|
for (const written of new Set([...text.matchAll(/["'`]([^"'`\s]+)["'`]/g)].map((m) => m[1]))) {
|
|
216
253
|
const name = normalizeBabelName(written);
|
|
217
254
|
if (KNOWN_PLUGINS.some((known) => known.names.includes(name))) plugins.push({
|
|
@@ -224,7 +261,8 @@ function readBabelConfig(root) {
|
|
|
224
261
|
source: found.map((f) => f.source).join(" + "),
|
|
225
262
|
evaluated,
|
|
226
263
|
plugins,
|
|
227
|
-
presets
|
|
264
|
+
presets: presetEntries.map((p) => p.name),
|
|
265
|
+
lazyImports: [.../* @__PURE__ */ new Set([...lazyImportSettings(presetEntries, plugins), ...lazyInText])]
|
|
228
266
|
};
|
|
229
267
|
}
|
|
230
268
|
/** Why a worklet plugin is not needed — only when the preset replacing its library is active. */
|
|
@@ -297,7 +335,8 @@ function classifyBabelPlugins(report, root, active) {
|
|
|
297
335
|
}
|
|
298
336
|
/** The installed major version of `vite`, or null. */
|
|
299
337
|
function viteMajor(root) {
|
|
300
|
-
|
|
338
|
+
const major = Number(installedViteVersion(root)?.replace(/^[^0-9]*/, "").split(".")[0]);
|
|
339
|
+
return Number.isFinite(major) ? major : installedMajor(root, "vite");
|
|
301
340
|
}
|
|
302
341
|
/** Whether `name` is installed where the project resolves it. */
|
|
303
342
|
function installed(root, name) {
|
|
@@ -331,8 +370,7 @@ function serializableOptions(entry) {
|
|
|
331
370
|
};
|
|
332
371
|
return entry.options === void 0 || plain(entry.options);
|
|
333
372
|
}
|
|
334
|
-
|
|
335
|
-
//#region src/cli/doctor.ts
|
|
373
|
+
// src/cli/doctor.ts
|
|
336
374
|
/**
|
|
337
375
|
* `vitest-native doctor` — diagnose the project's environment: peer versions,
|
|
338
376
|
* engine resolution (and why), detected presets, RNTL/Node compatibility, and
|
|
@@ -352,10 +390,18 @@ function packageVersion(root, name) {
|
|
|
352
390
|
*/
|
|
353
391
|
function nodeRange() {
|
|
354
392
|
const fallback = ">=22.13.0 <23 || >=24.3.0";
|
|
393
|
+
for (let dir = path.dirname(fileURLToPath(import.meta.url));; dir = path.dirname(dir)) {
|
|
394
|
+
const manifest = readManifest(path.join(dir, "package.json"));
|
|
395
|
+
if (manifest?.name === "vitest-native") return manifest.engines?.node?.trim() || fallback;
|
|
396
|
+
if (path.dirname(dir) === dir) return fallback;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
/** A package.json, or null where there is none or it does not parse. */
|
|
400
|
+
function readManifest(file) {
|
|
355
401
|
try {
|
|
356
|
-
return JSON.parse(fs.readFileSync(
|
|
402
|
+
return JSON.parse(fs.readFileSync(file, "utf8"));
|
|
357
403
|
} catch {
|
|
358
|
-
return
|
|
404
|
+
return null;
|
|
359
405
|
}
|
|
360
406
|
}
|
|
361
407
|
const versionParts = (version) => {
|
|
@@ -421,6 +467,67 @@ function findConfigFile(root) {
|
|
|
421
467
|
function withoutComments(source) {
|
|
422
468
|
return source.replace(/\/\*[\s\S]*?\*\//g, " ").replace(/(^|[^:])\/\/[^\n]*/g, "$1");
|
|
423
469
|
}
|
|
470
|
+
const settingOf = (value) => value === void 0 ? null : value === "false" ? "off" : "set";
|
|
471
|
+
/**
|
|
472
|
+
* How a config sets Vitest's `fsModuleCache`, read as text, in each spelling: inside
|
|
473
|
+
* `experimental: { ... }` (Vitest 4.0.11 and later) and anywhere else (the top-level
|
|
474
|
+
* option, Vitest 5). `off` for a literal `false`, `set` for any other value, null when
|
|
475
|
+
* that spelling is absent.
|
|
476
|
+
*/
|
|
477
|
+
function fsModuleCacheSetting(source) {
|
|
478
|
+
const code = withoutComments(source);
|
|
479
|
+
const nested = /\bexperimental\s*:\s*\{[^{}]*?\bfsModuleCache\s*:\s*([^,}\s]*)/.exec(code);
|
|
480
|
+
const rest = nested ? code.replace(nested[0], " ") : code;
|
|
481
|
+
const top = /\bfsModuleCache\s*:\s*([^,}\s]*)/.exec(rest);
|
|
482
|
+
return {
|
|
483
|
+
experimental: settingOf(nested?.[1]),
|
|
484
|
+
topLevel: settingOf(top?.[1])
|
|
485
|
+
};
|
|
486
|
+
}
|
|
487
|
+
/** Whether `version` is at or above `[major, minor, patch]`. */
|
|
488
|
+
function versionAtLeast(version, floor) {
|
|
489
|
+
const parts = version.replace(/^[^0-9]*/, "").split(/[.-]/).slice(0, 3).map(Number);
|
|
490
|
+
for (let i = 0; i < 3; i++) {
|
|
491
|
+
const part = parts[i] ?? 0;
|
|
492
|
+
if (part !== floor[i]) return part > floor[i];
|
|
493
|
+
}
|
|
494
|
+
return true;
|
|
495
|
+
}
|
|
496
|
+
/** The first Vitest with `experimental.fsModuleCache`. */
|
|
497
|
+
const EXPERIMENTAL_FS_MODULE_CACHE_FROM = [
|
|
498
|
+
4,
|
|
499
|
+
0,
|
|
500
|
+
11
|
|
501
|
+
];
|
|
502
|
+
const TROUBLESHOOTING_STALE_IMPORT = "https://danfry1.github.io/vitest-native/guide/troubleshooting#cannot-find-module-keeps-failing-after-the-missing-file-exists";
|
|
503
|
+
/**
|
|
504
|
+
* The transform-cache note for a Vitest the plugin does not turn the cache on for.
|
|
505
|
+
* Empty where it does (fsModuleCacheOnByDefault, which the plugin reads too), and where
|
|
506
|
+
* the config turns the cache off on purpose.
|
|
507
|
+
*/
|
|
508
|
+
function transformCacheAdvice(vitestVersion, configSource) {
|
|
509
|
+
const major = Number(vitestVersion?.replace(/^[^0-9]*/, "").split(".")[0]);
|
|
510
|
+
if (!vitestVersion || !(major >= 4) || fsModuleCacheOnByDefault(major)) return [];
|
|
511
|
+
const setting = configSource === null ? {
|
|
512
|
+
experimental: null,
|
|
513
|
+
topLevel: null
|
|
514
|
+
} : fsModuleCacheSetting(configSource);
|
|
515
|
+
if (setting.experimental === "off") return [];
|
|
516
|
+
const hasOption = versionAtLeast(vitestVersion, EXPERIMENTAL_FS_MODULE_CACHE_FROM);
|
|
517
|
+
const caveat = `A Vitest bug can keep a "Cannot find module" failure cached after the file exists (reproduced on Vitest 5.0.5, unverified on 4); ${TROUBLESHOOTING_STALE_IMPORT} lists when it happens and how to recover.`;
|
|
518
|
+
if (setting.experimental === "set" && hasOption) return [`Vitest ${vitestVersion}: the config sets \`experimental.fsModuleCache\`; while it is on, transformed modules are kept on disk between runs. ${caveat}`];
|
|
519
|
+
const lines = [`Vitest ${vitestVersion} re-transforms every imported module on each run, about half a second per thousand imported modules on one benchmark. Vitest 5 keeps transformed modules on disk between runs, and vitest-native turns that on.`];
|
|
520
|
+
if (setting.topLevel !== null) lines.push("The config sets `test.fsModuleCache`, the Vitest 5 spelling, which has no effect on Vitest 4.");
|
|
521
|
+
lines.push(hasOption ? `To keep them on disk on Vitest 4, set \`test.experimental.fsModuleCache: true\`. ${caveat}` : `Vitest 4.0.11 adds \`test.experimental.fsModuleCache\` to keep them on disk.`);
|
|
522
|
+
return lines;
|
|
523
|
+
}
|
|
524
|
+
/**
|
|
525
|
+
* The Vitest that runs here, as the peer check reports it: the project's own, or under
|
|
526
|
+
* Vite+ the copy this package loads.
|
|
527
|
+
*/
|
|
528
|
+
function runningVitestVersion(from) {
|
|
529
|
+
return packageVersion(from, "vitest") ?? (usesVitePlus(from) ? ownVitestPackage()?.version ?? null : null);
|
|
530
|
+
}
|
|
424
531
|
/**
|
|
425
532
|
* Whether a config is built on one imported from elsewhere — a shared preset in a
|
|
426
533
|
* workspace, typically. Such a file legitimately never mentions vitest-native, and
|
|
@@ -539,10 +646,11 @@ function runDoctor(root, nodeVersion = process.versions.node) {
|
|
|
539
646
|
for (const { name, minimum, maximumMajor, minimumByMajor, optional } of PEER_REQUIREMENTS) {
|
|
540
647
|
if (optional) continue;
|
|
541
648
|
const from = rootFor(name);
|
|
542
|
-
const
|
|
649
|
+
const running = name === "vite" ? installedViteVersion(from) : name === "vitest" ? runningVitestVersion(from) : null;
|
|
650
|
+
const error = validatePeerDependency(name, minimum, from, maximumMajor, minimumByMajor, running);
|
|
543
651
|
if (error) fail(error);
|
|
544
652
|
else {
|
|
545
|
-
pass(`${name} ${packageVersion(from, name)}`);
|
|
653
|
+
pass(`${name} ${running ?? packageVersion(from, name)}`);
|
|
546
654
|
if (from !== root) lines.push(` (resolved from ${from}, not from here)`);
|
|
547
655
|
}
|
|
548
656
|
}
|
|
@@ -598,8 +706,12 @@ function runDoctor(root, nodeVersion = process.versions.node) {
|
|
|
598
706
|
}
|
|
599
707
|
if (detected.length) for (const d of detected) pass(d);
|
|
600
708
|
else if (!installedAny) lines.push(" (none — no preset-covered packages installed)");
|
|
601
|
-
const expo =
|
|
602
|
-
|
|
709
|
+
const expo = installedExpoVersion(root) ?? (configRoot === root ? null : installedExpoVersion(configRoot));
|
|
710
|
+
const unsupported = expo && unsupportedExpoSdkMessage(expo);
|
|
711
|
+
if (unsupported) {
|
|
712
|
+
lines.push("", "Expo");
|
|
713
|
+
warn(unsupported);
|
|
714
|
+
} else if (expo) {
|
|
603
715
|
lines.push("", "Expo");
|
|
604
716
|
pass((decision.engine === "native" ? `expo ${expo} detected. Under the native engine Expo modules run their real JavaScript on native-module mocks, as under jest-expo (unless the config sets \`engine: 'mock'\` or \`presets: { expo: true }\`, where the expo preset shadows ${PRESET_MODULES.expo.join(", ")}). ` : `expo ${expo} detected. Under the mock engine the expo preset shadows ${PRESET_MODULES.expo.join(", ")}; other Expo modules load their real JavaScript. `) + `Coming from jest-expo, run \`vitest-native migrate\`. See https://danfry1.github.io/vitest-native/guide/expo`);
|
|
605
717
|
}
|
|
@@ -617,28 +729,44 @@ function runDoctor(root, nodeVersion = process.versions.node) {
|
|
|
617
729
|
}
|
|
618
730
|
lines.push("", "Config");
|
|
619
731
|
const configFile = findConfigFile(configRoot);
|
|
732
|
+
let configSource = null;
|
|
620
733
|
if (!configFile) warn("no vitest.config.* found — run `vitest-native init` to create one.");
|
|
621
734
|
else {
|
|
622
735
|
const content = fs.readFileSync(path.join(configRoot, configFile), "utf8");
|
|
736
|
+
configSource = content;
|
|
623
737
|
const usage = analyzeConfigUsage(content);
|
|
624
738
|
if (usage.imports && usage.invokes) pass(`${configFile} uses vitest-native.`);
|
|
625
739
|
else if (usage.imports) warn(`${configFile} imports vitest-native but never calls reactNative() — the plugin is not in \`plugins: [...]\`, so React Native imports will not resolve.`);
|
|
626
740
|
else if (extendsSharedConfig(content)) lines.push(` · ${configFile} builds on a shared config, so this cannot tell from here whether the plugin is wired up. Check the config it extends.`);
|
|
627
741
|
else warn(`${configFile} exists but does not import vitest-native` + (content.includes("vitest-native") ? " (it is only mentioned in a comment)" : "") + " — add `reactNative()` to `plugins: [...]`, or run `vitest-native init`.");
|
|
628
742
|
}
|
|
743
|
+
const advice = transformCacheAdvice(runningVitestVersion(rootFor("vitest")), configSource);
|
|
744
|
+
if (advice.length) {
|
|
745
|
+
lines.push("", "Transform cache");
|
|
746
|
+
for (const line of advice) lines.push(` · ${line}`);
|
|
747
|
+
}
|
|
629
748
|
lines.push("", ok ? "✓ No blocking problems found." : "✗ Blocking problems found (see ✗ above).");
|
|
630
749
|
return {
|
|
631
750
|
lines,
|
|
632
751
|
ok
|
|
633
752
|
};
|
|
634
753
|
}
|
|
635
|
-
|
|
636
|
-
//#region src/cli/init.ts
|
|
754
|
+
// src/cli/init.ts
|
|
637
755
|
/**
|
|
638
756
|
* `vitest-native init` — write a ready-to-run Vitest config. Two shapes:
|
|
639
757
|
* the zero-config default, and the jest-compat variant (the exact block the
|
|
640
758
|
* migration guide documents, so docs and CLI can never drift apart by shape).
|
|
641
759
|
*/
|
|
760
|
+
/**
|
|
761
|
+
* Printed after `init` and `migrate`: the hot runtime is off by default, so a user
|
|
762
|
+
* reading only the CLI output would not otherwise learn it exists or what it costs.
|
|
763
|
+
*/
|
|
764
|
+
const HOT_RUNTIME_NOTE = [
|
|
765
|
+
"Hot runtime: off by default. `hotRuntime: 'auto'` reuses workers across test files instead of",
|
|
766
|
+
"starting a fresh one per file. Whether that is faster depends on the suite, so measure it on yours.",
|
|
767
|
+
"It has known differences from per-file isolation:",
|
|
768
|
+
" https://danfry1.github.io/vitest-native/guide/troubleshooting#a-test-passes-on-its-own-but-fails-when-run-with-other-files"
|
|
769
|
+
];
|
|
642
770
|
const PLAIN_CONFIG = `import { defineConfig } from 'vitest/config'
|
|
643
771
|
import { reactNative } from 'vitest-native'
|
|
644
772
|
|
|
@@ -672,11 +800,23 @@ export default defineConfig({
|
|
|
672
800
|
},
|
|
673
801
|
})
|
|
674
802
|
`;
|
|
675
|
-
|
|
676
|
-
|
|
803
|
+
/**
|
|
804
|
+
* The config source. Under Vite+ the config is `vite.config.ts` with `defineConfig`
|
|
805
|
+
* from 'vite-plus' (viteplus.dev/guide/test): the project does not install `vitest`
|
|
806
|
+
* itself, so a config importing 'vitest/config' fails to load under pnpm.
|
|
807
|
+
*/
|
|
808
|
+
function renderInitConfig(jestCompat, vitePlus = false) {
|
|
809
|
+
const source = jestCompat ? JEST_COMPAT_CONFIG : PLAIN_CONFIG;
|
|
810
|
+
return vitePlus ? source.replace("import { defineConfig } from 'vitest/config'", "import { defineConfig } from 'vite-plus'") : source;
|
|
677
811
|
}
|
|
678
|
-
function existingConfig(root) {
|
|
812
|
+
function existingConfig(root, vitePlus) {
|
|
679
813
|
for (const name of [
|
|
814
|
+
...vitePlus ? [
|
|
815
|
+
"vite.config.ts",
|
|
816
|
+
"vite.config.mts",
|
|
817
|
+
"vite.config.js",
|
|
818
|
+
"vite.config.mjs"
|
|
819
|
+
] : [],
|
|
680
820
|
"vitest.config.ts",
|
|
681
821
|
"vitest.config.mts",
|
|
682
822
|
"vitest.config.js",
|
|
@@ -688,18 +828,36 @@ function existingConfig(root) {
|
|
|
688
828
|
}
|
|
689
829
|
function runInit(root, opts = {}) {
|
|
690
830
|
const lines = [];
|
|
691
|
-
const
|
|
831
|
+
const vitePlus = usesVitePlus(root);
|
|
832
|
+
const existing = existingConfig(root, vitePlus);
|
|
833
|
+
if (existing && vitePlus && existing.startsWith("vite.config")) return {
|
|
834
|
+
lines: [
|
|
835
|
+
`✗ ${existing} already exists. Vite+ keeps tests in the same config, so add`,
|
|
836
|
+
` vitest-native to it rather than replacing it:`,
|
|
837
|
+
``,
|
|
838
|
+
` import { reactNative } from 'vitest-native'`,
|
|
839
|
+
``,
|
|
840
|
+
` export default defineConfig({`,
|
|
841
|
+
` plugins: [reactNative()],`,
|
|
842
|
+
` test: { environment: 'node' },`,
|
|
843
|
+
` })`,
|
|
844
|
+
``,
|
|
845
|
+
` Then run: vp test`
|
|
846
|
+
],
|
|
847
|
+
ok: false
|
|
848
|
+
};
|
|
692
849
|
if (existing && !opts.force) return {
|
|
693
850
|
lines: [`✗ ${existing} already exists — not overwriting. Re-run with --force to replace it,`, ` or run \`vitest-native migrate\` to analyze an existing Jest setup first.`],
|
|
694
851
|
ok: false
|
|
695
852
|
};
|
|
696
|
-
const file = fs.existsSync(path.join(root, "tsconfig.json")) ? "vitest.config.mts" : "vitest.config.mjs";
|
|
853
|
+
const file = vitePlus ? "vite.config.ts" : fs.existsSync(path.join(root, "tsconfig.json")) ? "vitest.config.mts" : "vitest.config.mjs";
|
|
697
854
|
const target = path.join(root, file);
|
|
698
|
-
fs.writeFileSync(target, renderInitConfig(opts.jestCompat ?? false));
|
|
855
|
+
fs.writeFileSync(target, renderInitConfig(opts.jestCompat ?? false, vitePlus));
|
|
699
856
|
lines.push(`✓ wrote ${file}${opts.jestCompat ? " (jest-compat variant)" : ""}`);
|
|
700
857
|
if (existing && opts.force) lines.push(` (replaced ${existing === file ? "it" : existing})`);
|
|
701
|
-
lines.push("", "Next steps:", ` 1. add a script: "test": "vitest"`, ` 2. run: npx vitest`, ` 3. check the environment: npx vitest-native doctor`);
|
|
858
|
+
lines.push("", "Next steps:", vitePlus ? ` 1. add a script: "test": "vp test"` : ` 1. add a script: "test": "vitest"`, vitePlus ? ` 2. run: vp test` : ` 2. run: npx vitest`, ` 3. check the environment: npx vitest-native doctor`);
|
|
702
859
|
if (opts.jestCompat) lines.push(` 4. migrating a Jest suite? \`vitest-native migrate\` maps your jest.config onto this file.`);
|
|
860
|
+
lines.push("", ...HOT_RUNTIME_NOTE);
|
|
703
861
|
return {
|
|
704
862
|
lines,
|
|
705
863
|
ok: true,
|
|
@@ -776,8 +934,7 @@ function tryExpand(source, literalDot) {
|
|
|
776
934
|
throw error;
|
|
777
935
|
}
|
|
778
936
|
}
|
|
779
|
-
|
|
780
|
-
//#region src/cli/allowlist.ts
|
|
937
|
+
// src/cli/allowlist.ts
|
|
781
938
|
/**
|
|
782
939
|
* Reading Jest's `transformIgnorePatterns` allowlist,
|
|
783
940
|
* `node_modules/(?!(a|b|@scope/c)/)`. Real ones nest — React Native's own preset
|
|
@@ -874,8 +1031,7 @@ function allows(entry, pkg) {
|
|
|
874
1031
|
if (entry.kind === "scope") return pkg.startsWith(`${entry.name}/`);
|
|
875
1032
|
return pkg.startsWith(entry.name);
|
|
876
1033
|
}
|
|
877
|
-
|
|
878
|
-
//#region src/cli/jest-discovery.ts
|
|
1034
|
+
// src/cli/jest-discovery.ts
|
|
879
1035
|
/**
|
|
880
1036
|
* Which files Jest treats as tests, said to Vitest.
|
|
881
1037
|
*
|
|
@@ -1238,8 +1394,7 @@ function booleanFlag(value) {
|
|
|
1238
1394
|
if (value === true || value === "true") return true;
|
|
1239
1395
|
if (value === "false") return false;
|
|
1240
1396
|
}
|
|
1241
|
-
|
|
1242
|
-
//#region src/cli/untranspiled.ts
|
|
1397
|
+
// src/cli/untranspiled.ts
|
|
1243
1398
|
/**
|
|
1244
1399
|
* Evidence that a package needs `transform`: a `.js` file under one of its entry
|
|
1245
1400
|
* points that is not standard JavaScript (JSX or Flow), which neither Node nor Vite
|
|
@@ -1347,8 +1502,7 @@ function untranspiledFile(root, pkg) {
|
|
|
1347
1502
|
}
|
|
1348
1503
|
return null;
|
|
1349
1504
|
}
|
|
1350
|
-
|
|
1351
|
-
//#region src/cli/migrate.ts
|
|
1505
|
+
// src/cli/migrate.ts
|
|
1352
1506
|
/**
|
|
1353
1507
|
* `vitest-native migrate` — analyze a project's Jest configuration and report,
|
|
1354
1508
|
* key by key, what maps automatically, what the presets already cover (delete
|
|
@@ -1361,13 +1515,41 @@ function untranspiledFile(root, pkg) {
|
|
|
1361
1515
|
* so that a migrated project collects the same test files with the same settings
|
|
1362
1516
|
* without hand edits.
|
|
1363
1517
|
*/
|
|
1518
|
+
/**
|
|
1519
|
+
* Jest 30's lookup order for `jest.config.*` (jest-config's JEST_CONFIG_EXT_ORDER; Jest 29's is
|
|
1520
|
+
* the same without .mts and .cts). Jest refuses to run when more than one exists, so the order
|
|
1521
|
+
* only decides which one migrate reports in that case.
|
|
1522
|
+
*/
|
|
1523
|
+
const JEST_CONFIG_EXTENSIONS = [
|
|
1524
|
+
".js",
|
|
1525
|
+
".ts",
|
|
1526
|
+
".mjs",
|
|
1527
|
+
".mts",
|
|
1528
|
+
".cjs",
|
|
1529
|
+
".cts",
|
|
1530
|
+
".json"
|
|
1531
|
+
];
|
|
1532
|
+
/**
|
|
1533
|
+
* Loads a Jest config file the way Node would run it. TypeScript configs (.ts, .mts, .cts)
|
|
1534
|
+
* load through Node's own type stripping, which is on by default from Node 22.18 and 23.6;
|
|
1535
|
+
* ES module configs (.mjs, .mts, and .js/.ts in a "type": "module" package) through
|
|
1536
|
+
* `require()` of ES modules. Jest itself loads a TypeScript config through ts-node and falls
|
|
1537
|
+
* back to `import()`, so a config that relies on either (extensionless relative imports,
|
|
1538
|
+
* `enum`, path aliases, top-level `await`) is reported with Node's reason rather than guessed at.
|
|
1539
|
+
*/
|
|
1364
1540
|
function loadConfigFile(root, file) {
|
|
1365
1541
|
const abs = path.resolve(root, file);
|
|
1366
1542
|
const name = path.relative(root, abs).split(path.sep).join("/") || file;
|
|
1367
1543
|
if (!fs.existsSync(abs)) return null;
|
|
1368
|
-
if (!/\.(
|
|
1544
|
+
if (!/\.(?:[cm]?[jt]s|json)$/.test(abs)) return {
|
|
1369
1545
|
source: name,
|
|
1370
|
-
config: null
|
|
1546
|
+
config: null,
|
|
1547
|
+
error: "not a JavaScript, TypeScript or JSON file"
|
|
1548
|
+
};
|
|
1549
|
+
if (/\.[cm]?ts$/.test(abs) && !process.features.typescript) return {
|
|
1550
|
+
source: name,
|
|
1551
|
+
config: null,
|
|
1552
|
+
error: `Node ${process.version} does not load TypeScript by itself; run migrate on Node 22.18 or later`
|
|
1371
1553
|
};
|
|
1372
1554
|
try {
|
|
1373
1555
|
const req = createRequire(path.join(root, "package.json"));
|
|
@@ -1375,16 +1557,23 @@ function loadConfigFile(root, file) {
|
|
|
1375
1557
|
const config = loaded && typeof loaded === "object" && "default" in loaded ? loaded.default : loaded;
|
|
1376
1558
|
if (typeof config === "function") return {
|
|
1377
1559
|
source: name,
|
|
1378
|
-
config: null
|
|
1560
|
+
config: null,
|
|
1561
|
+
error: "it exports a function, which Jest calls to build the config; migrate reads only a config object"
|
|
1562
|
+
};
|
|
1563
|
+
if (!config || typeof config !== "object") return {
|
|
1564
|
+
source: name,
|
|
1565
|
+
config: null,
|
|
1566
|
+
error: "it does not export a config object"
|
|
1379
1567
|
};
|
|
1380
1568
|
return {
|
|
1381
1569
|
source: name,
|
|
1382
1570
|
config
|
|
1383
1571
|
};
|
|
1384
|
-
} catch {
|
|
1572
|
+
} catch (error) {
|
|
1385
1573
|
return {
|
|
1386
1574
|
source: name,
|
|
1387
|
-
config: null
|
|
1575
|
+
config: null,
|
|
1576
|
+
error: error instanceof Error ? error.message.split("\n")[0] : String(error)
|
|
1388
1577
|
};
|
|
1389
1578
|
}
|
|
1390
1579
|
}
|
|
@@ -1393,22 +1582,10 @@ function loadJestConfig(root, explicit) {
|
|
|
1393
1582
|
const loaded = loadConfigFile(root, explicit);
|
|
1394
1583
|
if (loaded) return loaded;
|
|
1395
1584
|
}
|
|
1396
|
-
for (const
|
|
1397
|
-
|
|
1398
|
-
"jest.config.cjs",
|
|
1399
|
-
"jest.config.json"
|
|
1400
|
-
]) {
|
|
1401
|
-
const loaded = loadConfigFile(root, name);
|
|
1585
|
+
for (const extension of JEST_CONFIG_EXTENSIONS) {
|
|
1586
|
+
const loaded = loadConfigFile(root, `jest.config${extension}`);
|
|
1402
1587
|
if (loaded) return loaded;
|
|
1403
1588
|
}
|
|
1404
|
-
for (const name of [
|
|
1405
|
-
"jest.config.mjs",
|
|
1406
|
-
"jest.config.ts",
|
|
1407
|
-
"jest.config.mts"
|
|
1408
|
-
]) if (fs.existsSync(path.join(root, name))) return {
|
|
1409
|
-
source: name,
|
|
1410
|
-
config: null
|
|
1411
|
-
};
|
|
1412
1589
|
try {
|
|
1413
1590
|
const pkg = JSON.parse(fs.readFileSync(path.join(root, "package.json"), "utf8"));
|
|
1414
1591
|
if (pkg.jest && typeof pkg.jest === "object") return {
|
|
@@ -1507,9 +1684,10 @@ function analyzeJestConfig(root) {
|
|
|
1507
1684
|
}
|
|
1508
1685
|
} catch {}
|
|
1509
1686
|
const explicitConfig = scriptFlags?.flags.get("config") ?? scriptFlags?.flags.get("c");
|
|
1510
|
-
const { source, config } = loadJestConfig(root, typeof explicitConfig === "string" ? explicitConfig : void 0);
|
|
1687
|
+
const { source, config, error: configError } = loadJestConfig(root, typeof explicitConfig === "string" ? explicitConfig : void 0);
|
|
1511
1688
|
const automatic = [];
|
|
1512
1689
|
const attention = [];
|
|
1690
|
+
if (source && configError) attention.push(`${source} could not be loaded (${configError}), so none of its settings are in the suggested config; carry them over by hand.`);
|
|
1513
1691
|
const presetCovered = [];
|
|
1514
1692
|
const dropped = [];
|
|
1515
1693
|
const testEntries = [`globals: true`, `environment: 'node'`];
|
|
@@ -1658,7 +1836,7 @@ function analyzeJestConfig(root) {
|
|
|
1658
1836
|
"c"
|
|
1659
1837
|
].includes(name)) continue;
|
|
1660
1838
|
if (name === "forceExit") dropped.push("scripts.test --forceExit — `vitest run` exits by itself: if something keeps the process alive after the run it waits test.teardownTimeout, warns, and exits.");
|
|
1661
|
-
else if (name === "ci") dropped.push(
|
|
1839
|
+
else if (name === "ci") dropped.push("scripts.test --ci — Vitest writes no new snapshots when it detects CI, as Jest's --ci defaults to; set CI=true to get that elsewhere.");
|
|
1662
1840
|
else if ((name === "silent" || name === "passWithNoTests") && booleanFlag(value) !== void 0) {
|
|
1663
1841
|
const on = booleanFlag(value);
|
|
1664
1842
|
testEntries.push(`${name}: ${on}`);
|
|
@@ -1674,7 +1852,7 @@ function analyzeJestConfig(root) {
|
|
|
1674
1852
|
const env = take("testEnvironment");
|
|
1675
1853
|
if (env && env !== "node") attention.push(`testEnvironment: '${env}' — vitest-native suites run under 'node'; review.`);
|
|
1676
1854
|
else handled.add("testEnvironment");
|
|
1677
|
-
if (take("fakeTimers") !== void 0) attention.push(`fakeTimers —
|
|
1855
|
+
if (take("fakeTimers") !== void 0) attention.push(`fakeTimers — not carried over: call jest.useFakeTimers(options) in a beforeEach, which the jest-compat shim translates, or set Vitest's test.fakeTimers. Not at a setup file's top level: RNTL's import drops options set there.`);
|
|
1678
1856
|
const discoveryOptions = {};
|
|
1679
1857
|
for (const key of [
|
|
1680
1858
|
"testMatch",
|
|
@@ -1770,6 +1948,7 @@ function analyzeJestConfig(root) {
|
|
|
1770
1948
|
}
|
|
1771
1949
|
for (const p of classified.filter((c) => c.verdict === "unneeded")) dropped.push(`${babel.source}: ${p.written} — ${p.reason}`);
|
|
1772
1950
|
for (const p of classified.filter((c) => c.verdict === "unknown")) attention.push(`${babel.source}: ${p.reason}`);
|
|
1951
|
+
if (babel.lazyImports.length) attention.push(`${babel.source}: ${babel.lazyImports.join(", ")} — under Jest a package import ran only when the test first used it; here every import runs when its file loads. A module a test never touched is now evaluated, so a partial jest.mock of something it needs can fail (e.g. "requireOptionalNativeModule is not a function"). Give such a mock the rest of the module: jest.mock('x', () => ({ ...jest.requireActual('x'), ... })).`);
|
|
1773
1952
|
if (!babel.evaluated) attention.push(`${babel.source} could not be evaluated, so only plugins recognized by name are listed; review it for others.`);
|
|
1774
1953
|
}
|
|
1775
1954
|
if (transformPkgs.length) pluginOptions.unshift(`transform: [${transformPkgs.map((p) => JSON.stringify(p)).join(", ")}]`);
|
|
@@ -1779,14 +1958,14 @@ function analyzeJestConfig(root) {
|
|
|
1779
1958
|
const at = e.indexOf(": ");
|
|
1780
1959
|
return ` { find: ${e.slice(0, at)}, replacement: ${e.slice(at + 2)} },\n`;
|
|
1781
1960
|
}).join("") + regexAliases.map((e) => ` ${e},\n`).join("") + ` ]` : `{ ${["...jestCompatAliases()", ...aliasEntries].join(", ")} }`;
|
|
1961
|
+
const vitePlus = usesVitePlus(root);
|
|
1782
1962
|
return {
|
|
1783
1963
|
source,
|
|
1784
1964
|
automatic,
|
|
1785
1965
|
attention,
|
|
1786
1966
|
presetCovered,
|
|
1787
1967
|
dropped,
|
|
1788
|
-
suggestedConfig: `import { ${excludeGlobs.length ? "configDefaults, " : ""}defineConfig } from 'vitest/config'
|
|
1789
|
-
import { reactNative } from 'vitest-native'
|
|
1968
|
+
suggestedConfig: `${vitePlus ? `import { defineConfig } from 'vite-plus'\n${excludeGlobs.length ? "import { configDefaults } from 'vite-plus/test/config'\n" : ""}` : `import { ${excludeGlobs.length ? "configDefaults, " : ""}defineConfig } from 'vitest/config'\n`}import { reactNative } from 'vitest-native'
|
|
1790
1969
|
import { jestCompatAliases, jestCompatSetup, jestMockTransform } from 'vitest-native/jest-compat'
|
|
1791
1970
|
${needsUrlImport ? "import { fileURLToPath } from 'node:url'\n" : ""}${extraImports.map((i) => `${i}\n`).join("")}
|
|
1792
1971
|
export default defineConfig({
|
|
@@ -1805,6 +1984,7 @@ export default defineConfig({
|
|
|
1805
1984
|
},
|
|
1806
1985
|
})
|
|
1807
1986
|
`,
|
|
1987
|
+
vitePlus,
|
|
1808
1988
|
ok: source !== null
|
|
1809
1989
|
};
|
|
1810
1990
|
}
|
|
@@ -1846,10 +2026,19 @@ function classifyAllowlist(root, entries, active, autoInlined, out) {
|
|
|
1846
2026
|
if (groups.detected.length) out.automatic.push(`transformIgnorePatterns allows ${list(groups.detected)} — detected by the engine (React Native ecosystem), which compiles what Node cannot run; nothing to do.`);
|
|
1847
2027
|
if (groups.absent.length) out.automatic.push(`transformIgnorePatterns allows ${list(groups.absent)} — not installed; nothing to do.`);
|
|
1848
2028
|
}
|
|
1849
|
-
|
|
2029
|
+
/** The commands to run once the suggested config is saved. */
|
|
2030
|
+
function migrationNextStep(report) {
|
|
2031
|
+
return report.vitePlus ? "npx vitest-native doctor && vp test" : "npx vitest-native doctor && npx vitest";
|
|
2032
|
+
}
|
|
2033
|
+
/**
|
|
2034
|
+
* The report `migrate` prints. With `write: true` (`migrate --write`) the config is about to
|
|
2035
|
+
* be saved, so the closing line does not ask for a re-run with --write; the CLI prints the
|
|
2036
|
+
* next step after the file is written.
|
|
2037
|
+
*/
|
|
2038
|
+
function renderMigrationReport(report, { write = false } = {}) {
|
|
1850
2039
|
const lines = [];
|
|
1851
2040
|
if (!report.source) {
|
|
1852
|
-
lines.push("✗ no Jest configuration found (package.json#jest or jest.config.{js,cjs,json}).", " Starting fresh? Run `vitest-native init` instead.");
|
|
2041
|
+
lines.push("✗ no Jest configuration found (package.json#jest or jest.config.{js,ts,mjs,cjs,mts,cts,json}).", " Starting fresh? Run `vitest-native init` instead.");
|
|
1853
2042
|
return lines;
|
|
1854
2043
|
}
|
|
1855
2044
|
lines.push(`Analyzed ${report.source}`, "");
|
|
@@ -1863,13 +2052,13 @@ function renderMigrationReport(report) {
|
|
|
1863
2052
|
section("Covered by presets — delete", report.presetCovered);
|
|
1864
2053
|
section("Needs your attention", report.attention);
|
|
1865
2054
|
section("Dropped (no equivalent needed)", report.dropped);
|
|
1866
|
-
lines.push("Suggested vitest.config:", "");
|
|
2055
|
+
lines.push(report.vitePlus ? "Suggested vite.config:" : "Suggested vitest.config:", "");
|
|
1867
2056
|
lines.push(...report.suggestedConfig.split("\n").map((l) => ` ${l}`));
|
|
1868
|
-
lines.push(
|
|
2057
|
+
if (!write) lines.push(`Re-run with --write to save this config, then: ${migrationNextStep(report)}`, "");
|
|
2058
|
+
lines.push(...HOT_RUNTIME_NOTE);
|
|
1869
2059
|
return lines;
|
|
1870
2060
|
}
|
|
1871
|
-
|
|
1872
|
-
//#region src/cli/index.ts
|
|
2061
|
+
// src/cli/index.ts
|
|
1873
2062
|
/**
|
|
1874
2063
|
* vitest-native CLI: `init` (write a ready config), `doctor` (diagnose the
|
|
1875
2064
|
* environment), `migrate` (analyze a Jest config and report the mapping).
|
|
@@ -1922,8 +2111,9 @@ function main(argv, log = console.log) {
|
|
|
1922
2111
|
}
|
|
1923
2112
|
case "migrate": {
|
|
1924
2113
|
const report = analyzeJestConfig(root);
|
|
1925
|
-
|
|
1926
|
-
|
|
2114
|
+
const write = has("--write");
|
|
2115
|
+
for (const line of renderMigrationReport(report, { write })) log(line);
|
|
2116
|
+
if (report.ok && write) {
|
|
1927
2117
|
const init = runInit(root, { force: has("--force") });
|
|
1928
2118
|
if (!init.ok) {
|
|
1929
2119
|
for (const line of init.lines) log(line);
|
|
@@ -1931,7 +2121,7 @@ function main(argv, log = console.log) {
|
|
|
1931
2121
|
}
|
|
1932
2122
|
const target = path.join(root, init.wrote);
|
|
1933
2123
|
fs.writeFileSync(target, report.suggestedConfig);
|
|
1934
|
-
log(`✓ wrote ${init.wrote} from the analysis above
|
|
2124
|
+
log(`✓ wrote ${init.wrote} from the analysis above. Next: ${migrationNextStep(report)}`);
|
|
1935
2125
|
}
|
|
1936
2126
|
return report.ok ? 0 : 1;
|
|
1937
2127
|
}
|
|
@@ -1943,5 +2133,4 @@ function main(argv, log = console.log) {
|
|
|
1943
2133
|
}
|
|
1944
2134
|
const entry = process.argv[1] ? path.basename(process.argv[1]) : "";
|
|
1945
2135
|
if (entry === "cli.mjs" || entry === "cli.cjs" || entry === "vitest-native") process.exit(main(process.argv.slice(2)));
|
|
1946
|
-
//#endregion
|
|
1947
2136
|
export { main };
|