-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
LogSerialization-test.native.tsx
269 lines (230 loc) · 10.3 KB
/
LogSerialization-test.native.tsx
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import Constants from 'expo-constants';
import React from 'react';
import symbolicateStackTrace from 'react-native/Libraries/Core/Devtools/symbolicateStackTrace';
import TestRenderer from 'react-test-renderer';
import LogSerialization from '../LogSerialization';
import { LogErrorData } from '../RemoteLogging';
jest.mock('react-native/Libraries/Core/Devtools/symbolicateStackTrace', () =>
jest.fn(async stack =>
// We don't test symbolication itself in this test, partly because it's complex
({ stack })
)
);
jest.mock('expo-constants', () => ({
manifest: {
developer: {
projectRoot: '/home/test/project',
},
},
}));
it(`serializes simple log messages`, async () => {
const result = await LogSerialization.serializeLogDataAsync(
['hi', 1, true, 0, '', null, false, undefined],
'info'
);
expect(result.body).toEqual(['hi', '1', 'true', '0', '', 'null', 'false', 'undefined']);
expect(result.includesStack).toBeFalsy();
});
it(`serializes nested objects`, async () => {
const result = await LogSerialization.serializeLogDataAsync(
[{ outer: { inner: [[], {}] } }],
'info'
);
expect(result.body).toMatchSnapshot();
expect(result.includesStack).toBeFalsy();
});
it(`serializes cyclic objects`, async () => {
const object: { [key: string]: any } = {};
object.self = object;
const result = await LogSerialization.serializeLogDataAsync([object], 'info');
expect(result.body).toMatchSnapshot();
expect(result.body[0]).toMatch('Circular');
expect(result.includesStack).toBeFalsy();
});
it(`serializes functions`, async () => {
const result = await LogSerialization.serializeLogDataAsync(
[function test() {}, () => {}],
'info'
);
expect(result.body).toEqual(['[Function test]', '[Function anonymous]']);
expect(result.includesStack).toBeFalsy();
});
it(`serializes symbols`, async () => {
const result = await LogSerialization.serializeLogDataAsync(
[Symbol('test'), Symbol.iterator],
'info'
);
expect(result.body).toEqual(['Symbol(test)', 'Symbol(Symbol.iterator)']);
expect(result.includesStack).toBeFalsy();
});
it(`serializes promises`, async () => {
const result = await LogSerialization.serializeLogDataAsync(
[Promise.resolve('test'), Promise.reject(new Error('Expected'))],
'info'
);
expect(result.body.length).toBe(2);
expect(result.body[0]).toMatch('Promise');
expect(result.body[1]).toMatch('Promise');
expect(result.includesStack).toBeFalsy();
});
it(`serializes React elements`, async () => {
class TestComponent extends React.Component {
render() {
return <TestComponent />;
}
}
const result = await LogSerialization.serializeLogDataAsync([<TestComponent />], 'info');
expect(Array.isArray(result.body)).toBe(true);
expect(result.includesStack).toBeFalsy();
});
it(`serializes React components (refs)`, async () => {
class TestComponent extends React.Component {
child: React.Component | null = null;
render() {
return <ChildComponent ref={component => (this.child = component)} />;
}
}
class ChildComponent extends React.Component {
render() {
return 'test';
}
}
const testRenderer = TestRenderer.create(<TestComponent />);
const result = await LogSerialization.serializeLogDataAsync(
[testRenderer.root.instance.child],
'info'
);
expect(Array.isArray(result.body)).toBe(true);
expect(result.body[0]).toMatch('ChildComponent');
expect(result.includesStack).toBeFalsy();
});
describe('with stack trace support in Expo CLI', () => {
it(`includes a symbolicated stack trace when logging an error`, async () => {
const mockError = _getMockError('Test error');
const result = await LogSerialization.serializeLogDataAsync([mockError], 'info');
expect(symbolicateStackTrace).toHaveBeenCalledTimes(1);
expect(Array.isArray(result.body)).toBe(true);
expect(result.body.length).toBe(1);
expect((result.body[0] as LogErrorData).message).toBe('Test error');
expect((result.body[0] as LogErrorData).stack).toMatchSnapshot();
expect(result.includesStack).toBe(true);
});
it(`can symbolicate errors from V8`, async () => {
const mockError = _getMockV8Error('Test error');
const result = await LogSerialization.serializeLogDataAsync([mockError], 'info');
expect(symbolicateStackTrace).toHaveBeenCalledTimes(1);
expect(Array.isArray(result.body)).toBe(true);
expect(result.body.length).toBe(1);
expect((result.body[0] as LogErrorData).message).toBe('Test error');
expect((result.body[0] as LogErrorData).stack).toMatch('_exampleFunction');
expect(result.includesStack).toBe(true);
});
it(`includes a symbolicated stack trace when warning`, async () => {
const result = await LogSerialization.serializeLogDataAsync(['warning message'], 'warn');
expect(symbolicateStackTrace).toHaveBeenCalledTimes(1);
expect(Array.isArray(result.body)).toBe(true);
expect(result.body.length).toBe(1);
expect((result.body[0] as LogErrorData).message).toBe('warning message');
expect((result.body[0] as LogErrorData).stack).toBeDefined();
expect(result.includesStack).toBe(true);
});
it(`includes a symbolicated stack trace when erroring`, async () => {
const result = await LogSerialization.serializeLogDataAsync(['error message'], 'error');
expect(symbolicateStackTrace).toHaveBeenCalledTimes(1);
expect(Array.isArray(result.body)).toBe(true);
expect(result.body.length).toBe(1);
expect((result.body[0] as LogErrorData).message).toBe('error message');
expect((result.body[0] as LogErrorData).stack).toBeDefined();
expect(result.includesStack).toBe(true);
});
it(`uses the provided error's stack trace when erroring`, async () => {
const mockError = _getMockError('Test error');
const result = await LogSerialization.serializeLogDataAsync([mockError], 'error');
expect(symbolicateStackTrace).toHaveBeenCalledTimes(1);
expect(Array.isArray(result.body)).toBe(true);
expect(result.body.length).toBe(1);
expect((result.body[0] as LogErrorData).message).toBe('Test error');
expect((result.body[0] as LogErrorData).stack).toMatch('_exampleFunction');
});
it(`symbolicates unhandled promise rejections`, async () => {
const warningMessage = _getMockUnhandledPromiseRejection();
const result = await LogSerialization.serializeLogDataAsync([warningMessage], 'warn');
expect(symbolicateStackTrace).toHaveBeenCalledTimes(1);
expect(Array.isArray(result.body)).toBe(true);
expect(result.body.length).toBe(1);
expect((result.body[0] as LogErrorData).message).toMatch('Unhandled promise rejection: ');
expect((result.body[0] as LogErrorData).stack).toBeDefined();
expect(result.includesStack).toBe(true);
});
it(`doesn't fail if the error has no stack frames`, async () => {
const mockError = new Error('Test error');
mockError.stack = mockError.stack!.split('\n')[0];
const result = await LogSerialization.serializeLogDataAsync([mockError], 'info');
expect(result).toMatchSnapshot();
});
it(`doesn't fail if the error stack property is missing`, async () => {
const mockError = new Error('Test error');
mockError.stack = undefined;
const result = await LogSerialization.serializeLogDataAsync([mockError], 'info');
expect(result).toMatchSnapshot();
});
it(`doesn't fail if symbolication fails`, async () => {
(symbolicateStackTrace as jest.Mock).mockImplementationOnce(async () => {
throw new Error('Intentional symbolication error');
});
const mockError = _getMockError('Test error');
const result = await LogSerialization.serializeLogDataAsync([mockError], 'error');
expect(symbolicateStackTrace).toHaveBeenCalledTimes(1);
expect(Array.isArray(result.body)).toBe(true);
expect(result.body.length).toBe(1);
expect((result.body[0] as LogErrorData).message).toBe('Test error');
expect((result.body[0] as LogErrorData).stack).toMatchSnapshot();
expect(result.includesStack).toBe(true);
});
});
describe(`without stack trace support in Expo CLI`, () => {
let originalProjectRoot;
beforeAll(() => {
if (!Constants.manifest.developer) {
throw new Error('Constants.manifest.developer is not defined');
}
originalProjectRoot = Constants.manifest.developer.projectRoot;
delete Constants.manifest.developer.projectRoot;
});
afterAll(() => {
if (!Constants.manifest.developer) {
throw new Error('Constants.manifest.developer is not defined');
}
Constants.manifest.developer.projectRoot = originalProjectRoot;
});
it(`doesn't capture a stack trace`, async () => {
const result = await LogSerialization.serializeLogDataAsync(['oh no'], 'error');
expect(result.includesStack).toBeFalsy();
expect(symbolicateStackTrace).not.toHaveBeenCalled();
});
});
function _getMockError(message) {
const error = new Error(message);
error.stack = `_exampleFunction@/home/test/project/App.js:125:13
_depRunCallbacks@/home/test/project/node_modules/dep/index.js:77:45
tryCallTwo@/home/test/project/node_modules/react-native/node_modules/promise/lib/core.js:45:5
doResolve@/home/test/project/node_modules/react-native/node_modules/promise/lib/core.js:200:13`;
return error;
}
function _getMockV8Error(message) {
const error = new Error(message);
const mockStack = `
at _exampleFunction (/home/test/project/App.js:125:13)
at _depRunCallbacks (/home/test/project/node_modules/dep/index.js:77:45)
at tryCallTwo (/home/test/project/node_modules/react-native/node_modules/promise/lib/core.js:45:5)
at doResolve (/home/test/project/node_modules/react-native/node_modules/promise/lib/core.js:200:13)`;
error.stack = error.stack!.split('\n')[0] + mockStack;
return error;
}
function _getMockUnhandledPromiseRejection() {
return `Possible Unhandled Promise Rejection (id: 0):
Error: Intentionally unhandled async error
_callee$@http://localhost:19001/entry.bundle?platform=ios&dev=true&strict=false&minify=false&hot=false&assetPlugin=/home/test/project/node_modules/expo/tools/hashAssetFiles:99344:32
tryCatch@http://localhost:19001/entry.bundle?platform=ios&dev=true&strict=false&minify=false&hot=false&assetPlugin=/home/test/project/node_modules/expo/tools/hashAssetFiles:12301:44
invoke@http://localhost:19001/entry.bundle?platform=ios&dev=true&strict=false&minify=false&hot=false&assetPlugin=/home/test/project/node_modules/expo/tools/hashAssetFiles:12489:30`;
}