-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathindex.test.js
369 lines (347 loc) · 14.1 KB
/
index.test.js
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
const Raven = require("raven-js");
const createRavenMiddleware = require("./index");
const { createStore, applyMiddleware } = require("redux");
Raven.config("https://[email protected]/146969", {
allowDuplicates: true
}).install();
const reducer = (previousState = { value: 0 }, action) => {
switch (action.type) {
case "THROW":
// Raven does not seem to be able to capture global exceptions in Jest tests.
// So we explicitly wrap this error in a Raven context.
Raven.context(() => {
throw new Error("Reducer error");
});
case "INCREMENT":
return { value: previousState.value + 1 };
case "DOUBLE":
return { value: previousState.value * 2 };
default:
return previousState;
}
};
const context = {};
describe("raven-for-redux", () => {
beforeEach(() => {
context.mockTransport = jest.fn();
Raven.setTransport(context.mockTransport);
Raven.setDataCallback(undefined);
Raven.setBreadcrumbCallback(undefined);
Raven.setUserContext(undefined);
Raven._breadcrumbs = [];
Raven._globalContext = {};
});
describe("in the default configuration", () => {
beforeEach(() => {
context.middleware = createRavenMiddleware(Raven);
context.store = createStore(reducer, applyMiddleware(context.middleware));
});
it("merges Redux info with existing 'extras'", () => {
Raven.captureException(new Error("Crash!"), {
extra: { anotherValue: 10 }
});
const { extra } = context.mockTransport.mock.calls[0][0].data;
expect(extra).toMatchObject({
state: { value: 0 },
lastAction: undefined,
anotherValue: 10
// session:duration will also be defined
});
});
it("if explicitly passed extras contain a `state` property, the explicit version wins", () => {
Raven.captureException(new Error("Crash!"), {
extra: { anotherValue: 10, state: "SOME OTHER STATE" }
});
const { extra } = context.mockTransport.mock.calls[0][0].data;
expect(extra).toMatchObject({
state: "SOME OTHER STATE",
lastAction: undefined,
anotherValue: 10
// session:duration will also be defined
});
});
it("if explicitly passed extras contain a `lastAction` property, the explicit version wins", () => {
Raven.captureException(new Error("Crash!"), {
extra: { anotherValue: 10, lastAction: "SOME OTHER LAST ACTION" }
});
const { extra } = context.mockTransport.mock.calls[0][0].data;
expect(extra).toMatchObject({
state: { value: 0 },
lastAction: "SOME OTHER LAST ACTION",
anotherValue: 10
// session:duration will also be defined
});
});
it("includes the initial state when crashing/messaging before any action has been dispatched", () => {
Raven.captureMessage("report!");
expect(context.mockTransport).toHaveBeenCalledTimes(1);
const { extra } = context.mockTransport.mock.calls[0][0].data;
expect(extra.lastAction).toBe(undefined);
expect(extra.state).toEqual({ value: 0 });
});
it("returns the result of the next dispatch function", () => {
expect(context.store.dispatch({ type: "INCREMENT" })).toEqual({
type: "INCREMENT"
});
});
it("logs the last action that was dispatched", () => {
context.store.dispatch({ type: "INCREMENT" });
expect(() => {
context.store.dispatch({ type: "THROW" });
}).toThrow();
expect(context.mockTransport).toHaveBeenCalledTimes(1);
const { extra } = context.mockTransport.mock.calls[0][0].data;
expect(extra.lastAction).toEqual({ type: "THROW" });
});
it("logs the last state when crashing in the reducer", () => {
context.store.dispatch({ type: "INCREMENT" });
expect(() => {
context.store.dispatch({ type: "THROW" });
}).toThrow();
expect(context.mockTransport).toHaveBeenCalledTimes(1);
const { extra } = context.mockTransport.mock.calls[0][0].data;
expect(extra.state).toEqual({ value: 1 });
});
it("logs a breadcrumb for each action", () => {
context.store.dispatch({ type: "INCREMENT", extra: "FOO" });
expect(() => {
context.store.dispatch({ type: "THROW", extra: "BAR" });
}).toThrow();
expect(context.mockTransport).toHaveBeenCalledTimes(1);
const { breadcrumbs } = context.mockTransport.mock.calls[0][0].data;
expect(breadcrumbs.values.length).toBe(2);
expect(breadcrumbs.values[0]).toMatchObject({
category: "redux-action",
data: undefined,
message: "INCREMENT"
});
expect(breadcrumbs.values[1]).toMatchObject({
category: "redux-action",
data: undefined,
message: "THROW"
});
});
it("includes timestamps in the breadcrumbs", () => {
context.store.dispatch({ type: "INCREMENT", extra: "FOO" });
expect(() => {
context.store.dispatch({ type: "THROW", extra: "BAR" });
}).toThrow();
const { breadcrumbs } = context.mockTransport.mock.calls[0][0].data;
const firstBreadcrumb = breadcrumbs.values[1];
expect(firstBreadcrumb.timestamp).toBeLessThanOrEqual(+new Date() / 1000);
});
it("trims breadcrumbs over 100", () => {
let n = 150;
while (n--) {
context.store.dispatch({ type: "INCREMENT", extra: "FOO" });
}
expect(() => {
context.store.dispatch({ type: "THROW", extra: "BAR" });
}).toThrow();
const { breadcrumbs } = context.mockTransport.mock.calls[0][0].data;
expect(breadcrumbs.values.length).toBe(100);
});
it("preserves order of native Raven breadcrumbs & raven-for-redux breadcrumbs", async () => {
context.store.dispatch({ type: "INCREMENT", extra: "FOO" });
await new Promise(resolve => setTimeout(resolve, 100));
Raven.captureBreadcrumb({ message: "some message" });
await new Promise(resolve => setTimeout(resolve, 100));
expect(() => {
context.store.dispatch({ type: "THROW", extra: "BAR" });
}).toThrow();
const { breadcrumbs } = context.mockTransport.mock.calls[0][0].data;
expect(breadcrumbs.values.length).toBe(3);
expect(breadcrumbs.values[0]).toMatchObject({ message: "INCREMENT" });
expect(breadcrumbs.values[1]).toMatchObject({ message: "some message" });
expect(breadcrumbs.values[2]).toMatchObject({ message: "THROW" });
});
it("includes the last state/action when crashing/reporting outside the reducer", () => {
context.store.dispatch({ type: "INCREMENT" });
context.store.dispatch({ type: "INCREMENT" });
context.store.dispatch({ type: "DOUBLE" });
Raven.captureMessage("report!");
expect(context.mockTransport).toHaveBeenCalledTimes(1);
const { extra } = context.mockTransport.mock.calls[0][0].data;
expect(extra.lastAction).toEqual({ type: "DOUBLE" });
expect(extra.state).toEqual({ value: 4 });
});
it("preserves user context", () => {
const userData = { userId: 1, username: "captbaritone" };
Raven.setUserContext(userData);
expect(() => {
context.store.dispatch({ type: "THROW", extra: "BAR" });
}).toThrow();
expect(context.mockTransport.mock.calls[0][0].data.user).toEqual(
userData
);
});
});
describe("with all the options enabled", () => {
beforeEach(() => {
context.stateTransformer = jest.fn(
state => `transformed state ${state.value}`
);
context.actionTransformer = jest.fn(
action => `transformed action ${action.type}`
);
context.getUserContext = jest.fn(state => `user context ${state.value}`);
context.getTags = jest.fn(state => `tags ${state.value}`);
context.breadcrumbDataFromAction = jest.fn(action => ({
extra: action.extra
}));
context.breadcrumbMessageFromAction = jest.fn(
action => `transformed action ${action.type}`
);
context.filterBreadcrumbActions = action => {
return action.type !== "UNINTERESTING_ACTION";
};
context.store = createStore(
reducer,
applyMiddleware(
createRavenMiddleware(Raven, {
stateTransformer: context.stateTransformer,
actionTransformer: context.actionTransformer,
breadcrumbDataFromAction: context.breadcrumbDataFromAction,
breadcrumbMessageFromAction: context.breadcrumbMessageFromAction,
filterBreadcrumbActions: context.filterBreadcrumbActions,
getUserContext: context.getUserContext,
getTags: context.getTags
})
)
);
});
it("does not transform the state or action until an exception is encountered", () => {
context.store.dispatch({ type: "INCREMENT" });
expect(context.stateTransformer).not.toHaveBeenCalled();
expect(context.actionTransformer).not.toHaveBeenCalled();
});
it("transforms the action if an error is encountered", () => {
context.store.dispatch({ type: "INCREMENT" });
expect(() => {
context.store.dispatch({ type: "THROW" });
}).toThrow();
expect(context.mockTransport).toHaveBeenCalledTimes(1);
const { extra } = context.mockTransport.mock.calls[0][0].data;
expect(extra.lastAction).toEqual("transformed action THROW");
});
it("transforms the state if an error is encountered", () => {
context.store.dispatch({ type: "INCREMENT" });
expect(() => {
context.store.dispatch({ type: "THROW" });
}).toThrow();
expect(context.mockTransport).toHaveBeenCalledTimes(1);
const { extra } = context.mockTransport.mock.calls[0][0].data;
expect(extra.state).toEqual("transformed state 1");
});
it("derives breadcrumb data from action", () => {
context.store.dispatch({ type: "INCREMENT", extra: "FOO" });
expect(() => {
context.store.dispatch({ type: "THROW", extra: "BAR" });
}).toThrow();
expect(context.mockTransport).toHaveBeenCalledTimes(1);
const { breadcrumbs } = context.mockTransport.mock.calls[0][0].data;
expect(breadcrumbs.values.length).toBe(2);
expect(breadcrumbs.values[0].message).toBe(
"transformed action INCREMENT"
);
expect(breadcrumbs.values[0].data).toMatchObject({ extra: "FOO" });
expect(breadcrumbs.values[1].message).toBe("transformed action THROW");
expect(breadcrumbs.values[1].data).toMatchObject({ extra: "BAR" });
});
it("transforms the user context on data callback", () => {
context.store.dispatch({ type: "INCREMENT", extra: "FOO" });
const userData = { userId: 1, username: "captbaritone" };
Raven.setUserContext(userData);
expect(() => {
context.store.dispatch({ type: "THROW", extra: "BAR" });
}).toThrow();
expect(context.mockTransport.mock.calls[0][0].data.user).toEqual(
"user context 1"
);
});
it("transforms the tags on data callback", () => {
context.store.dispatch({ type: "INCREMENT", extra: "FOO" });
expect(() => {
context.store.dispatch({ type: "THROW", extra: "BAR" });
}).toThrow();
expect(context.mockTransport).toHaveBeenCalledTimes(1);
expect(context.mockTransport.mock.calls[0][0].data.tags).toEqual(
"tags 1"
);
});
});
describe("with multiple data callbaks", () => {
beforeEach(() => {
context.firstOriginalDataCallback = jest.fn((data, original) => {
const newData = Object.assign({}, data, {
firstData: "first"
});
return original ? original(newData) : newData;
});
context.secondOriginalDataCallback = jest.fn((data, original) => {
const newData = Object.assign({}, data, {
secondData: "second"
});
return original ? original(newData) : newData;
});
Raven.setDataCallback(context.firstOriginalDataCallback);
Raven.setDataCallback(context.secondOriginalDataCallback);
context.stateTransformer = jest.fn(
state => `transformed state ${state.value}`
);
context.store = createStore(
reducer,
applyMiddleware(
createRavenMiddleware(Raven, {
stateTransformer: context.stateTransformer
})
)
);
});
it("runs all the data callbacks given", () => {
context.store.dispatch({ type: "INCREMENT" });
expect(() => {
context.store.dispatch({ type: "THROW" });
}).toThrow();
expect(context.firstOriginalDataCallback).toHaveBeenCalledTimes(1);
expect(context.secondOriginalDataCallback).toHaveBeenCalledTimes(1);
expect(context.mockTransport).toHaveBeenCalledTimes(1);
const data = context.mockTransport.mock.calls[0][0].data;
expect(data.extra.state).toEqual("transformed state 1");
expect(data.firstData).toEqual("first");
expect(data.secondData).toEqual("second");
});
});
describe("with filterBreadcrumbActions option enabled", () => {
beforeEach(() => {
context.filterBreadcrumbActions = action => {
return action.type !== "UNINTERESTING_ACTION";
};
context.store = createStore(
reducer,
applyMiddleware(
createRavenMiddleware(Raven, {
filterBreadcrumbActions: context.filterBreadcrumbActions
})
)
);
});
it("filters actions for breadcrumbs", () => {
context.store.dispatch({ type: "INCREMENT" });
context.store.dispatch({ type: "UNINTERESTING_ACTION" });
context.store.dispatch({ type: "UNINTERESTING_ACTION" });
Raven.captureMessage("report!");
expect(context.mockTransport).toHaveBeenCalledTimes(1);
const { breadcrumbs } = context.mockTransport.mock.calls[0][0].data;
expect(breadcrumbs.values.length).toBe(1);
});
it("sends action with data.extra even if it was filtered", () => {
context.store.dispatch({ type: "UNINTERESTING_ACTION" });
Raven.captureMessage("report!");
expect(context.mockTransport).toHaveBeenCalledTimes(1);
const { extra } = context.mockTransport.mock.calls[0][0].data;
// Even though the action isn't added to breadcrumbs, it should be sent with extra data
expect(extra.lastAction).toEqual({ type: "UNINTERESTING_ACTION" });
});
});
});