-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathReactErrorBoundary.test.js
More file actions
239 lines (208 loc) · 8.19 KB
/
ReactErrorBoundary.test.js
File metadata and controls
239 lines (208 loc) · 8.19 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
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
/**
* @jest-environment jsdom
*/
"use strict";
const { createOverlay } = require("../../client-src/overlay");
describe("createOverlay", () => {
const originalDocument = global.document;
const originalWindow = global.window;
beforeEach(() => {
global.document = {
createElement: jest.fn(() => {
return {
style: {},
appendChild: jest.fn(),
addEventListener: jest.fn(),
contentDocument: {
createElement: jest.fn(() => {
return { style: {}, appendChild: jest.fn() };
}),
body: { appendChild: jest.fn() },
},
};
}),
body: { appendChild: jest.fn(), removeChild: jest.fn() },
};
global.window = {
// Keep addEventListener mocked for other potential uses
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
// Mock trustedTypes
trustedTypes: null,
// Mock dispatchEvent
dispatchEvent: jest.fn(),
};
jest.useFakeTimers();
});
afterEach(() => {
global.document = originalDocument;
global.window = originalWindow;
jest.useRealTimers();
jest.clearAllMocks();
});
it("should not show overlay for errors caught by React error boundaries", () => {
const options = { trustedTypesPolicyName: null, catchRuntimeError: true };
const overlay = createOverlay(options);
const showOverlayMock = jest.spyOn(overlay, "send");
const reactError = new Error(
"Error inside React render\n" +
" at Boom (webpack:///./src/index.jsx?:41:11)\n" +
" at renderWithHooks (webpack:///./node_modules/react-dom/cjs/react-dom.development.js?:16305:18)\n" +
" at mountIndeterminateComponent (webpack:///./node_modules/react-dom/cjs/react-dom.development.js?:20069:13)\n" +
" at beginWork (webpack:///./node_modules/react-dom/cjs/react-dom.development.js?:21582:16)\n" +
" at HTMLUnknownElement.callCallback (webpack:///./node_modules/react-dom/cjs/react-dom.development.js?:4164:14)\n" +
" at Object.invokeGuardedCallbackDev (webpack:///./node_modules/react-dom/cjs/react-dom.development.js?:4213:16)\n" +
" at invokeGuardedCallback (webpack:///./node_modules/react-dom/cjs/react-dom.development.js?:4277:31)\n" +
" at beginWork$1 (webpack:///./node_modules/react-dom/cjs/react-dom.development.js?:27446:7)\n" +
" at performUnitOfWork (webpack:///./node_modules/react-dom/cjs/react-dom.development.js?:26555:12)\n" +
" at workLoopSync (webpack:///./node_modules/react-dom/cjs/react-dom.development.js?:26461:5)",
);
reactError._suppressLogging = true;
const errorEvent = new ErrorEvent("error", {
error: reactError,
message: reactError.message,
});
window.dispatchEvent(errorEvent);
expect(showOverlayMock).not.toHaveBeenCalled();
showOverlayMock.mockRestore();
});
it("should show overlay for normal uncaught errors", () => {
const options = { trustedTypesPolicyName: null, catchRuntimeError: true };
const overlay = createOverlay(options);
const showOverlayMock = jest.spyOn(overlay, "send");
const regularError = new Error(
"Error inside React render\n" +
" at Boom (webpack:///./src/index.jsx?:41:11)\n" +
" at renderWithHooks (webpack:///./node_modules/react-dom/cjs/react-dom.development.js?:16305:18)\n" +
" at mountIndeterminateComponent (webpack:///./node_modules/react-dom/cjs/react-dom.development.js?:20069:13)\n" +
" at beginWork (webpack:///./node_modules/react-dom/cjs/react-dom.development.js?:21582:16)\n" +
" at HTMLUnknownElement.callCallback (webpack:///./node_modules/react-dom/cjs/react-dom.development.js?:4164:14)\n",
);
const errorEvent = new ErrorEvent("error", {
error: regularError,
message: "Regular test error message",
});
window.dispatchEvent(errorEvent);
expect(showOverlayMock).toHaveBeenCalledWith({
type: "RUNTIME_ERROR",
messages: [
{
message: regularError.message,
stack: expect.anything(),
},
],
});
showOverlayMock.mockRestore();
});
it("should show overlay for normal uncaught errors (when null is thrown)", () => {
const options = { trustedTypesPolicyName: null, catchRuntimeError: true };
const overlay = createOverlay(options);
const showOverlayMock = jest.spyOn(overlay, "send");
const errorEvent = new ErrorEvent("error", {
error: null,
message: "error",
});
window.dispatchEvent(errorEvent);
expect(showOverlayMock).toHaveBeenCalledWith({
type: "RUNTIME_ERROR",
messages: [
{
message: "error",
stack: expect.anything(),
},
],
});
showOverlayMock.mockRestore();
});
it("should show overlay for normal uncaught errors when catchRuntimeError is a function that return true", () => {
const options = {
trustedTypesPolicyName: null,
catchRuntimeError: () => true,
};
const overlay = createOverlay(options);
const showOverlayMock = jest.spyOn(overlay, "send");
const regularError = new Error("Regular test error");
const errorEvent = new ErrorEvent("error", {
error: regularError,
message: "Regular test error message",
});
window.dispatchEvent(errorEvent);
expect(showOverlayMock).toHaveBeenCalledWith({
type: "RUNTIME_ERROR",
messages: [
{
message: regularError.message,
stack: expect.anything(),
},
],
});
showOverlayMock.mockRestore();
});
it("should not show overlay for normal uncaught errors when catchRuntimeError is a function that return false", () => {
const options = {
trustedTypesPolicyName: null,
catchRuntimeError: () => false,
};
const overlay = createOverlay(options);
const showOverlayMock = jest.spyOn(overlay, "send");
const regularError = new Error("Regular test error");
const errorEvent = new ErrorEvent("error", {
error: regularError,
message: "Regular test error message",
});
window.dispatchEvent(errorEvent);
expect(showOverlayMock).not.toHaveBeenCalled();
showOverlayMock.mockRestore();
});
it("should not show the overlay for errors with stack containing 'invokeGuardedCallbackDev'", () => {
const options = { trustedTypesPolicyName: null, catchRuntimeError: true };
const overlay = createOverlay(options);
const showOverlayMock = jest.spyOn(overlay, "send");
const reactInternalError = new Error("React internal error");
reactInternalError.stack = "invokeGuardedCallbackDev\n at somefile.js";
const errorEvent = new ErrorEvent("error", {
error: reactInternalError,
message: "React internal error",
});
window.dispatchEvent(errorEvent);
expect(showOverlayMock).not.toHaveBeenCalled();
showOverlayMock.mockRestore();
});
it("should show overlay for unhandled rejections", () => {
const options = { trustedTypesPolicyName: null, catchRuntimeError: true };
const overlay = createOverlay(options);
const showOverlayMock = jest.spyOn(overlay, "send");
const rejectionReason = new Error("Promise rejection reason");
const rejectionEvent = new Event("unhandledrejection");
rejectionEvent.reason = rejectionReason;
window.dispatchEvent(rejectionEvent);
expect(showOverlayMock).toHaveBeenCalledWith({
type: "RUNTIME_ERROR",
messages: [
{
message: rejectionReason.message,
stack: expect.anything(),
},
],
});
showOverlayMock.mockRestore();
});
it("should show overlay for unhandled rejections with string reason", () => {
const options = { trustedTypesPolicyName: null, catchRuntimeError: true };
const overlay = createOverlay(options);
const showOverlayMock = jest.spyOn(overlay, "send");
const rejectionEvent = new Event("unhandledrejection");
rejectionEvent.reason = "some reason";
window.dispatchEvent(rejectionEvent);
expect(showOverlayMock).toHaveBeenCalledWith({
type: "RUNTIME_ERROR",
messages: [
{
message: "some reason",
stack: expect.anything(),
},
],
});
showOverlayMock.mockRestore();
});
});