vitest-native 0.14.0-rc.8 → 0.14.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.
Files changed (99) hide show
  1. package/README.md +91 -72
  2. package/dist/cli.d.mts +1 -3
  3. package/dist/cli.mjs +215 -76
  4. package/dist/context.d.mts +45 -0
  5. package/dist/context.mjs +243 -0
  6. package/dist/errors.d.mts +3 -0
  7. package/dist/helpers.cjs +53 -4
  8. package/dist/helpers.d.cts +1 -3
  9. package/dist/helpers.d.mts +1 -3
  10. package/dist/helpers.mjs +2 -3
  11. package/dist/host-5o48Ujzs.mjs +939 -0
  12. package/dist/{index-BSyUeoga.d.mts → index-DqU4YO3D.d.mts} +32 -53
  13. package/dist/index.d.mts +1 -3
  14. package/dist/index.mjs +464 -596
  15. package/dist/jest-compat/error-equality.mjs +127 -0
  16. package/dist/jest-compat/jest-object.mjs +149 -4
  17. package/dist/jest-compat/node-registry.mjs +233 -58
  18. package/dist/jest-compat/setup.mjs +34 -4
  19. package/dist/jest-compat.cjs +227 -52
  20. package/dist/jest-compat.d.cts +5 -6
  21. package/dist/jest-compat.d.mts +5 -6
  22. package/dist/jest-compat.mjs +204 -52
  23. package/dist/matchers.cjs +1 -2
  24. package/dist/matchers.d.cts +0 -2
  25. package/dist/matchers.d.mts +0 -2
  26. package/dist/matchers.mjs +1 -2
  27. package/dist/{memory-FYbP0_gg.mjs → memory-Couw0Esx.mjs} +31 -6
  28. package/dist/native/assets.d.mts +2 -0
  29. package/dist/native/assets.mjs +34 -0
  30. package/dist/native/babel-toolchain.mjs +190 -0
  31. package/dist/native/babel-worker.mjs +47 -0
  32. package/dist/native/boundary.mjs +23 -8
  33. package/dist/native/compile-cache.mjs +21 -9
  34. package/dist/native/coverage-v8.mjs +287 -0
  35. package/dist/native/esm-interop.mjs +216 -0
  36. package/dist/native/expo-native-modules.mjs +7 -1
  37. package/dist/native/globals.mjs +104 -6
  38. package/dist/native/hooks.mjs +271 -60
  39. package/dist/native/loader.mjs +409 -20
  40. package/dist/native/match.d.mts +24 -2
  41. package/dist/native/match.mjs +117 -12
  42. package/dist/native/metro-profile-compiler.mjs +2 -31
  43. package/dist/native/module-reset.mjs +37 -5
  44. package/dist/native/native-modules.d.mts +2 -3
  45. package/dist/native/native-modules.mjs +22 -21
  46. package/dist/native/ownership.d.mts +10 -0
  47. package/dist/native/ownership.mjs +96 -9
  48. package/dist/native/registry-compiler.mjs +6 -0
  49. package/dist/native/registry.mjs +138 -119
  50. package/dist/native/resident-health.mjs +133 -0
  51. package/dist/native/resolve.d.mts +6 -0
  52. package/dist/native/resolve.mjs +100 -9
  53. package/dist/native/rntl-hooks.mjs +157 -6
  54. package/dist/native/rntl-text-host.mjs +64 -0
  55. package/dist/native/runner.mjs +106 -1
  56. package/dist/native/setup.mjs +105 -77
  57. package/dist/native/state-manifest.mjs +14 -8
  58. package/dist/native/transform.d.mts +2 -0
  59. package/dist/native/transform.mjs +476 -59
  60. package/dist/native/worker.mjs +151 -46
  61. package/dist/{pool-DjpOjOf3.mjs → pool-CFSpNfyw.mjs} +37 -11
  62. package/dist/{preset-map-BlBriTA0.mjs → preset-map-CogjYmSw.mjs} +4 -19
  63. package/dist/preset-modules.mjs +2 -11
  64. package/dist/{presets--zrEWQC2.mjs → presets-CXDyg51l.mjs} +65 -82
  65. package/dist/presets.d.mts +1 -1
  66. package/dist/presets.mjs +1 -1
  67. package/dist/report-error.d.mts +20 -0
  68. package/dist/report-error.mjs +145 -0
  69. package/dist/rntl-support.d.mts +12 -0
  70. package/dist/rntl-support.mjs +189 -0
  71. package/dist/{rolldown-runtime-BhDjJH2R.mjs → rolldown-runtime-D70C1uOS.mjs} +1 -2
  72. package/dist/serializer.cjs +1 -2
  73. package/dist/serializer.d.cts +1 -3
  74. package/dist/serializer.d.mts +1 -3
  75. package/dist/serializer.mjs +1 -2
  76. package/dist/setup.mjs +95 -156
  77. package/dist/{worker-vitest-BGPo0ek6.mjs → worker-vitest-CILPFeRX.mjs} +2 -3
  78. package/docs/migrating-from-jest.md +76 -12
  79. package/docs/migrating-from-vitest-react-native.md +1 -1
  80. package/docs/release-readiness.md +6 -5
  81. package/docs/versioning.md +1 -1
  82. package/package.json +121 -103
  83. package/CHANGELOG.md +0 -2475
  84. package/dist/cli.cjs +0 -1924
  85. package/dist/cli.d.cts +0 -3
  86. package/dist/ecosystem-D6e1OVOg.mjs +0 -1019
  87. package/dist/ecosystem-MFwkbnaC.cjs +0 -1099
  88. package/dist/errors-B3lJK46O.cjs +0 -75
  89. package/dist/errors-CxoBUfVJ.mjs +0 -64
  90. package/dist/index-bZ7AHqLc.d.cts +0 -482
  91. package/dist/memory-6EPHrJA1.cjs +0 -144
  92. package/dist/pool-B4yesGhI.cjs +0 -121
  93. package/dist/preset-map-jSFsNy1Q.cjs +0 -241
  94. package/dist/preset-modules-BoLkaeEN.cjs +0 -53
  95. package/dist/preset-modules-CG8-G82P.mjs +0 -36
  96. package/dist/presets-9f83H0WW.cjs +0 -3939
  97. package/dist/rolldown-runtime-C0BPl7ul.cjs +0 -43
  98. package/dist/worker-vitest-BfiQB8ND.cjs +0 -62
  99. package/docs/hot-default-design.md +0 -216
@@ -0,0 +1,243 @@
1
+ // One validated, frozen description of a Vitest project (ADR-0003). The plugin writes
2
+ // it to a content-addressed file; workers find it through VITEST_NATIVE_CONTEXT, read
3
+ // it once per realm and fail by name (CONTEXT_INVALID) when it is wrong, instead of
4
+ // turning a bad value into a default. Shipped verbatim and imported by every bundle,
5
+ // so each realm holds one instance.
6
+ import crypto from "node:crypto";
7
+ import fs from "node:fs";
8
+ import os from "node:os";
9
+ import path from "node:path";
10
+ import { VitestNativeError } from "./errors.mjs";
11
+
12
+ export const CONTEXT_ENV = "VITEST_NATIVE_CONTEXT";
13
+ export const CONTEXT_VERSION = 1;
14
+
15
+ /** Hand-set diagnostic switches that stay environment variables. */
16
+ export const ESCAPE_HATCHES = Object.freeze([
17
+ "VITEST_NATIVE_LOADER_THREAD",
18
+ "VITEST_NATIVE_NO_REGISTRY",
19
+ "VITEST_NATIVE_HOT_ESM_GEN",
20
+ "VITEST_NATIVE_HOT_STATE_MUTATION",
21
+ ]);
22
+
23
+ const strings = (v) => Array.isArray(v) && v.every((x) => typeof x === "string");
24
+ const object = (v) => v !== null && typeof v === "object" && !Array.isArray(v);
25
+ const absolute = (v) => typeof v === "string" && path.isAbsolute(v);
26
+
27
+ /** Every field and its shape. Required fields are always written. */
28
+ const SCHEMA = {
29
+ platform: { valid: (v) => v === "ios" || v === "android", required: true },
30
+ diagnostics: { valid: (v) => typeof v === "boolean", required: true },
31
+ projectRoot: { valid: absolute, required: true },
32
+ reactNativeVersion: { valid: (v) => typeof v === "string" },
33
+ transform: { valid: strings },
34
+ sourceExts: { valid: strings },
35
+ assetExts: { valid: strings },
36
+ hotPreserveGlobals: { valid: strings },
37
+ hotEsmGeneration: { valid: (v) => typeof v === "boolean" },
38
+ sourceMaps: { valid: (v) => typeof v === "boolean" },
39
+ hotReleaseGenerations: { valid: (v) => typeof v === "boolean" },
40
+ registry: { valid: absolute },
41
+ ownership: { valid: object },
42
+ projectDirs: { valid: strings },
43
+ requireAliases: {
44
+ valid: (v) =>
45
+ Array.isArray(v) && v.every((e) => Array.isArray(e) && e.length === 2 && strings(e)),
46
+ },
47
+ requireAliasesSkipped: { valid: strings },
48
+ presetNames: { valid: strings },
49
+ presetConfig: { valid: object },
50
+ presetExports: { valid: object },
51
+ presetModules: { valid: strings },
52
+ presetDisabled: { valid: strings },
53
+ mocks: { valid: (v) => v !== undefined },
54
+ knownNativeModules: { valid: strings },
55
+ nativeModuleSpecs: { valid: object },
56
+ nativeModules: { valid: (v) => v === "permissive" },
57
+ expoSdks: {
58
+ valid: (v) => Array.isArray(v) && v.length === 2 && v.every((n) => Number.isInteger(n)),
59
+ },
60
+ };
61
+
62
+ function invalid(reason, field, detail, dir) {
63
+ return new VitestNativeError(
64
+ "CONTEXT_INVALID",
65
+ `the project context is invalid (${reason}${field ? `: ${field}` : ""}). ${detail} ` +
66
+ `The plugin writes it for each run. Re-run Vitest; if it persists, delete ` +
67
+ `${dir ?? "the contexts directory in vitest-native's cache"}.`,
68
+ );
69
+ }
70
+
71
+ function validate(fields, dir) {
72
+ if (!object(fields)) throw invalid("schema", null, "Its fields are not an object.", dir);
73
+ for (const name of Object.keys(fields)) {
74
+ if (!Object.hasOwn(SCHEMA, name)) throw invalid("schema", name, `Unknown field.`, dir);
75
+ }
76
+ for (const [name, { valid, required }] of Object.entries(SCHEMA)) {
77
+ if (fields[name] === undefined) {
78
+ if (required) throw invalid("schema", name, `The field is missing.`, dir);
79
+ } else if (!valid(fields[name])) {
80
+ throw invalid("schema", name, `The field has the wrong shape.`, dir);
81
+ }
82
+ }
83
+ }
84
+
85
+ function deepFreeze(value) {
86
+ if (value && typeof value === "object" && !Object.isFrozen(value)) {
87
+ Object.freeze(value);
88
+ for (const child of Object.values(value)) deepFreeze(child);
89
+ }
90
+ return value;
91
+ }
92
+
93
+ const keyOf = (fields) =>
94
+ crypto.createHash("sha256").update(JSON.stringify(fields)).digest("hex").slice(0, 20);
95
+
96
+ /** Files older than this are removed whenever a context is written. */
97
+ const PRUNE_AFTER_MS = 7 * 24 * 60 * 60 * 1000;
98
+
99
+ /** Files this process wrote or reused: never pruned while it runs. */
100
+ const inUse = new Set();
101
+
102
+ function writeIn(dir, key, body) {
103
+ fs.mkdirSync(dir, { recursive: true });
104
+ const file = path.join(dir, `${key}.json`);
105
+ inUse.add(file);
106
+ if (fs.existsSync(file)) {
107
+ // Reused: mark it fresh, or another run's pruning would remove it mid-run.
108
+ const now = new Date();
109
+ fs.utimesSync(file, now, now);
110
+ return file;
111
+ }
112
+ const tmp = `${file}.${process.pid}.${crypto.randomUUID()}.tmp`;
113
+ try {
114
+ fs.writeFileSync(tmp, body, { mode: 0o600 });
115
+ try {
116
+ fs.renameSync(tmp, file);
117
+ } catch (error) {
118
+ // Another run wrote the same content first (Windows refuses the overwrite).
119
+ if (!((error.code === "EPERM" || error.code === "EEXIST") && fs.existsSync(file))) {
120
+ throw error;
121
+ }
122
+ }
123
+ } finally {
124
+ fs.rmSync(tmp, { force: true });
125
+ }
126
+ for (const name of fs.readdirSync(dir)) {
127
+ const old = path.join(dir, name);
128
+ try {
129
+ if (!inUse.has(old) && Date.now() - fs.statSync(old).mtimeMs > PRUNE_AFTER_MS) {
130
+ fs.rmSync(old, { force: true });
131
+ }
132
+ } catch {
133
+ // Removed by a concurrent run.
134
+ }
135
+ }
136
+ return file;
137
+ }
138
+
139
+ /**
140
+ * Main process: write `fields` and return the value for CONTEXT_ENV. Falls back to the
141
+ * system temp directory when `cacheRoot` cannot be written.
142
+ */
143
+ export function writeProjectContext(fields, cacheRoot) {
144
+ validate(fields);
145
+ const key = keyOf(fields);
146
+ const body = JSON.stringify({ version: CONTEXT_VERSION, key, fields });
147
+ let file;
148
+ try {
149
+ file = writeIn(path.join(cacheRoot, "contexts"), key, body);
150
+ } catch {
151
+ file = writeIn(path.join(os.tmpdir(), "vitest-native-cache", "contexts"), key, body);
152
+ }
153
+ return `${file}#${key}`;
154
+ }
155
+
156
+ function read(ref) {
157
+ const hash = ref.lastIndexOf("#");
158
+ if (hash === -1) throw invalid("missing", null, `'${ref}' does not name a context.`);
159
+ const file = ref.slice(0, hash);
160
+ const key = ref.slice(hash + 1);
161
+ const dir = path.dirname(file);
162
+ let parsed;
163
+ try {
164
+ parsed = JSON.parse(fs.readFileSync(file, "utf8"));
165
+ } catch (cause) {
166
+ const reason = cause?.code === "ENOENT" ? "missing" : "schema";
167
+ throw invalid(reason, null, `${file} could not be read: ${cause?.message ?? cause}.`, dir);
168
+ }
169
+ if (parsed?.version !== CONTEXT_VERSION) {
170
+ throw invalid("version", null, `${file} has version ${parsed?.version}.`, dir);
171
+ }
172
+ if (parsed.key !== key) throw invalid("key", null, `${file} holds key ${parsed.key}.`, dir);
173
+ validate(parsed.fields, dir);
174
+ if (keyOf(parsed.fields) !== key) {
175
+ throw invalid("key", null, `${file} was changed after it was written.`, dir);
176
+ }
177
+ return parsed.fields;
178
+ }
179
+
180
+ /**
181
+ * Main process: make sure the file behind `ref` holds `fields`, rewriting it when it
182
+ * is missing or fails validation (a cache cleared during a watch session). Returns the
183
+ * reference to use, which moves to the temp directory if the cache cannot be written.
184
+ */
185
+ export function ensureProjectContext(ref, fields) {
186
+ const file = ref.slice(0, ref.lastIndexOf("#"));
187
+ try {
188
+ read(ref);
189
+ const now = new Date();
190
+ fs.utimesSync(file, now, now);
191
+ return ref;
192
+ } catch {
193
+ fs.rmSync(file, { force: true });
194
+ return writeProjectContext(fields, path.dirname(path.dirname(file)));
195
+ }
196
+ }
197
+
198
+ const loaded = new Map();
199
+
200
+ /**
201
+ * Worker: the context named by `ref` (default CONTEXT_ENV), validated and frozen, read
202
+ * once per realm. Null only when no reference is given: code running outside the
203
+ * plugin, which keeps its own defaults.
204
+ */
205
+ export function projectContext(ref = process.env[CONTEXT_ENV]) {
206
+ if (!ref) return null;
207
+ let context = loaded.get(ref);
208
+ if (!context) {
209
+ context = deepFreeze(read(ref));
210
+ loaded.set(ref, context);
211
+ }
212
+ return context;
213
+ }
214
+
215
+ /** Worker code that only runs under the plugin. */
216
+ export function requireProjectContext(ref = process.env[CONTEXT_ENV]) {
217
+ const context = projectContext(ref);
218
+ if (!context) throw invalid("missing", null, `${CONTEXT_ENV} is not set in this worker.`);
219
+ return context;
220
+ }
221
+
222
+ const HANDLE = Symbol.for("vitest-native");
223
+
224
+ /** Hot worker boot: record the project this worker serves. */
225
+ export function markBootContext(ref = process.env[CONTEXT_ENV]) {
226
+ (globalThis[HANDLE] ??= {}).bootContext = ref;
227
+ }
228
+
229
+ /**
230
+ * Per file: a hot worker serves one project. Vitest refuses to reuse a runner across
231
+ * projects (its isEqualRunner); this is the backstop.
232
+ */
233
+ export function assertBootContext(ref = process.env[CONTEXT_ENV]) {
234
+ const boot = globalThis[HANDLE]?.bootContext;
235
+ if (boot !== undefined && boot !== ref) {
236
+ throw invalid(
237
+ "project",
238
+ null,
239
+ `This worker booted for ${boot} and was handed a file from ${ref}. Report this, ` +
240
+ `with the Vitest config, as a vitest-native bug.`,
241
+ );
242
+ }
243
+ }
package/dist/errors.d.mts CHANGED
@@ -11,6 +11,8 @@ export type VitestNativeErrorCode =
11
11
  | "INVALID_OPTION"
12
12
  | "UNKNOWN_OPTION"
13
13
  | "UNSUPPORTED_PEER"
14
+ | "VITEST_TWIN_INSTANCE"
15
+ | "CONTEXT_INVALID"
14
16
  | "UNSUPPORTED_POOL"
15
17
  | "INLINE_BREAKS_OWNERSHIP"
16
18
  | "MODULE_OWNER_CONFLICT"
@@ -40,6 +42,7 @@ export type VitestNativeErrorCode =
40
42
  | "JEST_MOCK_FACTORY_CYCLE"
41
43
  | "JEST_COMPAT_SETUP_MISSING"
42
44
  | "BABEL_IGNORES_FILE"
45
+ | "IMPORT_META_IN_NODE_OWNED"
43
46
  | "MATCHER_BAD_RECEIVER";
44
47
 
45
48
  export interface VitestNativeErrorOptions {
package/dist/helpers.cjs CHANGED
@@ -1,6 +1,56 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_errors = require("./errors-B3lJK46O.cjs");
3
- //#region src/helpers.ts
2
+ // src/errors.mjs
3
+ /**
4
+ * The error types this package throws.
5
+ *
6
+ * Plain .mjs on purpose. The shipped native and jest-compat runtimes are copied to dist
7
+ * verbatim and import this by relative path, and the test suite loads those same files
8
+ * from src — so the module has to resolve identically in both trees. A .ts source would
9
+ * only exist in one of them.
10
+ *
11
+ * WHAT SURVIVES WHERE, measured rather than assumed:
12
+ *
13
+ * - In the main process, and in a user's own try/catch inside a test, everything is
14
+ * intact.
15
+ * - Across the worker/reporter boundary Vitest keeps only `name`, `message` and
16
+ * `stack`. `code` and every other own property is dropped.
17
+ *
18
+ * So the message must stay self-sufficient: nothing may move out of it into a field on
19
+ * the assumption a reader will see it.
20
+ *
21
+ * `isVitestNativeError` deliberately checks SHAPE rather than `instanceof`. Bundled
22
+ * entries and the verbatim .mjs runtimes can hold separate copies of this module, and a
23
+ * prototype check would quietly fail across that seam. Shape does not care.
24
+ */
25
+ const PREFIX = "[vitest-native]";
26
+ /** Guarantees the prefix exactly once, however the caller wrote the message. */
27
+ function withPrefix(message) {
28
+ return message.startsWith(PREFIX) ? message : `${PREFIX} ${message}`;
29
+ }
30
+ function buildMessage(message, options) {
31
+ const base = withPrefix(message);
32
+ return options?.docs ? `${base}\nSee ${options.docs}` : base;
33
+ }
34
+ /**
35
+ * Error options, omitted entirely when there is no cause.
36
+ *
37
+ * `new Error(msg, { cause: undefined })` installs an own `cause` property set to
38
+ * undefined, which a plain `new Error(msg)` does not — enough to make some reporters
39
+ * print an empty cause and to make these errors shaped differently from every other
40
+ * error in the process. Passing nothing keeps them identical.
41
+ */
42
+ function errorOptions(options) {
43
+ return options && "cause" in options && options.cause !== void 0 ? { cause: options.cause } : void 0;
44
+ }
45
+ /** A failure originating in vitest-native. */
46
+ var VitestNativeError = class extends Error {
47
+ constructor(code, message, options) {
48
+ super(buildMessage(message, options), errorOptions(options));
49
+ this.name = "VitestNativeError";
50
+ this.code = code;
51
+ }
52
+ };
53
+ // src/helpers.ts
4
54
  /**
5
55
  * Test helpers for vitest-native.
6
56
  *
@@ -9,7 +59,7 @@ const require_errors = require("./errors-B3lJK46O.cjs");
9
59
  */
10
60
  function getMock() {
11
61
  const mock = globalThis.__vitest_native_mock;
12
- if (!mock) throw new require_errors.VitestNativeError("HELPERS_BEFORE_SETUP", "helpers were called before setup ran. Ensure the vitest-native plugin is configured.");
62
+ if (!mock) throw new VitestNativeError("HELPERS_BEFORE_SETUP", "helpers were called before setup ran. Ensure the vitest-native plugin is configured.");
13
63
  return mock;
14
64
  }
15
65
  function getNativeControl() {
@@ -151,7 +201,6 @@ function resetAllMocks() {
151
201
  }
152
202
  }
153
203
  }
154
- //#endregion
155
204
  exports.extendPresetMock = extendPresetMock;
156
205
  exports.mockNativeModule = mockNativeModule;
157
206
  exports.resetAllMocks = resetAllMocks;
@@ -1,4 +1,3 @@
1
- //#region src/helpers.d.ts
2
1
  export declare function setPlatform(os: "ios" | "android"): void;
3
2
  export declare function setDimensions(dims: {
4
3
  width: number;
@@ -21,5 +20,4 @@ export declare function setInsets(insets: {
21
20
  */
22
21
  export declare function extendPresetMock(pkg: string, overrides: Record<string, any>): boolean;
23
22
  export declare function mockNativeModule(name: string, impl: Record<string, any>): void;
24
- export declare function resetAllMocks(): void;
25
- //#endregion
23
+ export declare function resetAllMocks(): void;
@@ -1,4 +1,3 @@
1
- //#region src/helpers.d.ts
2
1
  export declare function setPlatform(os: "ios" | "android"): void;
3
2
  export declare function setDimensions(dims: {
4
3
  width: number;
@@ -21,5 +20,4 @@ export declare function setInsets(insets: {
21
20
  */
22
21
  export declare function extendPresetMock(pkg: string, overrides: Record<string, any>): boolean;
23
22
  export declare function mockNativeModule(name: string, impl: Record<string, any>): void;
24
- export declare function resetAllMocks(): void;
25
- //#endregion
23
+ export declare function resetAllMocks(): void;
package/dist/helpers.mjs CHANGED
@@ -1,5 +1,5 @@
1
- import { t as VitestNativeError } from "./errors-CxoBUfVJ.mjs";
2
- //#region src/helpers.ts
1
+ import { VitestNativeError } from "./errors.mjs";
2
+ // src/helpers.ts
3
3
  /**
4
4
  * Test helpers for vitest-native.
5
5
  *
@@ -150,5 +150,4 @@ function resetAllMocks() {
150
150
  }
151
151
  }
152
152
  }
153
- //#endregion
154
153
  export { extendPresetMock, mockNativeModule, resetAllMocks, setColorScheme, setDimensions, setInsets, setPlatform };