Repository navigation
Expand file tree
/
Copy pathjest.setup.js
More file actions
206 lines (185 loc) · 6.17 KB
/
Copy pathjest.setup.js
File metadata and controls
206 lines (185 loc) · 6.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import {jest, afterAll} from '@jest/globals';
import {performance as nodePerformance} from 'node:perf_hooks';
// undici reads `performance.markResourceTiming` at import time (as early as
// @comapeo/core's own imports), so it must be patched before any test file loads.
globalThis.performance.markResourceTiming =
nodePerformance.markResourceTiming.bind(nodePerformance);
jest.mock('./translations/index', () => {
const actual = jest.requireActual('./translations/index');
return {
localeImports: Object.fromEntries(
Object.keys(actual.localeImports).map(key => [
key,
() =>
Promise.resolve({
default: jest.requireActual('./translations/en.json'),
}),
]),
),
};
});
import 'react-native-gesture-handler/jestSetup';
import mockNetInfo from '@react-native-community/netinfo/jest/netinfo-mock.js';
import mockSafeAreaContext from 'react-native-safe-area-context/jest/mock';
import mockRNDeviceInfo from 'react-native-device-info/jest/react-native-device-info-mock';
import {randomBytes} from 'node:crypto';
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import * as url from 'node:url';
import {NativeModules} from 'react-native';
import {setUpTests} from 'react-native-reanimated';
setUpTests();
// Module resolution error when using expo/winter. See: https://github.com/expo/expo/issues/36831#issuecomment-3107047371
jest.mock('expo/src/winter/ImportMetaRegistry', () => ({
ImportMetaRegistry: {
get url() {
return null;
},
},
}));
jest.mock('react-native-nitro-modules', () => {});
jest.mock('expo-audio', () => {
const grantedPermission = {
status: 'granted',
expires: 'never',
granted: true,
canAskAgain: true,
};
const mockRecorder = {
prepareToRecordAsync: jest.fn(() => Promise.resolve()),
record: jest.fn(),
stop: jest.fn(() => Promise.resolve()),
uri: null,
};
return {
getRecordingPermissionsAsync: jest.fn(() =>
Promise.resolve(grantedPermission),
),
requestRecordingPermissionsAsync: jest.fn(() =>
Promise.resolve(grantedPermission),
),
useAudioRecorder: jest.fn(() => mockRecorder),
useAudioRecorderState: jest.fn(() => ({
isRecording: false,
durationMillis: 0,
})),
useAudioPlayer: jest.fn(() => ({
play: jest.fn(),
pause: jest.fn(),
seekTo: jest.fn(),
})),
useAudioPlayerStatus: jest.fn(() => ({
isLoaded: false,
isPlaying: false,
currentTime: 0,
duration: 0,
})),
RecordingPresets: {
HIGH_QUALITY: {ios: {}, android: {}, web: {}},
LOW_QUALITY: {ios: {}, android: {}, web: {}},
},
setAudioModeAsync: jest.fn(() => Promise.resolve()),
};
});
jest.mock('@lodev09/react-native-exify', () => ({
read: jest.fn(() => Promise.resolve(null)),
}));
jest.mock('@react-native-community/netinfo', () => mockNetInfo);
jest.mock('react-native-safe-area-context', () => mockSafeAreaContext);
jest.mock('react-native-device-info', () => mockRNDeviceInfo);
// https://github.com/callstack/react-native-testing-library/issues/1712
jest.mock('expo-font', () => {
/** @type {import('expo-font')} */
const module = {
...jest.requireActual('expo-font'),
isLoaded: jest.fn(() => true),
};
return module;
});
// Native modules won't work in tests.
NativeModules.FlagSecureModule = {
activate: () => {},
deactivate: () => {},
};
// MLRNModule crashes at load time when NativeModules.MLRNModule is absent.
jest.mock('@maplibre/maplibre-react-native', () => ({
MapView: 'MapView',
Camera: 'Camera',
MarkerView: 'MarkerView',
UserLocation: 'UserLocation',
ShapeSource: 'ShapeSource',
LineLayer: 'LineLayer',
setAccessToken: jest.fn(),
setTelemetryEnabled: jest.fn(),
LineJoin: {Round: 'round', Bevel: 'bevel', Miter: 'miter'},
LineCap: {Round: 'round', Butt: 'butt', Square: 'square'},
}));
// `ComapeoCoreModule.ts` calls `requireNativeModule("ComapeoCore")` at
// import time, which throws in tests since there's no native module and
// no `mocks/ComapeoCore.js` for jest-expo to pick up. Tests that need a real
// backend build one themselves and pass it in as `mapeoApi`.
jest.mock('@comapeo/core-react-native', () => {
const grantedPermission = {
status: 'granted',
expires: 'never',
granted: true,
canAskAgain: true,
};
return {
comapeo: {},
state: {addListener: jest.fn(() => ({remove: jest.fn()}))},
comapeoServicesClient: {mapServer: {getBaseUrl: jest.fn()}},
getNotificationPermissionsAsync: jest.fn(() =>
Promise.resolve(grantedPermission),
),
requestNotificationPermissionsAsync: jest.fn(() =>
Promise.resolve(grantedPermission),
),
};
});
jest.mock('@comapeo/core-react-native/sentry', () => ({
sentryConfig: {},
getDiagnosticsEnabled: jest.fn(() => false),
setDiagnosticsEnabled: jest.fn(() => Promise.resolve()),
getApplicationUsageData: jest.fn(() => false),
setApplicationUsageData: jest.fn(() => Promise.resolve()),
getDebugEnabled: jest.fn(() => false),
setDebugEnabled: jest.fn(() => Promise.resolve()),
getRootUserId: jest.fn(() => 'TEST-TEST-TEST'),
initSentry: jest.fn(),
}));
jest.mock('react-native-vision-camera', () => {
const React = jest.requireActual('react');
// Camera has to be a component with properties
// because it has to check for permission status.
const Camera = Object.assign(
React.forwardRef((props, ref) =>
React.createElement('Camera', {...props, ref}),
),
{
getCameraPermissionStatus: jest.fn(() => 'granted'),
requestCameraPermission: jest.fn(() => Promise.resolve('granted')),
},
);
return {
Camera,
useCameraDevice: jest.fn(() => undefined),
};
});
function temporaryDirectory() {
const result = path.join(
os.tmpdir(),
'comapeo-mobile-tests-' + randomBytes(16).toString('hex'),
);
fs.mkdirSync(result);
afterAll(async () => {
await fs.promises.rm(result, {force: true, recursive: true, maxRetries: 3});
});
return result;
}
// `expo-file-system` is already mocked, but we need to extend it.
const FileSystem = jest.requireMock('expo-file-system');
FileSystem.documentDirectory = url
.pathToFileURL(temporaryDirectory())
.toString();