@granite-js/vitest 1.0.20

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,291 @@
1
+ import { vi } from "vitest";
2
+
3
+ //#region src/runtimeBootstrap.ts
4
+ function defineGlobalProperty(runtimeGlobals, key, value) {
5
+ Object.defineProperty(runtimeGlobals, key, {
6
+ configurable: true,
7
+ enumerable: true,
8
+ value,
9
+ writable: true
10
+ });
11
+ }
12
+ function createViewManagerConfig(name) {
13
+ return {
14
+ Commands: {},
15
+ Constants: {},
16
+ Manager: name,
17
+ NativeProps: {},
18
+ bubblingEventTypes: {},
19
+ directEventTypes: {},
20
+ uiViewClassName: name,
21
+ validAttributes: {}
22
+ };
23
+ }
24
+ function installRequestAnimationFrameShim(runtimeGlobals) {
25
+ Object.defineProperty(runtimeGlobals, "requestAnimationFrame", {
26
+ configurable: true,
27
+ enumerable: true,
28
+ value: (callback) => setTimeout(() => callback(runtimeGlobals.jest?.now?.() ?? Date.now()), 0),
29
+ writable: true
30
+ });
31
+ Object.defineProperty(runtimeGlobals, "cancelAnimationFrame", {
32
+ configurable: true,
33
+ enumerable: true,
34
+ value: (id) => clearTimeout(id),
35
+ writable: true
36
+ });
37
+ }
38
+ function installReactNativeGlobals(runtimeGlobals) {
39
+ const windowEventListeners = /* @__PURE__ */ new Map();
40
+ defineGlobalProperty(runtimeGlobals, "IS_REACT_ACT_ENVIRONMENT", true);
41
+ defineGlobalProperty(runtimeGlobals, "IS_REACT_NATIVE_TEST_ENVIRONMENT", true);
42
+ defineGlobalProperty(runtimeGlobals, "__DEV__", true);
43
+ defineGlobalProperty(runtimeGlobals, "window", runtimeGlobals);
44
+ defineGlobalProperty(runtimeGlobals, "nativeFabricUIManager", {});
45
+ defineGlobalProperty(runtimeGlobals, "addEventListener", vi.fn((type, listener) => {
46
+ const listeners = windowEventListeners.get(type) ?? /* @__PURE__ */ new Set();
47
+ listeners.add(listener);
48
+ windowEventListeners.set(type, listeners);
49
+ }));
50
+ defineGlobalProperty(runtimeGlobals, "removeEventListener", vi.fn((type, listener) => {
51
+ windowEventListeners.get(type)?.delete(listener);
52
+ }));
53
+ defineGlobalProperty(runtimeGlobals, "dispatchEvent", vi.fn((event) => {
54
+ if (event?.type != null) windowEventListeners.get(event.type)?.forEach((listener) => listener(event));
55
+ return true;
56
+ }));
57
+ Object.defineProperty(runtimeGlobals, "performance", {
58
+ configurable: true,
59
+ enumerable: true,
60
+ value: { now: vi.fn(Date.now) },
61
+ writable: true
62
+ });
63
+ installRequestAnimationFrameShim(runtimeGlobals);
64
+ }
65
+ function installNativeModuleProxy(runtimeGlobals) {
66
+ const dimensions = {
67
+ screen: {
68
+ fontScale: 2,
69
+ height: 1334,
70
+ scale: 2,
71
+ width: 750
72
+ },
73
+ window: {
74
+ fontScale: 2,
75
+ height: 1334,
76
+ scale: 2,
77
+ width: 750
78
+ }
79
+ };
80
+ const viewManagerConfigs = {
81
+ AndroidDrawerLayout: {
82
+ ...createViewManagerConfig("AndroidDrawerLayout"),
83
+ Constants: { DrawerPosition: { Left: 10 } }
84
+ },
85
+ AndroidTextInput: {
86
+ ...createViewManagerConfig("AndroidTextInput"),
87
+ Commands: {}
88
+ },
89
+ RCTMultilineTextInputView: createViewManagerConfig("RCTMultilineTextInputView"),
90
+ RCTSafeAreaView: createViewManagerConfig("RCTSafeAreaView"),
91
+ RCTScrollView: createViewManagerConfig("RCTScrollView"),
92
+ RCTSinglelineTextInputView: createViewManagerConfig("RCTSinglelineTextInputView"),
93
+ RCTText: createViewManagerConfig("RCTText"),
94
+ RCTView: createViewManagerConfig("RCTView"),
95
+ RCTVirtualText: createViewManagerConfig("RCTVirtualText"),
96
+ ScrollView: createViewManagerConfig("ScrollView"),
97
+ Text: createViewManagerConfig("Text"),
98
+ View: createViewManagerConfig("View")
99
+ };
100
+ const uiManager = {
101
+ AndroidDrawerLayout: { Constants: { DrawerPosition: { Left: 10 } } },
102
+ AndroidViewPager: { Commands: {
103
+ setPage: vi.fn(),
104
+ setPageWithoutAnimation: vi.fn()
105
+ } },
106
+ AndroidTextInput: { Commands: {} },
107
+ ScrollView: { Constants: {} },
108
+ View: { Constants: {} },
109
+ blur: vi.fn(),
110
+ createView: vi.fn(),
111
+ customBubblingEventTypes: {},
112
+ customDirectEventTypes: {},
113
+ dispatchViewManagerCommand: vi.fn(),
114
+ focus: vi.fn(),
115
+ lazilyLoadView: vi.fn(() => null),
116
+ manageChildren: vi.fn(),
117
+ measure: vi.fn(),
118
+ measureInWindow: vi.fn(),
119
+ measureLayout: vi.fn(),
120
+ measureLayoutRelativeToParent: vi.fn(),
121
+ setChildren: vi.fn(),
122
+ updateView: vi.fn()
123
+ };
124
+ uiManager.getConstants = vi.fn(() => viewManagerConfigs);
125
+ uiManager.getConstantsForViewManager = vi.fn((name) => viewManagerConfigs[name] ?? createViewManagerConfig(name));
126
+ uiManager.getViewManagerConfig = vi.fn((name) => viewManagerConfigs[name] ?? uiManager.getConstantsForViewManager(name));
127
+ uiManager.hasViewManagerConfig = vi.fn((name) => uiManager.getViewManagerConfig(name) != null);
128
+ const nativeAnimatedModule = {
129
+ addAnimatedEventToView: vi.fn(),
130
+ addListener: vi.fn(),
131
+ connectAnimatedNodeToView: vi.fn(),
132
+ connectAnimatedNodes: vi.fn(),
133
+ createAnimatedNode: vi.fn(),
134
+ disconnectAnimatedNodeFromView: vi.fn(),
135
+ disconnectAnimatedNodes: vi.fn(),
136
+ dropAnimatedNode: vi.fn(),
137
+ extractAnimatedNodeOffset: vi.fn(),
138
+ finishOperationBatch: vi.fn(),
139
+ flattenAnimatedNodeOffset: vi.fn(),
140
+ getValue: vi.fn(),
141
+ queueAndExecuteBatchedOperations: vi.fn(),
142
+ removeAnimatedEventFromView: vi.fn(),
143
+ removeListener: vi.fn(),
144
+ removeListeners: vi.fn(),
145
+ restoreDefaultValues: vi.fn(),
146
+ setAnimatedNodeOffset: vi.fn(),
147
+ setAnimatedNodeValue: vi.fn(),
148
+ startAnimatingNode: vi.fn((animationId, nodeTag, config, endCallback) => {
149
+ setTimeout(() => endCallback?.({ finished: true }), 16);
150
+ }),
151
+ startListeningToAnimatedNodeValue: vi.fn(),
152
+ startOperationBatch: vi.fn(),
153
+ stopAnimation: vi.fn(),
154
+ stopListeningToAnimatedNodeValue: vi.fn(),
155
+ updateAnimatedNodeConfig: vi.fn()
156
+ };
157
+ const nativeModuleProxy = {
158
+ AlertManager: { alertWithArgs: vi.fn() },
159
+ AsyncLocalStorage: {
160
+ clear: vi.fn((callback) => process.nextTick(() => callback?.(null))),
161
+ getAllKeys: vi.fn((callback) => process.nextTick(() => callback(null, []))),
162
+ multiGet: vi.fn((keys, callback) => process.nextTick(() => callback(null, []))),
163
+ multiMerge: vi.fn((entries, callback) => process.nextTick(() => callback?.(null))),
164
+ multiRemove: vi.fn((keys, callback) => process.nextTick(() => callback?.(null))),
165
+ multiSet: vi.fn((entries, callback) => process.nextTick(() => callback?.(null)))
166
+ },
167
+ BlobModule: {
168
+ addNetworkingHandler: vi.fn(),
169
+ createFromParts: vi.fn(),
170
+ disableBlobSupport: vi.fn(),
171
+ enableBlobSupport: vi.fn(),
172
+ getConstants: () => ({
173
+ BLOB_URI_HOST: null,
174
+ BLOB_URI_SCHEME: "content"
175
+ }),
176
+ release: vi.fn(),
177
+ sendBlob: vi.fn()
178
+ },
179
+ DevSettings: {
180
+ addMenuItem: vi.fn(),
181
+ reload: vi.fn()
182
+ },
183
+ DeviceInfo: { getConstants: () => ({ Dimensions: dimensions }) },
184
+ I18nManager: {
185
+ allowRTL: vi.fn(),
186
+ forceRTL: vi.fn(),
187
+ getConstants: () => ({
188
+ doLeftAndRightSwapInRTL: true,
189
+ isRTL: false,
190
+ localeIdentifier: "en_US"
191
+ }),
192
+ swapLeftAndRightInRTL: vi.fn()
193
+ },
194
+ ImageLoader: {
195
+ getSize: vi.fn((url) => Promise.resolve([320, 240])),
196
+ getSizeWithHeaders: vi.fn((url, headers) => Promise.resolve({
197
+ height: 222,
198
+ width: 333
199
+ })),
200
+ prefetchImage: vi.fn(),
201
+ prefetchImageWithMetadata: vi.fn(),
202
+ queryCache: vi.fn()
203
+ },
204
+ ImageViewManager: {
205
+ getSize: vi.fn((uri, success) => process.nextTick(() => success(320, 240))),
206
+ prefetchImage: vi.fn()
207
+ },
208
+ KeyboardObserver: {
209
+ addListener: vi.fn(),
210
+ removeListeners: vi.fn()
211
+ },
212
+ NativeAnimatedModule: nativeAnimatedModule,
213
+ Networking: {
214
+ abortRequest: vi.fn(),
215
+ addListener: vi.fn(),
216
+ removeListeners: vi.fn(),
217
+ sendRequest: vi.fn()
218
+ },
219
+ PlatformConstants: { getConstants: () => ({
220
+ forceTouchAvailable: false,
221
+ interfaceIdiom: "phone",
222
+ isDisableAnimations: false,
223
+ isMacCatalyst: false,
224
+ isTesting: true,
225
+ osVersion: "17.0",
226
+ reactNativeVersion: {
227
+ major: 0,
228
+ minor: 79,
229
+ patch: 5,
230
+ prerelease: void 0
231
+ },
232
+ systemName: "iOS"
233
+ }) },
234
+ PushNotificationManager: {
235
+ abandonPermissions: vi.fn(),
236
+ addListener: vi.fn(),
237
+ cancelAllLocalNotifications: vi.fn(),
238
+ cancelLocalNotifications: vi.fn(),
239
+ checkPermissions: vi.fn((callback) => process.nextTick(() => callback({
240
+ alert: true,
241
+ badge: true,
242
+ sound: true
243
+ }))),
244
+ getApplicationIconBadgeNumber: vi.fn((callback) => process.nextTick(() => callback(0))),
245
+ getDeliveredNotifications: vi.fn((callback) => process.nextTick(() => callback([]))),
246
+ getInitialNotification: vi.fn(() => Promise.resolve(null)),
247
+ getScheduledLocalNotifications: vi.fn((callback) => process.nextTick(() => callback?.())),
248
+ presentLocalNotification: vi.fn(),
249
+ removeAllDeliveredNotifications: vi.fn(),
250
+ removeDeliveredNotifications: vi.fn(),
251
+ removeListeners: vi.fn(),
252
+ requestPermissions: vi.fn(() => Promise.resolve({
253
+ alert: true,
254
+ badge: true,
255
+ sound: true
256
+ })),
257
+ scheduleLocalNotification: vi.fn(),
258
+ setApplicationIconBadgeNumber: vi.fn()
259
+ },
260
+ SourceCode: { getConstants: () => ({ scriptURL: null }) },
261
+ StatusBarManager: {
262
+ getConstants: () => ({ HEIGHT: 42 }),
263
+ setBackgroundColor: vi.fn(),
264
+ setColor: vi.fn(),
265
+ setHidden: vi.fn(),
266
+ setNetworkActivityIndicatorVisible: vi.fn(),
267
+ setStyle: vi.fn(),
268
+ setTranslucent: vi.fn()
269
+ },
270
+ Timing: {
271
+ createTimer: vi.fn(),
272
+ deleteTimer: vi.fn()
273
+ },
274
+ UIManager: uiManager,
275
+ WebSocketModule: {
276
+ addListener: vi.fn(),
277
+ close: vi.fn(),
278
+ connect: vi.fn(),
279
+ ping: vi.fn(),
280
+ removeListeners: vi.fn(),
281
+ send: vi.fn(),
282
+ sendBinary: vi.fn()
283
+ }
284
+ };
285
+ defineGlobalProperty(runtimeGlobals, "nativeModuleProxy", nativeModuleProxy);
286
+ defineGlobalProperty(runtimeGlobals, "__turboModuleProxy", (name) => nativeModuleProxy[name] ?? null);
287
+ defineGlobalProperty(runtimeGlobals, "__fbBatchedBridgeConfig", { remoteModuleConfig: [] });
288
+ }
289
+
290
+ //#endregion
291
+ export { installRequestAnimationFrameShim as i, installNativeModuleProxy as n, installReactNativeGlobals as r, defineGlobalProperty as t };
@@ -0,0 +1 @@
1
+ export { };
package/dist/setup.js ADDED
@@ -0,0 +1,48 @@
1
+ import { i as installRequestAnimationFrameShim } from "./runtimeBootstrap-CHWitCG1.js";
2
+ import { createRequire } from "node:module";
3
+ import { vi } from "vitest";
4
+
5
+ //#region src/jestBridge.ts
6
+ function installVitestPrettierWorkaround(runtimeRequire = createRequire(import.meta.url)) {
7
+ try {
8
+ const prettierPath = runtimeRequire.resolve("prettier");
9
+ vi.doMock("prettier", () => runtimeRequire(prettierPath));
10
+ } catch {}
11
+ }
12
+ function installVitestJestBridge() {
13
+ const runtimeGlobals = globalThis;
14
+ const runtimeRequire = createRequire(import.meta.url);
15
+ installRequestAnimationFrameShim(runtimeGlobals);
16
+ runtimeGlobals.jest = {
17
+ advanceTimersByTime: vi.advanceTimersByTime,
18
+ clearAllMocks: vi.clearAllMocks,
19
+ doMock: vi.doMock,
20
+ fn: vi.fn,
21
+ getRealSystemTime: vi.getRealSystemTime,
22
+ mock: vi.mock,
23
+ now: () => vi.getMockedSystemTime()?.valueOf() ?? Date.now(),
24
+ requireActual: (moduleName) => runtimeRequire(moduleName),
25
+ resetAllMocks: vi.resetAllMocks,
26
+ restoreAllMocks: vi.restoreAllMocks,
27
+ spyOn: vi.spyOn,
28
+ unmock: vi.unmock,
29
+ useFakeTimers: (...args) => {
30
+ const result = vi.useFakeTimers(...args);
31
+ installRequestAnimationFrameShim(runtimeGlobals);
32
+ return result;
33
+ },
34
+ useRealTimers: () => {
35
+ const result = vi.useRealTimers();
36
+ installRequestAnimationFrameShim(runtimeGlobals);
37
+ return result;
38
+ }
39
+ };
40
+ }
41
+
42
+ //#endregion
43
+ //#region src/setup.ts
44
+ installVitestPrettierWorkaround();
45
+ installVitestJestBridge();
46
+
47
+ //#endregion
48
+ export { };
package/package.json ADDED
@@ -0,0 +1,64 @@
1
+ {
2
+ "name": "@granite-js/vitest",
3
+ "version": "1.0.20",
4
+ "type": "module",
5
+ "description": "Vitest helpers for React Native",
6
+ "main": "./dist/index.js",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/toss/granite.git",
12
+ "directory": "packages/vitest"
13
+ },
14
+ "exports": {
15
+ ".": {
16
+ "types": "./dist/index.d.ts",
17
+ "import": "./dist/index.js",
18
+ "default": "./dist/index.js"
19
+ },
20
+ "./setup": {
21
+ "types": "./dist/setup.d.ts",
22
+ "import": "./dist/setup.js",
23
+ "default": "./dist/setup.js"
24
+ },
25
+ "./react-native-runtime": {
26
+ "types": "./dist/reactNativeRuntime.d.ts",
27
+ "import": "./dist/reactNativeRuntime.js",
28
+ "default": "./dist/reactNativeRuntime.js"
29
+ },
30
+ "./package.json": "./package.json"
31
+ },
32
+ "files": [
33
+ "dist"
34
+ ],
35
+ "scripts": {
36
+ "prepack": "yarn build",
37
+ "typecheck": "tsc --noEmit",
38
+ "lint": "eslint .",
39
+ "test": "vitest --run",
40
+ "build": "tsdown"
41
+ },
42
+ "dependencies": {
43
+ "@react-native/js-polyfills": "0.84.0",
44
+ "fast-flow-transform": "0.0.3"
45
+ },
46
+ "devDependencies": {
47
+ "@types/node": "catalog:tools",
48
+ "@types/react": "catalog:react-native",
49
+ "react": "catalog:react-native",
50
+ "react-native": "catalog:react-native",
51
+ "tsdown": "catalog:tools",
52
+ "typescript": "catalog:tools",
53
+ "vitest": "4.1.3"
54
+ },
55
+ "peerDependencies": {
56
+ "react": "*",
57
+ "react-native": "*",
58
+ "vitest": "*"
59
+ },
60
+ "sideEffects": [
61
+ "./dist/reactNativeRuntime.js",
62
+ "./dist/setup.js"
63
+ ]
64
+ }