vitest-native 0.9.0 → 0.10.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/CHANGELOG.md +731 -0
- package/README.md +32 -1
- package/dist/cli.cjs +163 -27
- package/dist/cli.mjs +162 -26
- package/dist/{ecosystem-EFtHnKS9.mjs → ecosystem-CO5vqExO.mjs} +77 -15
- package/dist/{ecosystem-BIWaVwYL.cjs → ecosystem-IuZ-Gv0R.cjs} +78 -16
- package/dist/errors-Dm5yLNTF.mjs +64 -0
- package/dist/errors-flXDIeWk.cjs +75 -0
- package/dist/errors.d.mts +46 -0
- package/dist/errors.mjs +78 -0
- package/dist/helpers.cjs +6 -23
- package/dist/helpers.d.cts +0 -6
- package/dist/helpers.d.mts +0 -6
- package/dist/helpers.mjs +6 -23
- package/dist/{index-CEgRDdhd.d.cts → index-BJL4cQ-l.d.cts} +9 -4
- package/dist/{index-j-0tueBr.d.mts → index-CvCU0cLl.d.mts} +9 -4
- package/dist/index.d.mts +8 -6
- package/dist/index.mjs +245 -59
- package/dist/jest-compat/interop.mjs +15 -0
- package/dist/jest-compat/setup.mjs +102 -7
- package/dist/matchers.cjs +16 -7
- package/dist/matchers.mjs +16 -7
- package/dist/native/hooks.mjs +75 -1
- package/dist/native/loader.mjs +26 -6
- package/dist/native/module-reset.mjs +63 -0
- package/dist/native/registry.mjs +80 -11
- package/dist/native/resolve.d.mts +22 -0
- package/dist/native/resolve.mjs +21 -13
- package/dist/native/runner.mjs +21 -0
- package/dist/native/setup.mjs +4 -2
- package/dist/native/transform.mjs +108 -3
- package/dist/native/worker.mjs +59 -16
- package/dist/{pool-CJmjRgsB.mjs → pool-Bcxp4Qrg.mjs} +2 -1
- package/dist/{pool-DzsEo9vv.cjs → pool-CFND7plG.cjs} +2 -1
- package/dist/{preset-map-CNsZKfvu.mjs → preset-map-BpDG_D5m.mjs} +0 -1
- package/dist/{preset-map-CG4FMtsl.cjs → preset-map-DxilQ4pI.cjs} +0 -1
- package/dist/{presets-C4faM3ds.cjs → presets-BGTqVH6C.cjs} +94 -50
- package/dist/{presets-CuDrNA_W.mjs → presets-CK85x8ey.mjs} +94 -50
- package/dist/presets.d.mts +1 -1
- package/dist/presets.mjs +1 -1
- package/dist/rntl-matchers.d.ts +35 -0
- package/dist/serializer.cjs +33 -3
- package/dist/serializer.mjs +33 -3
- package/dist/setup.mjs +234 -71
- package/docs/release-readiness.md +22 -5
- package/docs/versioning.md +133 -0
- package/package.json +25 -35
- package/dist/index.cjs +0 -788
- package/dist/index.d.cts +0 -14
- package/dist/presets.cjs +0 -18
- package/dist/presets.d.cts +0 -2
- package/dist/setup.cjs +0 -2702
- package/dist/setup.d.cts +0 -1
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { t as __exportAll } from "./chunk-CjcI7cDX.mjs";
|
|
2
|
+
import { t as VitestNativeError } from "./errors-Dm5yLNTF.mjs";
|
|
2
3
|
import { createRequire } from "node:module";
|
|
3
4
|
import path from "node:path";
|
|
4
5
|
import { vi } from "vitest";
|
|
@@ -47,11 +48,6 @@ function reanimated() {
|
|
|
47
48
|
"addWhitelistedUIProps",
|
|
48
49
|
"addWhitelistedNativeProps",
|
|
49
50
|
"configureProps",
|
|
50
|
-
"View",
|
|
51
|
-
"Text",
|
|
52
|
-
"Image",
|
|
53
|
-
"ScrollView",
|
|
54
|
-
"FlatList",
|
|
55
51
|
"Easing",
|
|
56
52
|
"FadeIn",
|
|
57
53
|
"FadeOut",
|
|
@@ -231,7 +227,7 @@ function reanimated() {
|
|
|
231
227
|
function makeAnimatedHost(name) {
|
|
232
228
|
const Comp = React.forwardRef((props, ref) => {
|
|
233
229
|
const Base = getRN()[name];
|
|
234
|
-
if (!Base) throw new
|
|
230
|
+
if (!Base) throw new VitestNativeError("PRESET_UNAVAILABLE", `The reanimated preset needs React Native's '${name}' component to build Animated.${name}, and react-native did not provide it.\nThis usually means react-native resolved to something unexpected — a partially installed copy, or a mock replacing the whole module. Check that 'react-native' resolves in your project and that no vi.mock replaces it wholesale.`);
|
|
235
231
|
return React.createElement(Base, {
|
|
236
232
|
...props,
|
|
237
233
|
ref
|
|
@@ -278,11 +274,6 @@ function reanimated() {
|
|
|
278
274
|
addWhitelistedUIProps,
|
|
279
275
|
addWhitelistedNativeProps,
|
|
280
276
|
configureProps,
|
|
281
|
-
View,
|
|
282
|
-
Text,
|
|
283
|
-
Image,
|
|
284
|
-
ScrollView,
|
|
285
|
-
FlatList,
|
|
286
277
|
useSharedValue,
|
|
287
278
|
useAnimatedStyle,
|
|
288
279
|
useAnimatedProps,
|
|
@@ -642,10 +633,7 @@ function safeAreaContext() {
|
|
|
642
633
|
"useSafeAreaFrame",
|
|
643
634
|
"withSafeAreaInsets",
|
|
644
635
|
"initialWindowMetrics",
|
|
645
|
-
"initialWindowSafeAreaInsets"
|
|
646
|
-
"EdgeInsets",
|
|
647
|
-
"Rect",
|
|
648
|
-
"Metrics"
|
|
636
|
+
"initialWindowSafeAreaInsets"
|
|
649
637
|
],
|
|
650
638
|
factory: () => {
|
|
651
639
|
const defaultInsets = {
|
|
@@ -1263,12 +1251,29 @@ function screens() {
|
|
|
1263
1251
|
"screensEnabled",
|
|
1264
1252
|
"enableFreeze",
|
|
1265
1253
|
"freezeEnabled",
|
|
1254
|
+
"featureFlags",
|
|
1255
|
+
"compatibilityFlags",
|
|
1256
|
+
"executeNativeBackPress",
|
|
1257
|
+
"isSearchBarAvailableForCurrentPlatform",
|
|
1258
|
+
"useTransitionProgress",
|
|
1266
1259
|
"Screen",
|
|
1260
|
+
"InnerScreen",
|
|
1267
1261
|
"ScreenContainer",
|
|
1262
|
+
"ScreenContentWrapper",
|
|
1263
|
+
"ScreenContext",
|
|
1264
|
+
"ScreenFooter",
|
|
1268
1265
|
"ScreenStack",
|
|
1266
|
+
"ScreenStackItem",
|
|
1269
1267
|
"ScreenStackHeaderConfig",
|
|
1268
|
+
"ScreenStackHeaderBackButtonImage",
|
|
1269
|
+
"ScreenStackHeaderCenterView",
|
|
1270
|
+
"ScreenStackHeaderLeftView",
|
|
1271
|
+
"ScreenStackHeaderRightView",
|
|
1272
|
+
"ScreenStackHeaderSearchBarView",
|
|
1273
|
+
"ScreenStackHeaderSubview",
|
|
1270
1274
|
"SearchBar",
|
|
1271
1275
|
"FullWindowOverlay",
|
|
1276
|
+
"Tabs",
|
|
1272
1277
|
"NativeScreen",
|
|
1273
1278
|
"NativeScreenContainer"
|
|
1274
1279
|
],
|
|
@@ -1299,6 +1304,24 @@ function screens() {
|
|
|
1299
1304
|
const FullWindowOverlay = createScreenComponent("FullWindowOverlay");
|
|
1300
1305
|
const NativeScreen = createScreenComponent("NativeScreen");
|
|
1301
1306
|
const NativeScreenContainer = createScreenComponent("NativeScreenContainer");
|
|
1307
|
+
const InnerScreen = createScreenComponent("InnerScreen");
|
|
1308
|
+
const ScreenContentWrapper = createScreenComponent("ScreenContentWrapper");
|
|
1309
|
+
const ScreenFooter = createScreenComponent("ScreenFooter");
|
|
1310
|
+
const ScreenStackItem = createScreenComponent("ScreenStackItem");
|
|
1311
|
+
const ScreenStackHeaderBackButtonImage = createScreenComponent("ScreenStackHeaderBackButtonImage");
|
|
1312
|
+
const ScreenStackHeaderCenterView = createScreenComponent("ScreenStackHeaderCenterView");
|
|
1313
|
+
const ScreenStackHeaderLeftView = createScreenComponent("ScreenStackHeaderLeftView");
|
|
1314
|
+
const ScreenStackHeaderRightView = createScreenComponent("ScreenStackHeaderRightView");
|
|
1315
|
+
const ScreenStackHeaderSearchBarView = createScreenComponent("ScreenStackHeaderSearchBarView");
|
|
1316
|
+
const ScreenStackHeaderSubview = createScreenComponent("ScreenStackHeaderSubview");
|
|
1317
|
+
const Tabs = createScreenComponent("Tabs");
|
|
1318
|
+
const ScreenContext = React.createContext(Screen);
|
|
1319
|
+
const useTransitionProgress = vi.fn(() => ({
|
|
1320
|
+
progress: 1,
|
|
1321
|
+
closing: 0,
|
|
1322
|
+
goingForward: 0
|
|
1323
|
+
}));
|
|
1324
|
+
const executeNativeBackPress = vi.fn(() => true);
|
|
1302
1325
|
return {
|
|
1303
1326
|
default: {
|
|
1304
1327
|
Screen,
|
|
@@ -1317,7 +1340,27 @@ function screens() {
|
|
|
1317
1340
|
SearchBar,
|
|
1318
1341
|
FullWindowOverlay,
|
|
1319
1342
|
NativeScreen,
|
|
1320
|
-
NativeScreenContainer
|
|
1343
|
+
NativeScreenContainer,
|
|
1344
|
+
InnerScreen,
|
|
1345
|
+
ScreenContentWrapper,
|
|
1346
|
+
ScreenContext,
|
|
1347
|
+
ScreenFooter,
|
|
1348
|
+
ScreenStackItem,
|
|
1349
|
+
ScreenStackHeaderBackButtonImage,
|
|
1350
|
+
ScreenStackHeaderCenterView,
|
|
1351
|
+
ScreenStackHeaderLeftView,
|
|
1352
|
+
ScreenStackHeaderRightView,
|
|
1353
|
+
ScreenStackHeaderSearchBarView,
|
|
1354
|
+
ScreenStackHeaderSubview,
|
|
1355
|
+
Tabs,
|
|
1356
|
+
useTransitionProgress,
|
|
1357
|
+
executeNativeBackPress,
|
|
1358
|
+
isSearchBarAvailableForCurrentPlatform: true,
|
|
1359
|
+
featureFlags: {
|
|
1360
|
+
experiment: {},
|
|
1361
|
+
flags: {}
|
|
1362
|
+
},
|
|
1363
|
+
compatibilityFlags: {}
|
|
1321
1364
|
};
|
|
1322
1365
|
}
|
|
1323
1366
|
} }
|
|
@@ -1331,42 +1374,44 @@ function expo() {
|
|
|
1331
1374
|
modules: {
|
|
1332
1375
|
"expo-constants": {
|
|
1333
1376
|
exports: [
|
|
1334
|
-
"
|
|
1335
|
-
"
|
|
1336
|
-
"
|
|
1337
|
-
"isDevice",
|
|
1338
|
-
"platform",
|
|
1339
|
-
"manifest",
|
|
1340
|
-
"manifest2",
|
|
1341
|
-
"expoGoConfig",
|
|
1342
|
-
"easConfig",
|
|
1343
|
-
"experienceUrl",
|
|
1344
|
-
"linkingUri",
|
|
1345
|
-
"getWebViewUserAgentAsync"
|
|
1377
|
+
"AppOwnership",
|
|
1378
|
+
"ExecutionEnvironment",
|
|
1379
|
+
"UserInterfaceIdiom"
|
|
1346
1380
|
],
|
|
1347
1381
|
factory: () => {
|
|
1348
|
-
const Constants = {
|
|
1349
|
-
expoConfig: {
|
|
1350
|
-
name: "test-app",
|
|
1351
|
-
slug: "test-app",
|
|
1352
|
-
version: "1.0.0",
|
|
1353
|
-
extra: {}
|
|
1354
|
-
},
|
|
1355
|
-
executionEnvironment: "storeClient",
|
|
1356
|
-
appOwnership: "standalone",
|
|
1357
|
-
isDevice: true,
|
|
1358
|
-
platform: {},
|
|
1359
|
-
manifest: null,
|
|
1360
|
-
manifest2: null,
|
|
1361
|
-
expoGoConfig: null,
|
|
1362
|
-
easConfig: null,
|
|
1363
|
-
experienceUrl: "",
|
|
1364
|
-
linkingUri: "",
|
|
1365
|
-
getWebViewUserAgentAsync: vi.fn(async () => "test-user-agent")
|
|
1366
|
-
};
|
|
1367
1382
|
return {
|
|
1368
|
-
default:
|
|
1369
|
-
|
|
1383
|
+
default: {
|
|
1384
|
+
expoConfig: {
|
|
1385
|
+
name: "test-app",
|
|
1386
|
+
slug: "test-app",
|
|
1387
|
+
version: "1.0.0",
|
|
1388
|
+
extra: {}
|
|
1389
|
+
},
|
|
1390
|
+
executionEnvironment: "storeClient",
|
|
1391
|
+
appOwnership: "standalone",
|
|
1392
|
+
isDevice: true,
|
|
1393
|
+
platform: {},
|
|
1394
|
+
manifest: null,
|
|
1395
|
+
manifest2: null,
|
|
1396
|
+
expoGoConfig: null,
|
|
1397
|
+
easConfig: null,
|
|
1398
|
+
experienceUrl: "",
|
|
1399
|
+
linkingUri: "",
|
|
1400
|
+
getWebViewUserAgentAsync: vi.fn(async () => "test-user-agent")
|
|
1401
|
+
},
|
|
1402
|
+
AppOwnership: { Expo: "expo" },
|
|
1403
|
+
ExecutionEnvironment: {
|
|
1404
|
+
Bare: "bare",
|
|
1405
|
+
Standalone: "standalone",
|
|
1406
|
+
StoreClient: "storeClient"
|
|
1407
|
+
},
|
|
1408
|
+
UserInterfaceIdiom: {
|
|
1409
|
+
Handset: "handset",
|
|
1410
|
+
Tablet: "tablet",
|
|
1411
|
+
Desktop: "desktop",
|
|
1412
|
+
TV: "tv",
|
|
1413
|
+
Unsupported: "unsupported"
|
|
1414
|
+
}
|
|
1370
1415
|
};
|
|
1371
1416
|
}
|
|
1372
1417
|
},
|
|
@@ -1828,7 +1873,6 @@ function flashList() {
|
|
|
1828
1873
|
"FlashList",
|
|
1829
1874
|
"AnimatedFlashList",
|
|
1830
1875
|
"LayoutCommitObserver",
|
|
1831
|
-
"ViewToken",
|
|
1832
1876
|
"RenderTargetOptions",
|
|
1833
1877
|
"useRecyclingState",
|
|
1834
1878
|
"useLayoutState",
|
package/dist/presets.d.mts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { _ as reanimated, a as flashList, c as svg, d as expo, f as screens, g as gestureHandler, h as safeAreaContext, i as bottomSheet, l as mmkv, m as navigation, n as worklets, o as vectorIcons, p as asyncStorage, r as keyboardController, s as webview, u as deviceInfo } from "./index-
|
|
1
|
+
import { _ as reanimated, a as flashList, c as svg, d as expo, f as screens, g as gestureHandler, h as safeAreaContext, i as bottomSheet, l as mmkv, m as navigation, n as worklets, o as vectorIcons, p as asyncStorage, r as keyboardController, s as webview, u as deviceInfo } from "./index-CvCU0cLl.mjs";
|
|
2
2
|
export { asyncStorage, bottomSheet, deviceInfo, expo, flashList, gestureHandler, keyboardController, mmkv, navigation, reanimated, safeAreaContext, screens, svg, vectorIcons, webview, worklets };
|
package/dist/presets.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { C as navigation, O as reanimated, S as asyncStorage, T as gestureHandler, _ as svg, a as bottomSheet, b as expo, g as webview, h as vectorIcons, m as flashList, n as worklets, r as keyboardController, v as mmkv, w as safeAreaContext, x as screens, y as deviceInfo } from "./presets-
|
|
1
|
+
import { C as navigation, O as reanimated, S as asyncStorage, T as gestureHandler, _ as svg, a as bottomSheet, b as expo, g as webview, h as vectorIcons, m as flashList, n as worklets, r as keyboardController, v as mmkv, w as safeAreaContext, x as screens, y as deviceInfo } from "./presets-CK85x8ey.mjs";
|
|
2
2
|
export { asyncStorage, bottomSheet, deviceInfo, expo, flashList, gestureHandler, keyboardController, mmkv, navigation, reanimated, safeAreaContext, screens, svg, vectorIcons, webview, worklets };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Opt-in types for React Native Testing Library's matchers under Vitest.
|
|
3
|
+
*
|
|
4
|
+
* RNTL's matchers work at runtime under this plugin, but it declares their types only
|
|
5
|
+
* for Jest — `dist/matchers/types.d.ts` augments the global `jest` namespace and the
|
|
6
|
+
* `@jest/expect` module, and neither reaches Vitest's `Assertion`. Without this,
|
|
7
|
+
* `toHaveTextContent`, `toHaveStyle`, `toBeVisible` and the rest are
|
|
8
|
+
* `Property 'x' does not exist on type 'Assertion<...>'` for anyone who typechecks.
|
|
9
|
+
*
|
|
10
|
+
* Reference it once, anywhere in the project:
|
|
11
|
+
*
|
|
12
|
+
* /// <reference types="vitest-native/rntl-matchers" />
|
|
13
|
+
*
|
|
14
|
+
* or add "vitest-native/rntl-matchers" to `compilerOptions.types` in tsconfig.json.
|
|
15
|
+
*
|
|
16
|
+
* SEPARATE, rather than folded into the package's main types, because
|
|
17
|
+
* @testing-library/react-native is an OPTIONAL peer. An unresolvable type import
|
|
18
|
+
* inside a shipped .d.ts is invisible under `skipLibCheck: true` — React Native's own
|
|
19
|
+
* tsconfig default — but reports TS2307 under `skipLibCheck: false`, which would break
|
|
20
|
+
* projects that use the mock engine without RNTL. Measured both ways before choosing
|
|
21
|
+
* this shape. @testing-library/jest-dom ships its `/vitest` entry for the same reason.
|
|
22
|
+
*
|
|
23
|
+
* The import is a deep path because RNTL does not re-export the interface from its
|
|
24
|
+
* entry point. It has no `exports` map, so the path resolves; if a future RNTL moves
|
|
25
|
+
* it, this file fails loudly at the reference site rather than silently dropping the
|
|
26
|
+
* matchers.
|
|
27
|
+
*/
|
|
28
|
+
import type { JestNativeMatchers } from "@testing-library/react-native/dist/matchers/types";
|
|
29
|
+
|
|
30
|
+
declare module "vitest" {
|
|
31
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
|
|
32
|
+
interface Assertion<T = any> extends JestNativeMatchers<T> {}
|
|
33
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
|
|
34
|
+
interface AsymmetricMatchersContaining extends JestNativeMatchers<void> {}
|
|
35
|
+
}
|
package/dist/serializer.cjs
CHANGED
|
@@ -45,6 +45,37 @@ function getTypeName(type) {
|
|
|
45
45
|
return "Unknown";
|
|
46
46
|
}
|
|
47
47
|
/**
|
|
48
|
+
* JSON-ish rendering of a prop value, for the cases `JSON.stringify` gets wrong here.
|
|
49
|
+
*
|
|
50
|
+
* - Cycles print as `"[Circular]"` instead of throwing. A prop holding a navigation
|
|
51
|
+
* object, a store, or anything with a parent back-reference made the serializer
|
|
52
|
+
* raise `Converting circular structure to JSON`, which fails the test with a
|
|
53
|
+
* TypeError instead of producing a snapshot.
|
|
54
|
+
* - Object keys are sorted, so two structurally equal props serialize identically.
|
|
55
|
+
* Unsorted, `{ a: 1, b: 2 }` and `{ b: 2, a: 1 }` produced different snapshots and
|
|
56
|
+
* churned on a rewrite that changed nothing. Prop KEYS were already sorted below;
|
|
57
|
+
* this extends the same rule inside values. Arrays keep their order, which is
|
|
58
|
+
* meaningful.
|
|
59
|
+
* - Functions and `undefined` are rendered rather than dropped. `JSON.stringify`
|
|
60
|
+
* silently removes them, so `{ onPress: fn }` printed as `{}` — an empty object
|
|
61
|
+
* that reads like missing data.
|
|
62
|
+
*/
|
|
63
|
+
function stableStringify(value, seen = /* @__PURE__ */ new Set()) {
|
|
64
|
+
if (value === null) return "null";
|
|
65
|
+
if (typeof value === "function") return `"[Function ${value.name || "anonymous"}]"`;
|
|
66
|
+
if (typeof value === "undefined") return `"[undefined]"`;
|
|
67
|
+
if (typeof value !== "object") return JSON.stringify(value) ?? "null";
|
|
68
|
+
const obj = value;
|
|
69
|
+
if (seen.has(obj)) return `"[Circular]"`;
|
|
70
|
+
seen.add(obj);
|
|
71
|
+
try {
|
|
72
|
+
if (Array.isArray(obj)) return `[${obj.map((entry) => stableStringify(entry, seen)).join(",")}]`;
|
|
73
|
+
return `{${Object.keys(obj).sort().map((key) => `${JSON.stringify(key)}:${stableStringify(obj[key], seen)}`).join(",")}}`;
|
|
74
|
+
} finally {
|
|
75
|
+
seen.delete(obj);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
48
79
|
* Format a single prop value for snapshot display.
|
|
49
80
|
*/
|
|
50
81
|
function formatPropValue(value) {
|
|
@@ -53,8 +84,7 @@ function formatPropValue(value) {
|
|
|
53
84
|
if (typeof value === "function") return `{[Function ${value.name || "anonymous"}]}`;
|
|
54
85
|
if (value === null) return "{null}";
|
|
55
86
|
if (value === void 0) return "{undefined}";
|
|
56
|
-
if (Array.isArray(value)) return `{${
|
|
57
|
-
if (typeof value === "object") return `{${JSON.stringify(value)}}`;
|
|
87
|
+
if (Array.isArray(value) || typeof value === "object") return `{${stableStringify(value)}}`;
|
|
58
88
|
return `{${String(value)}}`;
|
|
59
89
|
}
|
|
60
90
|
/**
|
|
@@ -108,7 +138,7 @@ const serializer = {
|
|
|
108
138
|
if (typeof child === "string") result += `\n${nextIndentation}${child}`;
|
|
109
139
|
else if (typeof child === "number") result += `\n${nextIndentation}${String(child)}`;
|
|
110
140
|
else if (isReactTestInstance(child)) result += "\n" + printer(child, config, nextIndentation, depth + 1, refs);
|
|
111
|
-
else result += "\n" +
|
|
141
|
+
else result += "\n" + printer(child, config, nextIndentation, depth + 1, refs);
|
|
112
142
|
}
|
|
113
143
|
result += `\n${indentation}</${typeName}>`;
|
|
114
144
|
return result;
|
package/dist/serializer.mjs
CHANGED
|
@@ -44,6 +44,37 @@ function getTypeName(type) {
|
|
|
44
44
|
return "Unknown";
|
|
45
45
|
}
|
|
46
46
|
/**
|
|
47
|
+
* JSON-ish rendering of a prop value, for the cases `JSON.stringify` gets wrong here.
|
|
48
|
+
*
|
|
49
|
+
* - Cycles print as `"[Circular]"` instead of throwing. A prop holding a navigation
|
|
50
|
+
* object, a store, or anything with a parent back-reference made the serializer
|
|
51
|
+
* raise `Converting circular structure to JSON`, which fails the test with a
|
|
52
|
+
* TypeError instead of producing a snapshot.
|
|
53
|
+
* - Object keys are sorted, so two structurally equal props serialize identically.
|
|
54
|
+
* Unsorted, `{ a: 1, b: 2 }` and `{ b: 2, a: 1 }` produced different snapshots and
|
|
55
|
+
* churned on a rewrite that changed nothing. Prop KEYS were already sorted below;
|
|
56
|
+
* this extends the same rule inside values. Arrays keep their order, which is
|
|
57
|
+
* meaningful.
|
|
58
|
+
* - Functions and `undefined` are rendered rather than dropped. `JSON.stringify`
|
|
59
|
+
* silently removes them, so `{ onPress: fn }` printed as `{}` — an empty object
|
|
60
|
+
* that reads like missing data.
|
|
61
|
+
*/
|
|
62
|
+
function stableStringify(value, seen = /* @__PURE__ */ new Set()) {
|
|
63
|
+
if (value === null) return "null";
|
|
64
|
+
if (typeof value === "function") return `"[Function ${value.name || "anonymous"}]"`;
|
|
65
|
+
if (typeof value === "undefined") return `"[undefined]"`;
|
|
66
|
+
if (typeof value !== "object") return JSON.stringify(value) ?? "null";
|
|
67
|
+
const obj = value;
|
|
68
|
+
if (seen.has(obj)) return `"[Circular]"`;
|
|
69
|
+
seen.add(obj);
|
|
70
|
+
try {
|
|
71
|
+
if (Array.isArray(obj)) return `[${obj.map((entry) => stableStringify(entry, seen)).join(",")}]`;
|
|
72
|
+
return `{${Object.keys(obj).sort().map((key) => `${JSON.stringify(key)}:${stableStringify(obj[key], seen)}`).join(",")}}`;
|
|
73
|
+
} finally {
|
|
74
|
+
seen.delete(obj);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
47
78
|
* Format a single prop value for snapshot display.
|
|
48
79
|
*/
|
|
49
80
|
function formatPropValue(value) {
|
|
@@ -52,8 +83,7 @@ function formatPropValue(value) {
|
|
|
52
83
|
if (typeof value === "function") return `{[Function ${value.name || "anonymous"}]}`;
|
|
53
84
|
if (value === null) return "{null}";
|
|
54
85
|
if (value === void 0) return "{undefined}";
|
|
55
|
-
if (Array.isArray(value)) return `{${
|
|
56
|
-
if (typeof value === "object") return `{${JSON.stringify(value)}}`;
|
|
86
|
+
if (Array.isArray(value) || typeof value === "object") return `{${stableStringify(value)}}`;
|
|
57
87
|
return `{${String(value)}}`;
|
|
58
88
|
}
|
|
59
89
|
/**
|
|
@@ -107,7 +137,7 @@ const serializer = {
|
|
|
107
137
|
if (typeof child === "string") result += `\n${nextIndentation}${child}`;
|
|
108
138
|
else if (typeof child === "number") result += `\n${nextIndentation}${String(child)}`;
|
|
109
139
|
else if (isReactTestInstance(child)) result += "\n" + printer(child, config, nextIndentation, depth + 1, refs);
|
|
110
|
-
else result += "\n" +
|
|
140
|
+
else result += "\n" + printer(child, config, nextIndentation, depth + 1, refs);
|
|
111
141
|
}
|
|
112
142
|
result += `\n${indentation}</${typeName}>`;
|
|
113
143
|
return result;
|