vitest-native 0.15.0 → 0.15.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.
@@ -0,0 +1,104 @@
1
+ import { createRequire } from "node:module";
2
+ import path from "node:path";
3
+ import { docsUrl } from "./docs-url.mjs";
4
+ import { VitestNativeError } from "./errors.mjs";
5
+
6
+ /**
7
+ * The context a preset module's factory builds its mock with: the React the test renders
8
+ * with, which is the copy `react-native` resolves (its components and the test renderer
9
+ * must share one React for hooks and contexts to work). Not the copy beside the shadowed
10
+ * package: the preset replaces that package's code, so a React it nests is never used.
11
+ * Where `react-native` is not installed (the mock engine can run without it), the
12
+ * project's React.
13
+ *
14
+ * Required when the mock is built, in the file that imports it, and not imported by the
15
+ * presets themselves: vitest-native's presets load once per worker, and under the hot
16
+ * runtime's freshRuntime each file has its own React (#719). Elsewhere this is the one
17
+ * React the worker keeps.
18
+ *
19
+ * With `check`, once per project root in a worker, it also fails when the project or React
20
+ * Native Testing Library resolves another copy of React: a mock built with one copy fails in a tree another renders, and
21
+ * a copy that never renders cannot tell a hook call inside a render from one outside it.
22
+ */
23
+ export function presetFactoryContext(pkg, root, { check = false } = {}) {
24
+ if (check) assertOneReact(root, `the ${pkg} preset builds its mock with`);
25
+ return { React: rendererRequire(root)("react") };
26
+ }
27
+
28
+ /**
29
+ * A require from where the test's renderer resolves its packages: the directory
30
+ * `react-native` resolves to from `root`, or `root` where `react-native` is not
31
+ * installed. The one anchor for which copy of React (and its siblings) a test uses:
32
+ * preset factories take React from it, and anything else that must agree with them
33
+ * resolves from it too.
34
+ */
35
+ export function rendererRequire(root) {
36
+ return requireFrom(createRequire(path.join(root, "package.json")), "react-native/package.json");
37
+ }
38
+
39
+ /**
40
+ * Fails with PRESET_REACT_MISMATCH when the project or React Native Testing Library
41
+ * resolves another copy of React than rendererRequire's. `user` names who uses the
42
+ * anchor's copy, for the message. The check runs once per root in a worker, since it
43
+ * compares installed files, which do not change during a run; its finding is kept, so
44
+ * each later call on a failed root fails again, naming its own `user`.
45
+ *
46
+ * Safe to call from a resolver: the check's own resolutions go back through Node's
47
+ * resolver, and a call for the same root made during them returns at once, leaving the
48
+ * outer call to decide.
49
+ */
50
+ export function assertOneReact(root, user = "vitest-native uses") {
51
+ let finding = findings.get(root);
52
+ if (finding === CHECKING) return;
53
+ if (finding === undefined) {
54
+ findings.set(root, CHECKING);
55
+ try {
56
+ const fromRoot = createRequire(path.join(root, "package.json"));
57
+ finding = findMismatch(fromRoot, rendererRequire(root));
58
+ } finally {
59
+ findings.delete(root);
60
+ }
61
+ findings.set(root, finding);
62
+ }
63
+ if (finding) throw mismatchError(user, finding);
64
+ }
65
+
66
+ /** Per root: CHECKING while the check runs, then null (one React) or the mismatch found. */
67
+ const findings = new Map();
68
+ const CHECKING = Symbol("checking");
69
+
70
+ /** A require from the file `fromRoot` resolves `request` to, or `fromRoot` where there is none. */
71
+ function requireFrom(fromRoot, request) {
72
+ let file;
73
+ try {
74
+ file = fromRoot.resolve(request);
75
+ } catch {
76
+ return fromRoot;
77
+ }
78
+ // A resolver hook can answer with a virtual module id rather than throw (the mock
79
+ // engine serves react-native itself): only a file on disk anchors a require.
80
+ return path.isAbsolute(file) ? createRequire(file) : fromRoot;
81
+ }
82
+
83
+ function findMismatch(fromRoot, fromRenderer) {
84
+ const react = fromRenderer.resolve("react");
85
+ const others = [["the project", fromRoot]];
86
+ const rntl = requireFrom(fromRoot, "@testing-library/react-native/package.json");
87
+ if (rntl !== fromRoot) others.push(["@testing-library/react-native", rntl]);
88
+ for (const [who, from] of others) {
89
+ const theirs = from.resolve("react");
90
+ if (theirs !== react) return { react, who, theirs };
91
+ }
92
+ return null;
93
+ }
94
+
95
+ function mismatchError(user, { react, who, theirs }) {
96
+ return new VitestNativeError(
97
+ "PRESET_REACT_MISMATCH",
98
+ `${user} the React react-native uses (${react}), ` +
99
+ `but ${who} resolves another copy (${theirs}). Hooks and contexts from one copy ` +
100
+ `fail in a tree the other renders. Install one copy of react: dedupe it in the ` +
101
+ `workspace, or align the versions the packages ask for.`,
102
+ { docs: docsUrl("/guide/errors#PRESET_REACT_MISMATCH") },
103
+ );
104
+ }