vitest-native 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +456 -0
- package/dist/chunk-CjcI7cDX.mjs +15 -0
- package/dist/helpers.cjs +113 -0
- package/dist/helpers.d.cts +19 -0
- package/dist/helpers.d.mts +19 -0
- package/dist/helpers.mjs +108 -0
- package/dist/index-CJqS-y_M.d.cts +268 -0
- package/dist/index-h4vpIkQO.d.mts +266 -0
- package/dist/index.cjs +308 -0
- package/dist/index.d.cts +14 -0
- package/dist/index.d.mts +14 -0
- package/dist/index.mjs +299 -0
- package/dist/preset-map-C5d8DSY7.cjs +21 -0
- package/dist/preset-map-CTAjv5aa.mjs +16 -0
- package/dist/presets-ArwboRy7.cjs +980 -0
- package/dist/presets-DgEuMxfn.mjs +896 -0
- package/dist/presets.cjs +9 -0
- package/dist/presets.d.cts +2 -0
- package/dist/presets.d.mts +2 -0
- package/dist/presets.mjs +2 -0
- package/dist/serializer.cjs +118 -0
- package/dist/serializer.d.cts +16 -0
- package/dist/serializer.d.mts +16 -0
- package/dist/serializer.mjs +117 -0
- package/dist/setup.cjs +1869 -0
- package/dist/setup.d.cts +1 -0
- package/dist/setup.d.mts +1 -0
- package/dist/setup.mjs +1869 -0
- package/package.json +124 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_presets = require("./presets-ArwboRy7.cjs");
|
|
3
|
+
const require_preset_map = require("./preset-map-C5d8DSY7.cjs");
|
|
4
|
+
let node_url = require("node:url");
|
|
5
|
+
let node_path = require("node:path");
|
|
6
|
+
node_path = require_presets.__toESM(node_path);
|
|
7
|
+
let node_fs = require("node:fs");
|
|
8
|
+
node_fs = require_presets.__toESM(node_fs);
|
|
9
|
+
let node_module = require("node:module");
|
|
10
|
+
//#region src/resolve.ts
|
|
11
|
+
const PLATFORM_EXTENSIONS_IOS = [
|
|
12
|
+
".ios.tsx",
|
|
13
|
+
".ios.ts",
|
|
14
|
+
".ios.jsx",
|
|
15
|
+
".ios.js",
|
|
16
|
+
".native.tsx",
|
|
17
|
+
".native.ts",
|
|
18
|
+
".native.jsx",
|
|
19
|
+
".native.js",
|
|
20
|
+
".tsx",
|
|
21
|
+
".ts",
|
|
22
|
+
".jsx",
|
|
23
|
+
".js"
|
|
24
|
+
];
|
|
25
|
+
const PLATFORM_EXTENSIONS_ANDROID = [
|
|
26
|
+
".android.tsx",
|
|
27
|
+
".android.ts",
|
|
28
|
+
".android.jsx",
|
|
29
|
+
".android.js",
|
|
30
|
+
".native.tsx",
|
|
31
|
+
".native.ts",
|
|
32
|
+
".native.jsx",
|
|
33
|
+
".native.js",
|
|
34
|
+
".tsx",
|
|
35
|
+
".ts",
|
|
36
|
+
".jsx",
|
|
37
|
+
".js"
|
|
38
|
+
];
|
|
39
|
+
function getPlatformExtensions(platform) {
|
|
40
|
+
return platform === "ios" ? PLATFORM_EXTENSIONS_IOS : PLATFORM_EXTENSIONS_ANDROID;
|
|
41
|
+
}
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region src/plugin.ts
|
|
44
|
+
const DEFAULT_ASSET_EXTS = [
|
|
45
|
+
"png",
|
|
46
|
+
"jpg",
|
|
47
|
+
"jpeg",
|
|
48
|
+
"gif",
|
|
49
|
+
"bmp",
|
|
50
|
+
"webp",
|
|
51
|
+
"svg",
|
|
52
|
+
"tiff",
|
|
53
|
+
"heic",
|
|
54
|
+
"heif",
|
|
55
|
+
"mp4",
|
|
56
|
+
"mp3",
|
|
57
|
+
"wav",
|
|
58
|
+
"aac",
|
|
59
|
+
"m4a",
|
|
60
|
+
"mov",
|
|
61
|
+
"webm",
|
|
62
|
+
"ttf",
|
|
63
|
+
"otf",
|
|
64
|
+
"woff",
|
|
65
|
+
"woff2"
|
|
66
|
+
];
|
|
67
|
+
/** Strip Vite's /@fs/ prefix to get a real filesystem path. */
|
|
68
|
+
function stripFsPrefix(id) {
|
|
69
|
+
return id.startsWith("/@fs/") ? id.slice(4) : id;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Check if a package is installed from the consumer's project root,
|
|
73
|
+
* not from the plugin's own node_modules. This is critical for monorepos
|
|
74
|
+
* and non-hoisted layouts (pnpm strict, yarn PnP).
|
|
75
|
+
*/
|
|
76
|
+
function isPackageInstalled(packageName, projectRoot) {
|
|
77
|
+
try {
|
|
78
|
+
(0, node_module.createRequire)(node_path.default.join(projectRoot, "package.json")).resolve(packageName);
|
|
79
|
+
return true;
|
|
80
|
+
} catch {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
async function autoDetectPresets(diagnostics, projectRoot) {
|
|
85
|
+
const detected = [];
|
|
86
|
+
const presetFactories = await Promise.resolve().then(() => require("./presets.cjs"));
|
|
87
|
+
for (const [pkgName, exportName] of Object.entries(require_preset_map.AUTO_DETECT_PRESETS)) if (isPackageInstalled(pkgName, projectRoot)) {
|
|
88
|
+
const factory = presetFactories[exportName];
|
|
89
|
+
if (typeof factory === "function") {
|
|
90
|
+
detected.push(factory());
|
|
91
|
+
if (diagnostics) console.log(`[vitest-native] Auto-detected ${pkgName} → enabled ${exportName} preset`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return detected;
|
|
95
|
+
}
|
|
96
|
+
async function resolveOptions(options = {}, projectRoot) {
|
|
97
|
+
const platform = options.platform ?? "ios";
|
|
98
|
+
const diagnostics = options.diagnostics ?? false;
|
|
99
|
+
const userExts = (options.assetExts ?? []).map((e) => e.replace(/^\./, ""));
|
|
100
|
+
let presets;
|
|
101
|
+
if (options.presets) presets = options.presets;
|
|
102
|
+
else presets = await autoDetectPresets(diagnostics, projectRoot ?? process.cwd());
|
|
103
|
+
return {
|
|
104
|
+
platform,
|
|
105
|
+
diagnostics,
|
|
106
|
+
extensions: getPlatformExtensions(platform),
|
|
107
|
+
presets,
|
|
108
|
+
mocks: options.mocks ?? {},
|
|
109
|
+
assetExts: [...DEFAULT_ASSET_EXTS, ...userExts]
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* All named exports from the react-native mock that virtual subpath
|
|
114
|
+
* modules should re-export. Kept in sync with registry.ts.
|
|
115
|
+
*/
|
|
116
|
+
const RN_EXPORT_NAMES = [
|
|
117
|
+
"View",
|
|
118
|
+
"Text",
|
|
119
|
+
"Image",
|
|
120
|
+
"TextInput",
|
|
121
|
+
"ScrollView",
|
|
122
|
+
"FlatList",
|
|
123
|
+
"SectionList",
|
|
124
|
+
"Modal",
|
|
125
|
+
"Pressable",
|
|
126
|
+
"TouchableOpacity",
|
|
127
|
+
"TouchableHighlight",
|
|
128
|
+
"TouchableWithoutFeedback",
|
|
129
|
+
"TouchableNativeFeedback",
|
|
130
|
+
"ActivityIndicator",
|
|
131
|
+
"Button",
|
|
132
|
+
"Switch",
|
|
133
|
+
"RefreshControl",
|
|
134
|
+
"StatusBar",
|
|
135
|
+
"SafeAreaView",
|
|
136
|
+
"KeyboardAvoidingView",
|
|
137
|
+
"ImageBackground",
|
|
138
|
+
"VirtualizedList",
|
|
139
|
+
"InputAccessoryView",
|
|
140
|
+
"DrawerLayoutAndroid",
|
|
141
|
+
"Platform",
|
|
142
|
+
"Dimensions",
|
|
143
|
+
"StyleSheet",
|
|
144
|
+
"Animated",
|
|
145
|
+
"Alert",
|
|
146
|
+
"Linking",
|
|
147
|
+
"AppState",
|
|
148
|
+
"Keyboard",
|
|
149
|
+
"BackHandler",
|
|
150
|
+
"Vibration",
|
|
151
|
+
"PermissionsAndroid",
|
|
152
|
+
"Appearance",
|
|
153
|
+
"PixelRatio",
|
|
154
|
+
"LayoutAnimation",
|
|
155
|
+
"Clipboard",
|
|
156
|
+
"Share",
|
|
157
|
+
"AccessibilityInfo",
|
|
158
|
+
"InteractionManager",
|
|
159
|
+
"PanResponder",
|
|
160
|
+
"ToastAndroid",
|
|
161
|
+
"ActionSheetIOS",
|
|
162
|
+
"LogBox",
|
|
163
|
+
"Easing",
|
|
164
|
+
"I18nManager",
|
|
165
|
+
"DeviceEventEmitter",
|
|
166
|
+
"useColorScheme",
|
|
167
|
+
"useWindowDimensions",
|
|
168
|
+
"NativeModules",
|
|
169
|
+
"TurboModuleRegistry",
|
|
170
|
+
"UIManager",
|
|
171
|
+
"NativeEventEmitter",
|
|
172
|
+
"NativeAppEventEmitter",
|
|
173
|
+
"NativeComponentRegistry",
|
|
174
|
+
"requireNativeComponent",
|
|
175
|
+
"AppRegistry",
|
|
176
|
+
"VirtualizedSectionList",
|
|
177
|
+
"Touchable",
|
|
178
|
+
"processColor",
|
|
179
|
+
"findNodeHandle",
|
|
180
|
+
"PlatformColor",
|
|
181
|
+
"DynamicColorIOS",
|
|
182
|
+
"Settings",
|
|
183
|
+
"DeviceInfo",
|
|
184
|
+
"useAnimatedValue",
|
|
185
|
+
"RootTagContext",
|
|
186
|
+
"ReactNativeVersion",
|
|
187
|
+
"Systrace",
|
|
188
|
+
"DevSettings",
|
|
189
|
+
"Networking",
|
|
190
|
+
"unstable_batchedUpdates",
|
|
191
|
+
"registerCallableModule",
|
|
192
|
+
"codegenNativeCommands",
|
|
193
|
+
"codegenNativeComponent",
|
|
194
|
+
"UTFSequence",
|
|
195
|
+
"ProgressBarAndroid",
|
|
196
|
+
"PushNotificationIOS",
|
|
197
|
+
"NativeDialogManagerAndroid",
|
|
198
|
+
"usePressability"
|
|
199
|
+
];
|
|
200
|
+
/** Names of all built-in presets that can be reconstructed in the worker. */
|
|
201
|
+
const BUILT_IN_PRESET_NAMES = new Set([
|
|
202
|
+
"reanimated",
|
|
203
|
+
"gestureHandler",
|
|
204
|
+
"safeAreaContext",
|
|
205
|
+
"navigation",
|
|
206
|
+
"asyncStorage",
|
|
207
|
+
"screens",
|
|
208
|
+
"expo"
|
|
209
|
+
]);
|
|
210
|
+
/** Check if a value (or any nested value) contains functions. */
|
|
211
|
+
function containsFunctions(value, visited = /* @__PURE__ */ new WeakSet()) {
|
|
212
|
+
if (typeof value === "function") return true;
|
|
213
|
+
if (value === null || typeof value !== "object") return false;
|
|
214
|
+
if (visited.has(value)) return false;
|
|
215
|
+
visited.add(value);
|
|
216
|
+
for (const v of Object.values(value)) if (containsFunctions(v, visited)) return true;
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Vitest plugin for React Native.
|
|
221
|
+
*
|
|
222
|
+
* Handles platform-specific module resolution, asset stubs, preset virtual
|
|
223
|
+
* modules, and automatic setup-file injection so tests can run against
|
|
224
|
+
* React Native code in a Node/JSDOM environment.
|
|
225
|
+
*/
|
|
226
|
+
function reactNative(options) {
|
|
227
|
+
if (options?.presets) {
|
|
228
|
+
for (const preset of options.presets) if (!BUILT_IN_PRESET_NAMES.has(preset.name)) throw new Error(`[vitest-native] Unknown preset "${preset.name}". Only built-in presets are supported: ${[...BUILT_IN_PRESET_NAMES].join(", ")}. For custom module mocking, use vi.mock() in a setup file.`);
|
|
229
|
+
}
|
|
230
|
+
if (options?.mocks && containsFunctions(options.mocks)) throw new Error("[vitest-native] The \"mocks\" option contains function values, which cannot be transferred to Vitest worker processes. Only JSON-serializable values (strings, numbers, booleans, plain objects, arrays) are supported.\n\nFor function-based mock overrides, use vi.mock() in a setup file:\n\n // vitest.setup.ts\n import { vi } from 'vitest';\n vi.mock('react-native', async (importOriginal) => {\n const actual = await importOriginal();\n return { ...actual, Alert: { alert: vi.fn() } };\n });");
|
|
231
|
+
let resolved;
|
|
232
|
+
const presetModules = /* @__PURE__ */ new Map();
|
|
233
|
+
const presetExportNames = /* @__PURE__ */ new Map();
|
|
234
|
+
let assetPattern;
|
|
235
|
+
const thisDir = node_path.default.dirname((0, node_url.fileURLToPath)(require("url").pathToFileURL(__filename).href));
|
|
236
|
+
let setupFilePath = node_path.default.resolve(thisDir, "setup.mjs");
|
|
237
|
+
if (!node_fs.default.existsSync(setupFilePath)) {
|
|
238
|
+
const srcPath = node_path.default.resolve(thisDir, "setup.ts");
|
|
239
|
+
if (node_fs.default.existsSync(srcPath)) setupFilePath = srcPath;
|
|
240
|
+
}
|
|
241
|
+
const platform = options?.platform ?? "ios";
|
|
242
|
+
const diagnostics = options?.diagnostics ?? false;
|
|
243
|
+
const extensions = getPlatformExtensions(platform);
|
|
244
|
+
return {
|
|
245
|
+
name: "vitest-native",
|
|
246
|
+
enforce: "pre",
|
|
247
|
+
config(userConfig, _env) {
|
|
248
|
+
const resolvedRoot = userConfig.root ? node_path.default.resolve(userConfig.root) : process.cwd();
|
|
249
|
+
const env = {
|
|
250
|
+
VITEST_NATIVE_PLATFORM: platform,
|
|
251
|
+
VITEST_NATIVE_DIAGNOSTICS: String(diagnostics),
|
|
252
|
+
VITEST_NATIVE_PROJECT_ROOT: resolvedRoot
|
|
253
|
+
};
|
|
254
|
+
if (options?.mocks && Object.keys(options.mocks).length > 0) env.VITEST_NATIVE_MOCKS = JSON.stringify(options.mocks);
|
|
255
|
+
if (options?.presets) env.VITEST_NATIVE_PRESET_NAMES = JSON.stringify(options.presets.map((p) => p.name));
|
|
256
|
+
return {
|
|
257
|
+
resolve: {
|
|
258
|
+
extensions,
|
|
259
|
+
conditions: ["react-native"]
|
|
260
|
+
},
|
|
261
|
+
test: {
|
|
262
|
+
setupFiles: [setupFilePath],
|
|
263
|
+
env
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
},
|
|
267
|
+
async configResolved(config) {
|
|
268
|
+
resolved = await resolveOptions(options, config.root);
|
|
269
|
+
for (const preset of resolved.presets) for (const [moduleName, presetModule] of Object.entries(preset.modules)) {
|
|
270
|
+
presetModules.set(moduleName, presetModule.factory);
|
|
271
|
+
presetExportNames.set(moduleName, presetModule.exports);
|
|
272
|
+
}
|
|
273
|
+
assetPattern = new RegExp(`\\.(${resolved.assetExts.join("|")})$`);
|
|
274
|
+
},
|
|
275
|
+
resolveId(source) {
|
|
276
|
+
if (source === "react-native") return "\0virtual:react-native";
|
|
277
|
+
if (source.startsWith("react-native/")) return `\0virtual:rn-subpath:${source}`;
|
|
278
|
+
if (presetModules.has(source)) return `\0virtual:preset:${source}`;
|
|
279
|
+
},
|
|
280
|
+
load(id) {
|
|
281
|
+
if (id === "\0virtual:react-native") return "export default {};";
|
|
282
|
+
if (id.startsWith("\0virtual:rn-subpath:")) return [
|
|
283
|
+
`const _rn = globalThis.__vitest_native_mock || {};`,
|
|
284
|
+
...RN_EXPORT_NAMES.map((n) => `export const ${n} = _rn['${n}'];`),
|
|
285
|
+
`export default _rn;`
|
|
286
|
+
].join("\n");
|
|
287
|
+
if (id.startsWith("\0virtual:preset:")) {
|
|
288
|
+
const moduleName = id.slice(16);
|
|
289
|
+
const exportNames = presetExportNames.get(moduleName) || [];
|
|
290
|
+
return [
|
|
291
|
+
`const _m = (globalThis.__vitest_native_preset_mocks || {})['${moduleName}'] || {};`,
|
|
292
|
+
...exportNames.map((n) => `export const ${n} = _m['${n}'];`),
|
|
293
|
+
`export default _m;`
|
|
294
|
+
].join("\n");
|
|
295
|
+
}
|
|
296
|
+
const fsPath = stripFsPrefix(id);
|
|
297
|
+
if (assetPattern.test(fsPath)) return `export default "${fsPath.split("/").pop() ?? fsPath}";`;
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
//#endregion
|
|
302
|
+
Object.defineProperty(exports, "presets", {
|
|
303
|
+
enumerable: true,
|
|
304
|
+
get: function() {
|
|
305
|
+
return require_presets.presets_exports;
|
|
306
|
+
}
|
|
307
|
+
});
|
|
308
|
+
exports.reactNative = reactNative;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { d as VitestNativeOptions, l as Preset, t as index_d_exports, u as ReactNativeMock } from "./index-CJqS-y_M.cjs";
|
|
2
|
+
import { Plugin } from "vite";
|
|
3
|
+
|
|
4
|
+
//#region src/plugin.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Vitest plugin for React Native.
|
|
7
|
+
*
|
|
8
|
+
* Handles platform-specific module resolution, asset stubs, preset virtual
|
|
9
|
+
* modules, and automatic setup-file injection so tests can run against
|
|
10
|
+
* React Native code in a Node/JSDOM environment.
|
|
11
|
+
*/
|
|
12
|
+
declare function reactNative(options?: VitestNativeOptions): Plugin;
|
|
13
|
+
//#endregion
|
|
14
|
+
export { type Preset, type ReactNativeMock, type VitestNativeOptions, index_d_exports as presets, reactNative };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { d as VitestNativeOptions, l as Preset, t as index_d_exports, u as ReactNativeMock } from "./index-h4vpIkQO.mjs";
|
|
2
|
+
import { Plugin } from "vite";
|
|
3
|
+
|
|
4
|
+
//#region src/plugin.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Vitest plugin for React Native.
|
|
7
|
+
*
|
|
8
|
+
* Handles platform-specific module resolution, asset stubs, preset virtual
|
|
9
|
+
* modules, and automatic setup-file injection so tests can run against
|
|
10
|
+
* React Native code in a Node/JSDOM environment.
|
|
11
|
+
*/
|
|
12
|
+
declare function reactNative(options?: VitestNativeOptions): Plugin;
|
|
13
|
+
//#endregion
|
|
14
|
+
export { type Preset, type ReactNativeMock, type VitestNativeOptions, index_d_exports as presets, reactNative };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import { t as AUTO_DETECT_PRESETS } from "./preset-map-CTAjv5aa.mjs";
|
|
2
|
+
import { t as presets_exports } from "./presets-DgEuMxfn.mjs";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import fs from "node:fs";
|
|
7
|
+
//#region src/resolve.ts
|
|
8
|
+
const PLATFORM_EXTENSIONS_IOS = [
|
|
9
|
+
".ios.tsx",
|
|
10
|
+
".ios.ts",
|
|
11
|
+
".ios.jsx",
|
|
12
|
+
".ios.js",
|
|
13
|
+
".native.tsx",
|
|
14
|
+
".native.ts",
|
|
15
|
+
".native.jsx",
|
|
16
|
+
".native.js",
|
|
17
|
+
".tsx",
|
|
18
|
+
".ts",
|
|
19
|
+
".jsx",
|
|
20
|
+
".js"
|
|
21
|
+
];
|
|
22
|
+
const PLATFORM_EXTENSIONS_ANDROID = [
|
|
23
|
+
".android.tsx",
|
|
24
|
+
".android.ts",
|
|
25
|
+
".android.jsx",
|
|
26
|
+
".android.js",
|
|
27
|
+
".native.tsx",
|
|
28
|
+
".native.ts",
|
|
29
|
+
".native.jsx",
|
|
30
|
+
".native.js",
|
|
31
|
+
".tsx",
|
|
32
|
+
".ts",
|
|
33
|
+
".jsx",
|
|
34
|
+
".js"
|
|
35
|
+
];
|
|
36
|
+
function getPlatformExtensions(platform) {
|
|
37
|
+
return platform === "ios" ? PLATFORM_EXTENSIONS_IOS : PLATFORM_EXTENSIONS_ANDROID;
|
|
38
|
+
}
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region src/plugin.ts
|
|
41
|
+
const DEFAULT_ASSET_EXTS = [
|
|
42
|
+
"png",
|
|
43
|
+
"jpg",
|
|
44
|
+
"jpeg",
|
|
45
|
+
"gif",
|
|
46
|
+
"bmp",
|
|
47
|
+
"webp",
|
|
48
|
+
"svg",
|
|
49
|
+
"tiff",
|
|
50
|
+
"heic",
|
|
51
|
+
"heif",
|
|
52
|
+
"mp4",
|
|
53
|
+
"mp3",
|
|
54
|
+
"wav",
|
|
55
|
+
"aac",
|
|
56
|
+
"m4a",
|
|
57
|
+
"mov",
|
|
58
|
+
"webm",
|
|
59
|
+
"ttf",
|
|
60
|
+
"otf",
|
|
61
|
+
"woff",
|
|
62
|
+
"woff2"
|
|
63
|
+
];
|
|
64
|
+
/** Strip Vite's /@fs/ prefix to get a real filesystem path. */
|
|
65
|
+
function stripFsPrefix(id) {
|
|
66
|
+
return id.startsWith("/@fs/") ? id.slice(4) : id;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Check if a package is installed from the consumer's project root,
|
|
70
|
+
* not from the plugin's own node_modules. This is critical for monorepos
|
|
71
|
+
* and non-hoisted layouts (pnpm strict, yarn PnP).
|
|
72
|
+
*/
|
|
73
|
+
function isPackageInstalled(packageName, projectRoot) {
|
|
74
|
+
try {
|
|
75
|
+
createRequire(path.join(projectRoot, "package.json")).resolve(packageName);
|
|
76
|
+
return true;
|
|
77
|
+
} catch {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
async function autoDetectPresets(diagnostics, projectRoot) {
|
|
82
|
+
const detected = [];
|
|
83
|
+
const presetFactories = await import("./presets.mjs");
|
|
84
|
+
for (const [pkgName, exportName] of Object.entries(AUTO_DETECT_PRESETS)) if (isPackageInstalled(pkgName, projectRoot)) {
|
|
85
|
+
const factory = presetFactories[exportName];
|
|
86
|
+
if (typeof factory === "function") {
|
|
87
|
+
detected.push(factory());
|
|
88
|
+
if (diagnostics) console.log(`[vitest-native] Auto-detected ${pkgName} → enabled ${exportName} preset`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return detected;
|
|
92
|
+
}
|
|
93
|
+
async function resolveOptions(options = {}, projectRoot) {
|
|
94
|
+
const platform = options.platform ?? "ios";
|
|
95
|
+
const diagnostics = options.diagnostics ?? false;
|
|
96
|
+
const userExts = (options.assetExts ?? []).map((e) => e.replace(/^\./, ""));
|
|
97
|
+
let presets;
|
|
98
|
+
if (options.presets) presets = options.presets;
|
|
99
|
+
else presets = await autoDetectPresets(diagnostics, projectRoot ?? process.cwd());
|
|
100
|
+
return {
|
|
101
|
+
platform,
|
|
102
|
+
diagnostics,
|
|
103
|
+
extensions: getPlatformExtensions(platform),
|
|
104
|
+
presets,
|
|
105
|
+
mocks: options.mocks ?? {},
|
|
106
|
+
assetExts: [...DEFAULT_ASSET_EXTS, ...userExts]
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* All named exports from the react-native mock that virtual subpath
|
|
111
|
+
* modules should re-export. Kept in sync with registry.ts.
|
|
112
|
+
*/
|
|
113
|
+
const RN_EXPORT_NAMES = [
|
|
114
|
+
"View",
|
|
115
|
+
"Text",
|
|
116
|
+
"Image",
|
|
117
|
+
"TextInput",
|
|
118
|
+
"ScrollView",
|
|
119
|
+
"FlatList",
|
|
120
|
+
"SectionList",
|
|
121
|
+
"Modal",
|
|
122
|
+
"Pressable",
|
|
123
|
+
"TouchableOpacity",
|
|
124
|
+
"TouchableHighlight",
|
|
125
|
+
"TouchableWithoutFeedback",
|
|
126
|
+
"TouchableNativeFeedback",
|
|
127
|
+
"ActivityIndicator",
|
|
128
|
+
"Button",
|
|
129
|
+
"Switch",
|
|
130
|
+
"RefreshControl",
|
|
131
|
+
"StatusBar",
|
|
132
|
+
"SafeAreaView",
|
|
133
|
+
"KeyboardAvoidingView",
|
|
134
|
+
"ImageBackground",
|
|
135
|
+
"VirtualizedList",
|
|
136
|
+
"InputAccessoryView",
|
|
137
|
+
"DrawerLayoutAndroid",
|
|
138
|
+
"Platform",
|
|
139
|
+
"Dimensions",
|
|
140
|
+
"StyleSheet",
|
|
141
|
+
"Animated",
|
|
142
|
+
"Alert",
|
|
143
|
+
"Linking",
|
|
144
|
+
"AppState",
|
|
145
|
+
"Keyboard",
|
|
146
|
+
"BackHandler",
|
|
147
|
+
"Vibration",
|
|
148
|
+
"PermissionsAndroid",
|
|
149
|
+
"Appearance",
|
|
150
|
+
"PixelRatio",
|
|
151
|
+
"LayoutAnimation",
|
|
152
|
+
"Clipboard",
|
|
153
|
+
"Share",
|
|
154
|
+
"AccessibilityInfo",
|
|
155
|
+
"InteractionManager",
|
|
156
|
+
"PanResponder",
|
|
157
|
+
"ToastAndroid",
|
|
158
|
+
"ActionSheetIOS",
|
|
159
|
+
"LogBox",
|
|
160
|
+
"Easing",
|
|
161
|
+
"I18nManager",
|
|
162
|
+
"DeviceEventEmitter",
|
|
163
|
+
"useColorScheme",
|
|
164
|
+
"useWindowDimensions",
|
|
165
|
+
"NativeModules",
|
|
166
|
+
"TurboModuleRegistry",
|
|
167
|
+
"UIManager",
|
|
168
|
+
"NativeEventEmitter",
|
|
169
|
+
"NativeAppEventEmitter",
|
|
170
|
+
"NativeComponentRegistry",
|
|
171
|
+
"requireNativeComponent",
|
|
172
|
+
"AppRegistry",
|
|
173
|
+
"VirtualizedSectionList",
|
|
174
|
+
"Touchable",
|
|
175
|
+
"processColor",
|
|
176
|
+
"findNodeHandle",
|
|
177
|
+
"PlatformColor",
|
|
178
|
+
"DynamicColorIOS",
|
|
179
|
+
"Settings",
|
|
180
|
+
"DeviceInfo",
|
|
181
|
+
"useAnimatedValue",
|
|
182
|
+
"RootTagContext",
|
|
183
|
+
"ReactNativeVersion",
|
|
184
|
+
"Systrace",
|
|
185
|
+
"DevSettings",
|
|
186
|
+
"Networking",
|
|
187
|
+
"unstable_batchedUpdates",
|
|
188
|
+
"registerCallableModule",
|
|
189
|
+
"codegenNativeCommands",
|
|
190
|
+
"codegenNativeComponent",
|
|
191
|
+
"UTFSequence",
|
|
192
|
+
"ProgressBarAndroid",
|
|
193
|
+
"PushNotificationIOS",
|
|
194
|
+
"NativeDialogManagerAndroid",
|
|
195
|
+
"usePressability"
|
|
196
|
+
];
|
|
197
|
+
/** Names of all built-in presets that can be reconstructed in the worker. */
|
|
198
|
+
const BUILT_IN_PRESET_NAMES = new Set([
|
|
199
|
+
"reanimated",
|
|
200
|
+
"gestureHandler",
|
|
201
|
+
"safeAreaContext",
|
|
202
|
+
"navigation",
|
|
203
|
+
"asyncStorage",
|
|
204
|
+
"screens",
|
|
205
|
+
"expo"
|
|
206
|
+
]);
|
|
207
|
+
/** Check if a value (or any nested value) contains functions. */
|
|
208
|
+
function containsFunctions(value, visited = /* @__PURE__ */ new WeakSet()) {
|
|
209
|
+
if (typeof value === "function") return true;
|
|
210
|
+
if (value === null || typeof value !== "object") return false;
|
|
211
|
+
if (visited.has(value)) return false;
|
|
212
|
+
visited.add(value);
|
|
213
|
+
for (const v of Object.values(value)) if (containsFunctions(v, visited)) return true;
|
|
214
|
+
return false;
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Vitest plugin for React Native.
|
|
218
|
+
*
|
|
219
|
+
* Handles platform-specific module resolution, asset stubs, preset virtual
|
|
220
|
+
* modules, and automatic setup-file injection so tests can run against
|
|
221
|
+
* React Native code in a Node/JSDOM environment.
|
|
222
|
+
*/
|
|
223
|
+
function reactNative(options) {
|
|
224
|
+
if (options?.presets) {
|
|
225
|
+
for (const preset of options.presets) if (!BUILT_IN_PRESET_NAMES.has(preset.name)) throw new Error(`[vitest-native] Unknown preset "${preset.name}". Only built-in presets are supported: ${[...BUILT_IN_PRESET_NAMES].join(", ")}. For custom module mocking, use vi.mock() in a setup file.`);
|
|
226
|
+
}
|
|
227
|
+
if (options?.mocks && containsFunctions(options.mocks)) throw new Error("[vitest-native] The \"mocks\" option contains function values, which cannot be transferred to Vitest worker processes. Only JSON-serializable values (strings, numbers, booleans, plain objects, arrays) are supported.\n\nFor function-based mock overrides, use vi.mock() in a setup file:\n\n // vitest.setup.ts\n import { vi } from 'vitest';\n vi.mock('react-native', async (importOriginal) => {\n const actual = await importOriginal();\n return { ...actual, Alert: { alert: vi.fn() } };\n });");
|
|
228
|
+
let resolved;
|
|
229
|
+
const presetModules = /* @__PURE__ */ new Map();
|
|
230
|
+
const presetExportNames = /* @__PURE__ */ new Map();
|
|
231
|
+
let assetPattern;
|
|
232
|
+
const thisDir = path.dirname(fileURLToPath(import.meta.url));
|
|
233
|
+
let setupFilePath = path.resolve(thisDir, "setup.mjs");
|
|
234
|
+
if (!fs.existsSync(setupFilePath)) {
|
|
235
|
+
const srcPath = path.resolve(thisDir, "setup.ts");
|
|
236
|
+
if (fs.existsSync(srcPath)) setupFilePath = srcPath;
|
|
237
|
+
}
|
|
238
|
+
const platform = options?.platform ?? "ios";
|
|
239
|
+
const diagnostics = options?.diagnostics ?? false;
|
|
240
|
+
const extensions = getPlatformExtensions(platform);
|
|
241
|
+
return {
|
|
242
|
+
name: "vitest-native",
|
|
243
|
+
enforce: "pre",
|
|
244
|
+
config(userConfig, _env) {
|
|
245
|
+
const resolvedRoot = userConfig.root ? path.resolve(userConfig.root) : process.cwd();
|
|
246
|
+
const env = {
|
|
247
|
+
VITEST_NATIVE_PLATFORM: platform,
|
|
248
|
+
VITEST_NATIVE_DIAGNOSTICS: String(diagnostics),
|
|
249
|
+
VITEST_NATIVE_PROJECT_ROOT: resolvedRoot
|
|
250
|
+
};
|
|
251
|
+
if (options?.mocks && Object.keys(options.mocks).length > 0) env.VITEST_NATIVE_MOCKS = JSON.stringify(options.mocks);
|
|
252
|
+
if (options?.presets) env.VITEST_NATIVE_PRESET_NAMES = JSON.stringify(options.presets.map((p) => p.name));
|
|
253
|
+
return {
|
|
254
|
+
resolve: {
|
|
255
|
+
extensions,
|
|
256
|
+
conditions: ["react-native"]
|
|
257
|
+
},
|
|
258
|
+
test: {
|
|
259
|
+
setupFiles: [setupFilePath],
|
|
260
|
+
env
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
},
|
|
264
|
+
async configResolved(config) {
|
|
265
|
+
resolved = await resolveOptions(options, config.root);
|
|
266
|
+
for (const preset of resolved.presets) for (const [moduleName, presetModule] of Object.entries(preset.modules)) {
|
|
267
|
+
presetModules.set(moduleName, presetModule.factory);
|
|
268
|
+
presetExportNames.set(moduleName, presetModule.exports);
|
|
269
|
+
}
|
|
270
|
+
assetPattern = new RegExp(`\\.(${resolved.assetExts.join("|")})$`);
|
|
271
|
+
},
|
|
272
|
+
resolveId(source) {
|
|
273
|
+
if (source === "react-native") return "\0virtual:react-native";
|
|
274
|
+
if (source.startsWith("react-native/")) return `\0virtual:rn-subpath:${source}`;
|
|
275
|
+
if (presetModules.has(source)) return `\0virtual:preset:${source}`;
|
|
276
|
+
},
|
|
277
|
+
load(id) {
|
|
278
|
+
if (id === "\0virtual:react-native") return "export default {};";
|
|
279
|
+
if (id.startsWith("\0virtual:rn-subpath:")) return [
|
|
280
|
+
`const _rn = globalThis.__vitest_native_mock || {};`,
|
|
281
|
+
...RN_EXPORT_NAMES.map((n) => `export const ${n} = _rn['${n}'];`),
|
|
282
|
+
`export default _rn;`
|
|
283
|
+
].join("\n");
|
|
284
|
+
if (id.startsWith("\0virtual:preset:")) {
|
|
285
|
+
const moduleName = id.slice(16);
|
|
286
|
+
const exportNames = presetExportNames.get(moduleName) || [];
|
|
287
|
+
return [
|
|
288
|
+
`const _m = (globalThis.__vitest_native_preset_mocks || {})['${moduleName}'] || {};`,
|
|
289
|
+
...exportNames.map((n) => `export const ${n} = _m['${n}'];`),
|
|
290
|
+
`export default _m;`
|
|
291
|
+
].join("\n");
|
|
292
|
+
}
|
|
293
|
+
const fsPath = stripFsPrefix(id);
|
|
294
|
+
if (assetPattern.test(fsPath)) return `export default "${fsPath.split("/").pop() ?? fsPath}";`;
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
//#endregion
|
|
299
|
+
export { presets_exports as presets, reactNative };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
//#region src/preset-map.ts
|
|
2
|
+
/**
|
|
3
|
+
* Map of npm package names to their built-in preset export names.
|
|
4
|
+
* Shared between plugin.ts (Vite main process) and setup.ts (Vitest workers).
|
|
5
|
+
*/
|
|
6
|
+
const AUTO_DETECT_PRESETS = {
|
|
7
|
+
"react-native-reanimated": "reanimated",
|
|
8
|
+
"react-native-gesture-handler": "gestureHandler",
|
|
9
|
+
"react-native-safe-area-context": "safeAreaContext",
|
|
10
|
+
"@react-navigation/native": "navigation",
|
|
11
|
+
"@react-native-async-storage/async-storage": "asyncStorage",
|
|
12
|
+
"react-native-screens": "screens",
|
|
13
|
+
"expo-constants": "expo"
|
|
14
|
+
};
|
|
15
|
+
//#endregion
|
|
16
|
+
Object.defineProperty(exports, "AUTO_DETECT_PRESETS", {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
get: function() {
|
|
19
|
+
return AUTO_DETECT_PRESETS;
|
|
20
|
+
}
|
|
21
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
//#region src/preset-map.ts
|
|
2
|
+
/**
|
|
3
|
+
* Map of npm package names to their built-in preset export names.
|
|
4
|
+
* Shared between plugin.ts (Vite main process) and setup.ts (Vitest workers).
|
|
5
|
+
*/
|
|
6
|
+
const AUTO_DETECT_PRESETS = {
|
|
7
|
+
"react-native-reanimated": "reanimated",
|
|
8
|
+
"react-native-gesture-handler": "gestureHandler",
|
|
9
|
+
"react-native-safe-area-context": "safeAreaContext",
|
|
10
|
+
"@react-navigation/native": "navigation",
|
|
11
|
+
"@react-native-async-storage/async-storage": "asyncStorage",
|
|
12
|
+
"react-native-screens": "screens",
|
|
13
|
+
"expo-constants": "expo"
|
|
14
|
+
};
|
|
15
|
+
//#endregion
|
|
16
|
+
export { AUTO_DETECT_PRESETS as t };
|