@terreno/test 57.17.0 → 57.19.2
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 +1681 -0
- package/README.md +6 -0
- package/dist/assertions/expectAPIError.d.ts +18 -0
- package/dist/assertions/expectAPIError.js +33 -0
- package/dist/assertions/strictResponseProperties.d.ts +14 -0
- package/dist/assertions/strictResponseProperties.js +37 -0
- package/dist/cache/mongoTestCache.js +2 -2
- package/dist/cli/collectFiles.d.ts +38 -0
- package/dist/cli/collectFiles.js +73 -0
- package/dist/cli/testHygiene.d.ts +134 -0
- package/dist/cli/testHygiene.js +357 -0
- package/dist/cli/testHygieneCli.d.ts +2 -0
- package/dist/cli/testHygieneCli.js +5 -0
- package/dist/cli/testHygieneRules.d.ts +58 -0
- package/dist/cli/testHygieneRules.js +517 -0
- package/dist/cli/testShard.d.ts +155 -0
- package/dist/cli/testShard.js +377 -0
- package/dist/cli/testShardCli.d.ts +2 -0
- package/dist/cli/testShardCli.js +5 -0
- package/dist/expo/authTokenStorage.d.ts +76 -0
- package/dist/expo/authTokenStorage.js +160 -0
- package/dist/expo/expoModuleMocks.d.ts +24 -0
- package/dist/expo/expoModuleMocks.js +456 -0
- package/dist/expo/libraryModuleMocks.d.ts +17 -0
- package/dist/expo/libraryModuleMocks.js +347 -0
- package/dist/expo/mockHelpers.d.ts +39 -0
- package/dist/expo/mockHelpers.js +45 -0
- package/dist/expo/mockModuleAliases.d.ts +50 -0
- package/dist/expo/mockModuleAliases.js +174 -0
- package/dist/expo/reactNativeMock.d.ts +39 -0
- package/dist/expo/reactNativeMock.js +421 -0
- package/dist/expo/registerExpoTestEnvironment.d.ts +69 -0
- package/dist/expo/registerExpoTestEnvironment.js +163 -0
- package/dist/expo/renderWithTerreno.d.ts +47 -0
- package/dist/expo/renderWithTerreno.js +47 -0
- package/dist/http/authWithToken.d.ts +20 -0
- package/dist/http/authWithToken.js +30 -0
- package/dist/index.d.ts +14 -1
- package/dist/index.js +33 -1
- package/dist/logging/silenceLogs.js +0 -2
- package/dist/mocks/aiSdk.d.ts +77 -0
- package/dist/mocks/aiSdk.js +102 -0
- package/dist/mocks/cloudTasks.d.ts +91 -0
- package/dist/mocks/cloudTasks.js +197 -0
- package/dist/mocks/expoPush.d.ts +53 -0
- package/dist/mocks/expoPush.js +57 -0
- package/dist/mocks/gcs.d.ts +101 -0
- package/dist/mocks/gcs.js +162 -0
- package/dist/mocks/googleAuth.d.ts +69 -0
- package/dist/mocks/googleAuth.js +55 -0
- package/dist/mocks/mockGroup.d.ts +11 -0
- package/dist/mocks/mockGroup.js +21 -0
- package/dist/mocks/notifiers.d.ts +21 -0
- package/dist/mocks/notifiers.js +26 -0
- package/dist/mocks/sendgrid.d.ts +22 -0
- package/dist/mocks/sendgrid.js +32 -0
- package/dist/mocks/twilio.d.ts +72 -0
- package/dist/mocks/twilio.js +64 -0
- package/dist/preload/registerBackendPreload.js +4 -1
- package/dist/time/freezeTime.d.ts +16 -0
- package/dist/time/freezeTime.js +49 -0
- package/dist/transaction/testTransaction.d.ts +43 -6
- package/dist/transaction/testTransaction.js +371 -92
- package/dist/utils/waitUntil.d.ts +22 -0
- package/dist/utils/waitUntil.js +34 -0
- package/package.json +53 -8
- package/src/assertions/expectAPIError.ts +51 -0
- package/src/assertions/strictResponseProperties.ts +42 -0
- package/src/cache/mongoTestCache.ts +2 -2
- package/src/cli/collectFiles.ts +115 -0
- package/src/cli/testHygiene.ts +539 -0
- package/src/cli/testHygieneCli.ts +4 -0
- package/src/cli/testHygieneRules.ts +719 -0
- package/src/cli/testShard.ts +532 -0
- package/src/cli/testShardCli.ts +4 -0
- package/src/expo/authTokenStorage.ts +198 -0
- package/src/expo/expoModuleMocks.ts +503 -0
- package/src/expo/libraryModuleMocks.ts +411 -0
- package/src/expo/mockHelpers.ts +73 -0
- package/src/expo/mockModuleAliases.ts +252 -0
- package/src/expo/reactNativeMock.ts +534 -0
- package/src/expo/registerExpoTestEnvironment.ts +260 -0
- package/src/expo/renderWithTerreno.ts +79 -0
- package/src/http/authWithToken.ts +38 -0
- package/src/index.ts +82 -1
- package/src/logging/silenceLogs.ts +0 -2
- package/src/mocks/aiSdk.ts +199 -0
- package/src/mocks/cloudTasks.ts +323 -0
- package/src/mocks/expoPush.ts +134 -0
- package/src/mocks/gcs.ts +305 -0
- package/src/mocks/googleAuth.ts +136 -0
- package/src/mocks/mockGroup.ts +32 -0
- package/src/mocks/notifiers.ts +36 -0
- package/src/mocks/sendgrid.ts +57 -0
- package/src/mocks/twilio.ts +143 -0
- package/src/preload/registerBackendPreload.ts +4 -1
- package/src/time/freezeTime.ts +61 -0
- package/src/transaction/testTransaction.ts +456 -124
- package/src/utils/waitUntil.ts +47 -0
- package/biome.jsonc +0 -20
- package/bunfig.toml +0 -16
- package/src/cache/mongoTestCache.test.ts +0 -103
- package/src/env/setTerrenoTestEnv.test.ts +0 -26
- package/src/http/authAsUser.test.ts +0 -40
- package/src/http/getBaseServer.test.ts +0 -45
- package/src/logging/setupEnvironment.test.ts +0 -24
- package/src/logging/silenceLogs.test.ts +0 -52
- package/src/logging/testLogger.test.ts +0 -58
- package/src/mocks/sentryBun.test.ts +0 -230
- package/src/mongo/connection.test.ts +0 -43
- package/src/mongo/mongoServer.test.ts +0 -99
- package/src/preload/registerBackendPreload.test.ts +0 -10
- package/src/transaction/testTransaction.test.ts +0 -64
- package/src/utils/helpers.test.ts +0 -46
- package/tsconfig.json +0 -18
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createLibraryModuleMocks = exports.interpolate = exports.MOCK_NET_INFO_STATE = void 0;
|
|
4
|
+
const bun_test_1 = require("bun:test");
|
|
5
|
+
const react_1 = require("react");
|
|
6
|
+
const mockHelpers_1 = require("./mockHelpers");
|
|
7
|
+
/** Safe-area insets every safe-area hook and context reports. */
|
|
8
|
+
const MOCK_SAFE_AREA_INSETS = { bottom: 0, left: 0, right: 0, top: 0 };
|
|
9
|
+
/** Safe-area frame every safe-area hook and context reports. */
|
|
10
|
+
const MOCK_SAFE_AREA_FRAME = { height: 812, width: 375, x: 0, y: 0 };
|
|
11
|
+
/** Network state `@react-native-community/netinfo` reports: online over Wi-Fi. */
|
|
12
|
+
exports.MOCK_NET_INFO_STATE = {
|
|
13
|
+
details: {},
|
|
14
|
+
isConnected: true,
|
|
15
|
+
isInternetReachable: true,
|
|
16
|
+
isWifiEnabled: true,
|
|
17
|
+
type: "wifi",
|
|
18
|
+
};
|
|
19
|
+
const GESTURE_METHODS = [
|
|
20
|
+
"activeOffsetX",
|
|
21
|
+
"activeOffsetY",
|
|
22
|
+
"averageTouches",
|
|
23
|
+
"blocksExternalGesture",
|
|
24
|
+
"cancelsTouchesInView",
|
|
25
|
+
"direction",
|
|
26
|
+
"enabled",
|
|
27
|
+
"failOffsetX",
|
|
28
|
+
"failOffsetY",
|
|
29
|
+
"hitSlop",
|
|
30
|
+
"manualActivation",
|
|
31
|
+
"maxDelay",
|
|
32
|
+
"maxDist",
|
|
33
|
+
"maxDuration",
|
|
34
|
+
"maxPointers",
|
|
35
|
+
"minDistance",
|
|
36
|
+
"minDuration",
|
|
37
|
+
"minPointers",
|
|
38
|
+
"minVelocity",
|
|
39
|
+
"minVelocityX",
|
|
40
|
+
"minVelocityY",
|
|
41
|
+
"numberOfPointers",
|
|
42
|
+
"numberOfTaps",
|
|
43
|
+
"onBegin",
|
|
44
|
+
"onChange",
|
|
45
|
+
"onEnd",
|
|
46
|
+
"onFinalize",
|
|
47
|
+
"onStart",
|
|
48
|
+
"onTouchesCancelled",
|
|
49
|
+
"onTouchesDown",
|
|
50
|
+
"onTouchesMove",
|
|
51
|
+
"onTouchesUp",
|
|
52
|
+
"onUpdate",
|
|
53
|
+
"requireExternalGestureToFail",
|
|
54
|
+
"runOnJS",
|
|
55
|
+
"shouldCancelWhenOutside",
|
|
56
|
+
"simultaneousWithExternalGesture",
|
|
57
|
+
"withRef",
|
|
58
|
+
"withTestId",
|
|
59
|
+
];
|
|
60
|
+
const createChainableGesture = () => {
|
|
61
|
+
const gesture = {};
|
|
62
|
+
for (const method of GESTURE_METHODS) {
|
|
63
|
+
gesture[method] = (0, bun_test_1.mock)(() => gesture);
|
|
64
|
+
}
|
|
65
|
+
return gesture;
|
|
66
|
+
};
|
|
67
|
+
const createSharedValue = (initialValue) => {
|
|
68
|
+
const shared = {
|
|
69
|
+
get: () => shared.value,
|
|
70
|
+
modify: (modifier) => {
|
|
71
|
+
shared.value = modifier(shared.value);
|
|
72
|
+
return shared.value;
|
|
73
|
+
},
|
|
74
|
+
set: (value) => {
|
|
75
|
+
shared.value = value;
|
|
76
|
+
},
|
|
77
|
+
value: initialValue,
|
|
78
|
+
};
|
|
79
|
+
return shared;
|
|
80
|
+
};
|
|
81
|
+
/** Linear interpolation clamped to the first/last range entries, like Reanimated's default. */
|
|
82
|
+
const interpolate = (value, inputRange, outputRange) => {
|
|
83
|
+
const inMin = inputRange[0] ?? 0;
|
|
84
|
+
const inMax = inputRange[inputRange.length - 1] ?? inMin;
|
|
85
|
+
const outMin = outputRange[0] ?? 0;
|
|
86
|
+
const outMax = outputRange[outputRange.length - 1] ?? outMin;
|
|
87
|
+
if (inMax === inMin) {
|
|
88
|
+
return outMin;
|
|
89
|
+
}
|
|
90
|
+
return outMin + ((value - inMin) / (inMax - inMin)) * (outMax - outMin);
|
|
91
|
+
};
|
|
92
|
+
exports.interpolate = interpolate;
|
|
93
|
+
const createReanimatedMock = () => {
|
|
94
|
+
const Animated = {
|
|
95
|
+
createAnimatedComponent: (0, mockHelpers_1.stubIdentity)(),
|
|
96
|
+
FlatList: (0, mockHelpers_1.hostComponent)("FlatList"),
|
|
97
|
+
Image: (0, mockHelpers_1.hostComponent)("Image"),
|
|
98
|
+
ScrollView: (0, mockHelpers_1.hostComponent)("ScrollView"),
|
|
99
|
+
Text: (0, mockHelpers_1.hostComponent)("Text"),
|
|
100
|
+
View: (0, mockHelpers_1.hostComponent)("View"),
|
|
101
|
+
};
|
|
102
|
+
const layoutAnimation = () => {
|
|
103
|
+
const animation = {};
|
|
104
|
+
for (const method of ["delay", "duration", "easing", "springify", "damping", "withCallback"]) {
|
|
105
|
+
animation[method] = (0, bun_test_1.mock)(() => animation);
|
|
106
|
+
}
|
|
107
|
+
return animation;
|
|
108
|
+
};
|
|
109
|
+
return {
|
|
110
|
+
...Animated,
|
|
111
|
+
cancelAnimation: (0, mockHelpers_1.stub)(),
|
|
112
|
+
default: Animated,
|
|
113
|
+
Easing: {
|
|
114
|
+
bezier: (0, mockHelpers_1.stubReturning)((0, mockHelpers_1.stubIdentity)()),
|
|
115
|
+
cubic: (0, mockHelpers_1.stubIdentity)(),
|
|
116
|
+
ease: (0, mockHelpers_1.stubIdentity)(),
|
|
117
|
+
in: (0, mockHelpers_1.stubIdentity)(),
|
|
118
|
+
inOut: (0, mockHelpers_1.stubIdentity)(),
|
|
119
|
+
linear: (0, mockHelpers_1.stubIdentity)(),
|
|
120
|
+
out: (0, mockHelpers_1.stubIdentity)(),
|
|
121
|
+
quad: (0, mockHelpers_1.stubIdentity)(),
|
|
122
|
+
},
|
|
123
|
+
Extrapolation: { CLAMP: "clamp", EXTEND: "extend", IDENTITY: "identity" },
|
|
124
|
+
FadeIn: layoutAnimation(),
|
|
125
|
+
FadeOut: layoutAnimation(),
|
|
126
|
+
interpolate: (0, bun_test_1.mock)(exports.interpolate),
|
|
127
|
+
interpolateColor: (0, bun_test_1.mock)((_value, _input, output) => output[0]),
|
|
128
|
+
Layout: layoutAnimation(),
|
|
129
|
+
LinearTransition: layoutAnimation(),
|
|
130
|
+
makeMutable: (0, bun_test_1.mock)(createSharedValue),
|
|
131
|
+
ReduceMotion: { Always: "always", Never: "never", System: "system" },
|
|
132
|
+
runOnJS: (0, mockHelpers_1.stubIdentity)(),
|
|
133
|
+
runOnUI: (0, mockHelpers_1.stubIdentity)(),
|
|
134
|
+
SlideInDown: layoutAnimation(),
|
|
135
|
+
SlideOutDown: layoutAnimation(),
|
|
136
|
+
useAnimatedGestureHandler: (0, mockHelpers_1.stubReturning)({}),
|
|
137
|
+
useAnimatedProps: (0, bun_test_1.mock)((updater) => updater()),
|
|
138
|
+
useAnimatedReaction: (0, mockHelpers_1.stub)(),
|
|
139
|
+
useAnimatedRef: (0, mockHelpers_1.stubReturning)({ current: null }),
|
|
140
|
+
useAnimatedScrollHandler: (0, mockHelpers_1.stubReturning)((0, mockHelpers_1.stub)()),
|
|
141
|
+
useAnimatedStyle: (0, bun_test_1.mock)((updater) => updater()),
|
|
142
|
+
useDerivedValue: (0, bun_test_1.mock)((updater) => createSharedValue(updater())),
|
|
143
|
+
useReducedMotion: (0, mockHelpers_1.stubReturning)(false),
|
|
144
|
+
useSharedValue: (0, bun_test_1.mock)(createSharedValue),
|
|
145
|
+
withDecay: (0, mockHelpers_1.stubIdentity)(),
|
|
146
|
+
withDelay: (0, bun_test_1.mock)((_delay, value) => value),
|
|
147
|
+
withRepeat: (0, mockHelpers_1.stubIdentity)(),
|
|
148
|
+
withSequence: (0, bun_test_1.mock)((...values) => values[0]),
|
|
149
|
+
withSpring: (0, mockHelpers_1.stubIdentity)(),
|
|
150
|
+
withTiming: (0, mockHelpers_1.stubIdentity)(),
|
|
151
|
+
ZoomIn: layoutAnimation(),
|
|
152
|
+
ZoomOut: layoutAnimation(),
|
|
153
|
+
};
|
|
154
|
+
};
|
|
155
|
+
const createGestureHandlerMock = () => {
|
|
156
|
+
const gesture = (0, bun_test_1.mock)(createChainableGesture);
|
|
157
|
+
return {
|
|
158
|
+
...(0, mockHelpers_1.hostComponents)([
|
|
159
|
+
"BaseButton",
|
|
160
|
+
"BorderlessButton",
|
|
161
|
+
"DrawerLayout",
|
|
162
|
+
"FlatList",
|
|
163
|
+
"GestureDetector",
|
|
164
|
+
"LongPressGestureHandler",
|
|
165
|
+
"PanGestureHandler",
|
|
166
|
+
"PinchGestureHandler",
|
|
167
|
+
"RectButton",
|
|
168
|
+
"ScrollView",
|
|
169
|
+
"Swipeable",
|
|
170
|
+
"TapGestureHandler",
|
|
171
|
+
"TouchableHighlight",
|
|
172
|
+
"TouchableOpacity",
|
|
173
|
+
"TouchableWithoutFeedback",
|
|
174
|
+
]),
|
|
175
|
+
createNativeWrapper: (0, mockHelpers_1.stubIdentity)(),
|
|
176
|
+
Directions: { DOWN: 8, LEFT: 2, RIGHT: 1, UP: 4 },
|
|
177
|
+
Gesture: {
|
|
178
|
+
Exclusive: gesture,
|
|
179
|
+
Fling: gesture,
|
|
180
|
+
Hover: gesture,
|
|
181
|
+
LongPress: gesture,
|
|
182
|
+
Manual: gesture,
|
|
183
|
+
Native: gesture,
|
|
184
|
+
Pan: gesture,
|
|
185
|
+
Pinch: gesture,
|
|
186
|
+
Race: gesture,
|
|
187
|
+
Rotation: gesture,
|
|
188
|
+
Simultaneous: gesture,
|
|
189
|
+
Tap: gesture,
|
|
190
|
+
},
|
|
191
|
+
GestureHandlerRootView: (0, mockHelpers_1.hostComponent)("GestureHandlerRootView"),
|
|
192
|
+
gestureHandlerRootHOC: (0, mockHelpers_1.stubIdentity)(),
|
|
193
|
+
State: { ACTIVE: 4, BEGAN: 2, CANCELLED: 3, END: 5, FAILED: 1, UNDETERMINED: 0 },
|
|
194
|
+
};
|
|
195
|
+
};
|
|
196
|
+
const SVG_ELEMENTS = [
|
|
197
|
+
"Circle",
|
|
198
|
+
"ClipPath",
|
|
199
|
+
"Defs",
|
|
200
|
+
"Ellipse",
|
|
201
|
+
"ForeignObject",
|
|
202
|
+
"G",
|
|
203
|
+
"Line",
|
|
204
|
+
"LinearGradient",
|
|
205
|
+
"Marker",
|
|
206
|
+
"Mask",
|
|
207
|
+
"Path",
|
|
208
|
+
"Pattern",
|
|
209
|
+
"Polygon",
|
|
210
|
+
"Polyline",
|
|
211
|
+
"RadialGradient",
|
|
212
|
+
"Rect",
|
|
213
|
+
"Stop",
|
|
214
|
+
"Svg",
|
|
215
|
+
"Symbol",
|
|
216
|
+
"TextPath",
|
|
217
|
+
"TSpan",
|
|
218
|
+
"Use",
|
|
219
|
+
];
|
|
220
|
+
const createSvgMock = () => {
|
|
221
|
+
const elements = (0, mockHelpers_1.hostComponents)(SVG_ELEMENTS);
|
|
222
|
+
return {
|
|
223
|
+
...elements,
|
|
224
|
+
default: elements.Svg,
|
|
225
|
+
Image: (0, mockHelpers_1.hostComponent)("SvgImage"),
|
|
226
|
+
SvgXml: (0, mockHelpers_1.hostComponent)("SvgXml"),
|
|
227
|
+
Text: (0, mockHelpers_1.hostComponent)("SvgText"),
|
|
228
|
+
};
|
|
229
|
+
};
|
|
230
|
+
const createSafeAreaMock = () => {
|
|
231
|
+
const metrics = { frame: MOCK_SAFE_AREA_FRAME, insets: MOCK_SAFE_AREA_INSETS };
|
|
232
|
+
const SafeAreaInsetsContext = (0, react_1.createContext)(MOCK_SAFE_AREA_INSETS);
|
|
233
|
+
const SafeAreaFrameContext = (0, react_1.createContext)(MOCK_SAFE_AREA_FRAME);
|
|
234
|
+
return {
|
|
235
|
+
initialWindowMetrics: metrics,
|
|
236
|
+
initialWindowSafeAreaInsets: MOCK_SAFE_AREA_INSETS,
|
|
237
|
+
SafeAreaFrameContext,
|
|
238
|
+
SafeAreaInsetsContext,
|
|
239
|
+
SafeAreaProvider: mockHelpers_1.passthroughComponent,
|
|
240
|
+
SafeAreaView: (0, mockHelpers_1.hostComponent)("SafeAreaView"),
|
|
241
|
+
useSafeAreaFrame: (0, mockHelpers_1.stubReturning)(MOCK_SAFE_AREA_FRAME),
|
|
242
|
+
useSafeAreaInsets: (0, mockHelpers_1.stubReturning)(MOCK_SAFE_AREA_INSETS),
|
|
243
|
+
withSafeAreaInsets: (0, mockHelpers_1.stubIdentity)(),
|
|
244
|
+
};
|
|
245
|
+
};
|
|
246
|
+
const createNetInfoMock = () => {
|
|
247
|
+
const netInfo = {
|
|
248
|
+
addEventListener: (0, bun_test_1.mock)((listener) => {
|
|
249
|
+
listener?.(exports.MOCK_NET_INFO_STATE);
|
|
250
|
+
return (0, mockHelpers_1.stub)();
|
|
251
|
+
}),
|
|
252
|
+
configure: (0, mockHelpers_1.stub)(),
|
|
253
|
+
fetch: (0, mockHelpers_1.stubResolving)(exports.MOCK_NET_INFO_STATE),
|
|
254
|
+
refresh: (0, mockHelpers_1.stubResolving)(exports.MOCK_NET_INFO_STATE),
|
|
255
|
+
useNetInfo: (0, mockHelpers_1.stubReturning)(exports.MOCK_NET_INFO_STATE),
|
|
256
|
+
};
|
|
257
|
+
return {
|
|
258
|
+
...netInfo,
|
|
259
|
+
default: netInfo,
|
|
260
|
+
NetInfoStateType: { cellular: "cellular", none: "none", unknown: "unknown", wifi: "wifi" },
|
|
261
|
+
};
|
|
262
|
+
};
|
|
263
|
+
/**
|
|
264
|
+
* `react-native-actions-sheet` renders nothing until a native animation opens it. This stand-in
|
|
265
|
+
* tracks the imperative `show` / `hide` / `setModalVisible` calls instead, so opened content is
|
|
266
|
+
* assertable and closed content is absent.
|
|
267
|
+
*/
|
|
268
|
+
const ActionSheet = (0, react_1.forwardRef)(({ children }, ref) => {
|
|
269
|
+
const [isVisible, setIsVisible] = (0, react_1.useState)(false);
|
|
270
|
+
(0, react_1.useImperativeHandle)(ref, () => ({
|
|
271
|
+
hide: () => setIsVisible(false),
|
|
272
|
+
setModalVisible: (visible) => setIsVisible(Boolean(visible)),
|
|
273
|
+
show: () => setIsVisible(true),
|
|
274
|
+
}));
|
|
275
|
+
if (!isVisible) {
|
|
276
|
+
return null;
|
|
277
|
+
}
|
|
278
|
+
return (0, react_1.createElement)("ActionSheet", null, children);
|
|
279
|
+
});
|
|
280
|
+
const createPresstoMock = () => {
|
|
281
|
+
const pressable = (name) => ({ enabled = true, onPress, ...props }) => (0, react_1.createElement)(name, { ...props, disabled: !enabled, onPress: enabled ? onPress : undefined }, props.children);
|
|
282
|
+
return {
|
|
283
|
+
PressableOpacity: pressable("PressableOpacity"),
|
|
284
|
+
PressableScale: pressable("PressableScale"),
|
|
285
|
+
PressableWithoutFeedback: pressable("PressableWithoutFeedback"),
|
|
286
|
+
};
|
|
287
|
+
};
|
|
288
|
+
/**
|
|
289
|
+
* Builds stand-ins for the native libraries most Expo apps depend on: Reanimated, Gesture
|
|
290
|
+
* Handler, SVG, safe-area-context, NetInfo, WebView, the community picker / slider /
|
|
291
|
+
* date-time picker, react-native-actions-sheet, pressto, and Sentry.
|
|
292
|
+
*/
|
|
293
|
+
const createLibraryModuleMocks = () => {
|
|
294
|
+
const safeArea = createSafeAreaMock();
|
|
295
|
+
const Picker = Object.assign((0, mockHelpers_1.hostComponent)("Picker"), { Item: (0, mockHelpers_1.hostComponent)("Picker.Item") });
|
|
296
|
+
const sentry = () => ({
|
|
297
|
+
addBreadcrumb: (0, mockHelpers_1.stub)(),
|
|
298
|
+
captureEvent: (0, mockHelpers_1.stubReturning)("test-event-id"),
|
|
299
|
+
captureException: (0, mockHelpers_1.stubReturning)("test-event-id"),
|
|
300
|
+
captureMessage: (0, mockHelpers_1.stubReturning)("test-event-id"),
|
|
301
|
+
ErrorBoundary: mockHelpers_1.passthroughComponent,
|
|
302
|
+
flush: (0, mockHelpers_1.stubResolving)(true),
|
|
303
|
+
init: (0, mockHelpers_1.stub)(),
|
|
304
|
+
reactNavigationIntegration: (0, mockHelpers_1.stubReturning)({ name: "ReactNavigation" }),
|
|
305
|
+
setContext: (0, mockHelpers_1.stub)(),
|
|
306
|
+
setTag: (0, mockHelpers_1.stub)(),
|
|
307
|
+
setUser: (0, mockHelpers_1.stub)(),
|
|
308
|
+
withScope: (0, bun_test_1.mock)((callback) => callback({ setContext: (0, mockHelpers_1.stub)(), setExtra: (0, mockHelpers_1.stub)(), setLevel: (0, mockHelpers_1.stub)(), setTag: (0, mockHelpers_1.stub)() })),
|
|
309
|
+
wrap: (0, mockHelpers_1.stubIdentity)(),
|
|
310
|
+
});
|
|
311
|
+
return {
|
|
312
|
+
"@react-native-community/datetimepicker": () => ({
|
|
313
|
+
DateTimePickerAndroid: {
|
|
314
|
+
dismiss: (0, mockHelpers_1.stubResolving)(undefined),
|
|
315
|
+
open: (0, mockHelpers_1.stubResolving)({ action: "dismissedAction" }),
|
|
316
|
+
},
|
|
317
|
+
default: (0, mockHelpers_1.hostComponent)("DateTimePicker"),
|
|
318
|
+
}),
|
|
319
|
+
"@react-native-community/netinfo": createNetInfoMock,
|
|
320
|
+
"@react-native-community/slider": () => ({ default: (0, mockHelpers_1.hostComponent)("Slider") }),
|
|
321
|
+
"@react-native-picker/picker": () => ({ Picker, PickerIOS: Picker }),
|
|
322
|
+
"@sentry/react-native": sentry,
|
|
323
|
+
pressto: createPresstoMock,
|
|
324
|
+
"react-native-actions-sheet": () => ({
|
|
325
|
+
__esModule: true,
|
|
326
|
+
default: ActionSheet,
|
|
327
|
+
SheetManager: { hide: (0, mockHelpers_1.stubResolving)(undefined), show: (0, mockHelpers_1.stubResolving)(undefined) },
|
|
328
|
+
}),
|
|
329
|
+
"react-native-gesture-handler": createGestureHandlerMock,
|
|
330
|
+
"react-native-reanimated": createReanimatedMock,
|
|
331
|
+
"react-native-safe-area-context": () => safeArea,
|
|
332
|
+
"react-native-safe-area-context/jest/mock": () => ({ default: safeArea }),
|
|
333
|
+
"react-native-screens": () => ({
|
|
334
|
+
enableFreeze: (0, mockHelpers_1.stub)(),
|
|
335
|
+
enableScreens: (0, mockHelpers_1.stub)(),
|
|
336
|
+
Screen: (0, mockHelpers_1.hostComponent)("Screen"),
|
|
337
|
+
ScreenContainer: (0, mockHelpers_1.hostComponent)("ScreenContainer"),
|
|
338
|
+
ScreenStack: (0, mockHelpers_1.hostComponent)("ScreenStack"),
|
|
339
|
+
}),
|
|
340
|
+
"react-native-svg": createSvgMock,
|
|
341
|
+
"react-native-webview": () => {
|
|
342
|
+
const WebView = (0, mockHelpers_1.hostComponent)("WebView");
|
|
343
|
+
return { default: WebView, WebView };
|
|
344
|
+
},
|
|
345
|
+
};
|
|
346
|
+
};
|
|
347
|
+
exports.createLibraryModuleMocks = createLibraryModuleMocks;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { type Mock } from "bun:test";
|
|
2
|
+
import { type ReactElement, type ReactNode } from "react";
|
|
3
|
+
/** Exports a `mock.module` factory returns. */
|
|
4
|
+
export type ModuleExports = Record<string, unknown>;
|
|
5
|
+
/** A `mock.module` factory: called by Bun when the mocked module is first imported. */
|
|
6
|
+
export type ModuleFactory = () => ModuleExports;
|
|
7
|
+
/** Map of module specifier to the factory that replaces it. */
|
|
8
|
+
export type ModuleMockMap = Record<string, ModuleFactory>;
|
|
9
|
+
/** Props the host-element stand-ins accept and forward. */
|
|
10
|
+
export type MockComponentProps = Record<string, unknown> & {
|
|
11
|
+
children?: ReactNode;
|
|
12
|
+
};
|
|
13
|
+
/** A function component used as a stand-in for a native view. */
|
|
14
|
+
export type MockComponent = (props: MockComponentProps) => ReactElement;
|
|
15
|
+
/** Runtime platform the mocks report through `Platform.OS` and `EXPO_OS`. */
|
|
16
|
+
export type ExpoTestPlatform = "android" | "ios" | "web";
|
|
17
|
+
/** A `{remove}` subscription handle like the ones React Native listeners return. */
|
|
18
|
+
export interface MockSubscription {
|
|
19
|
+
remove: Mock<() => void>;
|
|
20
|
+
}
|
|
21
|
+
/** A `bun:test` mock that does nothing and returns `undefined`. */
|
|
22
|
+
export declare const stub: () => Mock<(...args: unknown[]) => void>;
|
|
23
|
+
/** A `bun:test` mock that always returns `value`. */
|
|
24
|
+
export declare const stubReturning: <T>(value: T) => Mock<(...args: unknown[]) => T>;
|
|
25
|
+
/** A `bun:test` mock that always resolves to `value`. */
|
|
26
|
+
export declare const stubResolving: <T>(value: T) => Mock<(...args: unknown[]) => Promise<T>>;
|
|
27
|
+
/** A `bun:test` mock that returns its first argument unchanged. */
|
|
28
|
+
export declare const stubIdentity: () => Mock<(<T>(value: T, ...rest: unknown[]) => T)>;
|
|
29
|
+
/** A `bun:test` mock that returns a fresh `{remove}` subscription on every call. */
|
|
30
|
+
export declare const stubSubscription: () => Mock<(...args: unknown[]) => MockSubscription>;
|
|
31
|
+
/**
|
|
32
|
+
* A component that renders a host element named `name` with every prop and child forwarded,
|
|
33
|
+
* so tests can find it by `testID`, inspect its props, and fire its handlers.
|
|
34
|
+
*/
|
|
35
|
+
export declare const hostComponent: (name: string) => MockComponent;
|
|
36
|
+
/** A component that renders its children only (a provider or wrapper stand-in). */
|
|
37
|
+
export declare const passthroughComponent: ({ children }: MockComponentProps) => ReactElement;
|
|
38
|
+
/** Builds a map of host components, one per name. */
|
|
39
|
+
export declare const hostComponents: (names: string[]) => Record<string, MockComponent>;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.hostComponents = exports.passthroughComponent = exports.hostComponent = exports.stubSubscription = exports.stubIdentity = exports.stubResolving = exports.stubReturning = exports.stub = void 0;
|
|
4
|
+
const bun_test_1 = require("bun:test");
|
|
5
|
+
const react_1 = require("react");
|
|
6
|
+
const noop = () => { };
|
|
7
|
+
/** A `bun:test` mock that does nothing and returns `undefined`. */
|
|
8
|
+
const stub = () => (0, bun_test_1.mock)(noop);
|
|
9
|
+
exports.stub = stub;
|
|
10
|
+
/** A `bun:test` mock that always returns `value`. */
|
|
11
|
+
const stubReturning = (value) => (0, bun_test_1.mock)((..._args) => value);
|
|
12
|
+
exports.stubReturning = stubReturning;
|
|
13
|
+
/** A `bun:test` mock that always resolves to `value`. */
|
|
14
|
+
const stubResolving = (value) => (0, bun_test_1.mock)((..._args) => Promise.resolve(value));
|
|
15
|
+
exports.stubResolving = stubResolving;
|
|
16
|
+
/** A `bun:test` mock that returns its first argument unchanged. */
|
|
17
|
+
const stubIdentity = () => (0, bun_test_1.mock)((value, ..._rest) => value);
|
|
18
|
+
exports.stubIdentity = stubIdentity;
|
|
19
|
+
/** A fresh `{remove}` subscription handle. */
|
|
20
|
+
const subscription = () => ({ remove: (0, bun_test_1.mock)(noop) });
|
|
21
|
+
/** A `bun:test` mock that returns a fresh `{remove}` subscription on every call. */
|
|
22
|
+
const stubSubscription = () => (0, bun_test_1.mock)((..._args) => subscription());
|
|
23
|
+
exports.stubSubscription = stubSubscription;
|
|
24
|
+
/**
|
|
25
|
+
* A component that renders a host element named `name` with every prop and child forwarded,
|
|
26
|
+
* so tests can find it by `testID`, inspect its props, and fire its handlers.
|
|
27
|
+
*/
|
|
28
|
+
const hostComponent = (name) => {
|
|
29
|
+
const Component = ({ children, ...props }) => (0, react_1.createElement)(name, props, children);
|
|
30
|
+
Object.defineProperty(Component, "name", { value: name });
|
|
31
|
+
return Component;
|
|
32
|
+
};
|
|
33
|
+
exports.hostComponent = hostComponent;
|
|
34
|
+
/** A component that renders its children only (a provider or wrapper stand-in). */
|
|
35
|
+
const passthroughComponent = ({ children }) => (0, react_1.createElement)(react_1.Fragment, null, children);
|
|
36
|
+
exports.passthroughComponent = passthroughComponent;
|
|
37
|
+
/** Builds a map of host components, one per name. */
|
|
38
|
+
const hostComponents = (names) => {
|
|
39
|
+
const components = {};
|
|
40
|
+
for (const name of names) {
|
|
41
|
+
components[name] = (0, exports.hostComponent)(name);
|
|
42
|
+
}
|
|
43
|
+
return components;
|
|
44
|
+
};
|
|
45
|
+
exports.hostComponents = hostComponents;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { ModuleExports } from "./mockHelpers";
|
|
2
|
+
/**
|
|
3
|
+
* Path aliases the app resolves, e.g. `{"@hooks": "./hooks", "@store": "./store/index.ts"}`
|
|
4
|
+
* (the `paths` in `tsconfig.json`, without the `/*`). A value may name a directory or a file.
|
|
5
|
+
*/
|
|
6
|
+
export type ModuleAliasMap = Record<string, string>;
|
|
7
|
+
/** Options for `createModuleAliasMocker`. */
|
|
8
|
+
export interface ModuleAliasMockerOptions {
|
|
9
|
+
/** Alias to directory-or-file map. Relative targets resolve against `rootDir`. */
|
|
10
|
+
aliases: ModuleAliasMap;
|
|
11
|
+
/** Extensions tried when resolving an alias path, in order. Default `.ts`, `.tsx`, `.js`, `.jsx`. */
|
|
12
|
+
extensions?: readonly string[];
|
|
13
|
+
/** Files skipped when a whole directory is mocked. Default: tests, `.d.ts`, and `.web.*` variants. */
|
|
14
|
+
ignore?: RegExp;
|
|
15
|
+
/** Directory relative alias targets resolve against. Default `process.cwd()`. */
|
|
16
|
+
rootDir?: string;
|
|
17
|
+
}
|
|
18
|
+
/** Options for one `mockModule` call. */
|
|
19
|
+
export interface MockAliasedModuleOptions {
|
|
20
|
+
/** Allow override names the real module(s) do not export. Default `false` (throws on a typo). */
|
|
21
|
+
allowNewExports?: boolean;
|
|
22
|
+
}
|
|
23
|
+
/** The mocker `createModuleAliasMocker` returns. */
|
|
24
|
+
export interface ModuleAliasMocker {
|
|
25
|
+
/** Every specifier Bun may key `file`'s module cache on: alias forms plus both path forms. */
|
|
26
|
+
getCacheKeys: (file: string) => string[];
|
|
27
|
+
/** The module files an alias specifier covers (one file, or every file under a directory). */
|
|
28
|
+
getModuleFiles: (specifier: string) => string[];
|
|
29
|
+
/** Real exports of a module file or alias specifier, captured once before any mock applied. */
|
|
30
|
+
getRealExports: (specifierOrFile: string) => ModuleExports;
|
|
31
|
+
/**
|
|
32
|
+
* Mocks `specifier` starting from its real exports, replacing only the names in `overrides`.
|
|
33
|
+
* A directory alias (`"@hooks"`) re-registers every file under it; each override applies to
|
|
34
|
+
* whichever files export that name. Registers under every cache key, so imports through the
|
|
35
|
+
* alias, a relative path, or the absolute path all see the mock.
|
|
36
|
+
*/
|
|
37
|
+
mockModule: (specifier: string, overrides: ModuleExports, options?: MockAliasedModuleOptions) => void;
|
|
38
|
+
/** Re-registers the real exports for `specifier`, or for every file mocked so far. */
|
|
39
|
+
restore: (specifier?: string) => void;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Builds a mocker for an app's path aliases. Bun keys its module cache on the specifier it
|
|
43
|
+
* resolved, so `@hooks/useProfile`, `../hooks/useProfile`, and `/abs/hooks/useProfile.ts` can be
|
|
44
|
+
* separate entries. The mocker registers one factory under all of them, built from the module's
|
|
45
|
+
* real exports, so a test overrides a single hook without stubbing the rest of the module.
|
|
46
|
+
*
|
|
47
|
+
* Like any `mock.module` call, a mock stays registered for the rest of the Bun process. Call
|
|
48
|
+
* `mockModule` from `beforeEach` and `restore()` from `afterEach` / `afterAll`.
|
|
49
|
+
*/
|
|
50
|
+
export declare const createModuleAliasMocker: ({ aliases, extensions, ignore, rootDir, }: ModuleAliasMockerOptions) => ModuleAliasMocker;
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createModuleAliasMocker = void 0;
|
|
4
|
+
const bun_test_1 = require("bun:test");
|
|
5
|
+
const node_fs_1 = require("node:fs");
|
|
6
|
+
const node_path_1 = require("node:path");
|
|
7
|
+
const DEFAULT_EXTENSIONS = [".ts", ".tsx", ".js", ".jsx"];
|
|
8
|
+
const DEFAULT_IGNORE = /(?:\.test\.|\.spec\.|\.d\.ts$|\.web\.[jt]sx?$|(?:^|[\\/])__tests__[\\/])/u;
|
|
9
|
+
/** Real exports keyed by absolute file path, shared across mockers so a snapshot is taken once. */
|
|
10
|
+
const realExportsByFile = new Map();
|
|
11
|
+
const toPosix = (path) => path.split(node_path_1.sep).join("/");
|
|
12
|
+
const isDirectory = (path) => (0, node_fs_1.existsSync)(path) && (0, node_fs_1.statSync)(path).isDirectory();
|
|
13
|
+
const isFile = (path) => (0, node_fs_1.existsSync)(path) && (0, node_fs_1.statSync)(path).isFile();
|
|
14
|
+
/**
|
|
15
|
+
* Builds a mocker for an app's path aliases. Bun keys its module cache on the specifier it
|
|
16
|
+
* resolved, so `@hooks/useProfile`, `../hooks/useProfile`, and `/abs/hooks/useProfile.ts` can be
|
|
17
|
+
* separate entries. The mocker registers one factory under all of them, built from the module's
|
|
18
|
+
* real exports, so a test overrides a single hook without stubbing the rest of the module.
|
|
19
|
+
*
|
|
20
|
+
* Like any `mock.module` call, a mock stays registered for the rest of the Bun process. Call
|
|
21
|
+
* `mockModule` from `beforeEach` and `restore()` from `afterEach` / `afterAll`.
|
|
22
|
+
*/
|
|
23
|
+
const createModuleAliasMocker = ({ aliases, extensions = DEFAULT_EXTENSIONS, ignore = DEFAULT_IGNORE, rootDir = process.cwd(), }) => {
|
|
24
|
+
const aliasTargets = Object.entries(aliases)
|
|
25
|
+
.map(([alias, target]) => [
|
|
26
|
+
alias.replace(/\/$/u, ""),
|
|
27
|
+
(0, node_path_1.isAbsolute)(target) ? target : (0, node_path_1.resolve)(rootDir, target),
|
|
28
|
+
])
|
|
29
|
+
// Longest alias first so `@store/sdk` wins over `@store` when both are declared.
|
|
30
|
+
.sort(([a], [b]) => b.length - a.length);
|
|
31
|
+
const mockedFiles = new Set();
|
|
32
|
+
const resolveFile = (basePath) => {
|
|
33
|
+
if (isFile(basePath)) {
|
|
34
|
+
return basePath;
|
|
35
|
+
}
|
|
36
|
+
for (const extension of extensions) {
|
|
37
|
+
if (isFile(`${basePath}${extension}`)) {
|
|
38
|
+
return `${basePath}${extension}`;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (!isDirectory(basePath)) {
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
for (const extension of extensions) {
|
|
45
|
+
const indexFile = (0, node_path_1.join)(basePath, `index${extension}`);
|
|
46
|
+
if (isFile(indexFile)) {
|
|
47
|
+
return indexFile;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return undefined;
|
|
51
|
+
};
|
|
52
|
+
const isModuleFile = (file) => extensions.some((extension) => file.endsWith(extension)) && !ignore.test(file);
|
|
53
|
+
const listModuleFiles = (directory) => {
|
|
54
|
+
const files = [];
|
|
55
|
+
for (const entry of (0, node_fs_1.readdirSync)(directory).sort()) {
|
|
56
|
+
if (entry.startsWith(".") || entry === "node_modules") {
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
const path = (0, node_path_1.join)(directory, entry);
|
|
60
|
+
if (isDirectory(path)) {
|
|
61
|
+
files.push(...listModuleFiles(path));
|
|
62
|
+
}
|
|
63
|
+
else if (isModuleFile(path)) {
|
|
64
|
+
files.push(path);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return files;
|
|
68
|
+
};
|
|
69
|
+
/** Absolute path an alias specifier points at (file or directory), or undefined. */
|
|
70
|
+
const resolveAliasPath = (specifier) => {
|
|
71
|
+
for (const [alias, target] of aliasTargets) {
|
|
72
|
+
if (specifier === alias) {
|
|
73
|
+
return target;
|
|
74
|
+
}
|
|
75
|
+
if (specifier.startsWith(`${alias}/`)) {
|
|
76
|
+
return (0, node_path_1.join)(target, specifier.slice(alias.length + 1));
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return undefined;
|
|
80
|
+
};
|
|
81
|
+
const getModuleFiles = (specifier) => {
|
|
82
|
+
const aliasPath = resolveAliasPath(specifier) ?? ((0, node_path_1.isAbsolute)(specifier) ? specifier : undefined);
|
|
83
|
+
if (!aliasPath) {
|
|
84
|
+
throw new Error(`mockModuleAliases: "${specifier}" matches no alias in ${JSON.stringify(aliases)}`);
|
|
85
|
+
}
|
|
86
|
+
// An exact directory alias covers every file below it, even when the directory has an index.
|
|
87
|
+
if (isDirectory(aliasPath) && (specifier in aliases || !resolveFile(aliasPath))) {
|
|
88
|
+
return listModuleFiles(aliasPath);
|
|
89
|
+
}
|
|
90
|
+
const file = resolveFile(aliasPath);
|
|
91
|
+
if (!file) {
|
|
92
|
+
throw new Error(`mockModuleAliases: "${specifier}" resolves to ${aliasPath}, which does not exist`);
|
|
93
|
+
}
|
|
94
|
+
return [file];
|
|
95
|
+
};
|
|
96
|
+
const getRealExports = (specifierOrFile) => {
|
|
97
|
+
const files = isFile(specifierOrFile) ? [specifierOrFile] : getModuleFiles(specifierOrFile);
|
|
98
|
+
const merged = {};
|
|
99
|
+
for (const file of files) {
|
|
100
|
+
let real = realExportsByFile.get(file);
|
|
101
|
+
if (!real) {
|
|
102
|
+
real = { ...require(file) };
|
|
103
|
+
realExportsByFile.set(file, real);
|
|
104
|
+
}
|
|
105
|
+
Object.assign(merged, real);
|
|
106
|
+
}
|
|
107
|
+
return merged;
|
|
108
|
+
};
|
|
109
|
+
const getCacheKeys = (file) => {
|
|
110
|
+
const withoutExtension = file.replace(/\.[cm]?[jt]sx?$/u, "");
|
|
111
|
+
const keys = new Set([file, withoutExtension]);
|
|
112
|
+
for (const [alias, target] of aliasTargets) {
|
|
113
|
+
if (target === file) {
|
|
114
|
+
keys.add(alias);
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
const relativePath = toPosix((0, node_path_1.relative)(target, withoutExtension));
|
|
118
|
+
if (relativePath.startsWith("..") || (0, node_path_1.isAbsolute)(relativePath) || !isDirectory(target)) {
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
keys.add(`${alias}/${relativePath}`);
|
|
122
|
+
if (relativePath === "index") {
|
|
123
|
+
keys.add(alias);
|
|
124
|
+
}
|
|
125
|
+
else if (relativePath.endsWith("/index")) {
|
|
126
|
+
keys.add(`${alias}/${relativePath.slice(0, -"/index".length)}`);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return [...keys];
|
|
130
|
+
};
|
|
131
|
+
const registerFile = (file, exports) => {
|
|
132
|
+
const factory = () => exports;
|
|
133
|
+
for (const key of getCacheKeys(file)) {
|
|
134
|
+
bun_test_1.mock.module(key, factory);
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
const mockModule = (specifier, overrides, { allowNewExports = false } = {}) => {
|
|
138
|
+
const files = getModuleFiles(specifier);
|
|
139
|
+
const realByFile = files.map((file) => getRealExports(file));
|
|
140
|
+
const unmatched = new Set(Object.keys(overrides));
|
|
141
|
+
for (const real of realByFile) {
|
|
142
|
+
for (const name of Object.keys(overrides)) {
|
|
143
|
+
if (name in real) {
|
|
144
|
+
unmatched.delete(name);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
if (unmatched.size > 0 && !allowNewExports) {
|
|
149
|
+
throw new Error(`mockModuleAliases: ${[...unmatched].map((name) => `"${name}"`).join(", ")} ` +
|
|
150
|
+
`is not exported by "${specifier}". Check for a typo or a renamed export, or pass {allowNewExports: true}.`);
|
|
151
|
+
}
|
|
152
|
+
files.forEach((file, index) => {
|
|
153
|
+
const real = realByFile[index];
|
|
154
|
+
const applied = { ...real };
|
|
155
|
+
for (const [name, value] of Object.entries(overrides)) {
|
|
156
|
+
// New names go on the first file so a single-file alias can still add exports.
|
|
157
|
+
if (name in real || (unmatched.has(name) && index === 0)) {
|
|
158
|
+
applied[name] = value;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
registerFile(file, applied);
|
|
162
|
+
mockedFiles.add(file);
|
|
163
|
+
});
|
|
164
|
+
};
|
|
165
|
+
const restore = (specifier) => {
|
|
166
|
+
const files = specifier ? getModuleFiles(specifier) : [...mockedFiles];
|
|
167
|
+
for (const file of files) {
|
|
168
|
+
registerFile(file, getRealExports(file));
|
|
169
|
+
mockedFiles.delete(file);
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
return { getCacheKeys, getModuleFiles, getRealExports, mockModule, restore };
|
|
173
|
+
};
|
|
174
|
+
exports.createModuleAliasMocker = createModuleAliasMocker;
|