8000 fix: linting and tests · DanielGibbsNZ/sentry-javascript@70dafe3 · GitHub
Skip to content

Commit 70dafe3

Browse files
committed
fix: linting and tests
1 parent 53b4744 commit 70dafe3

File tree

9 files changed

+232
-151
lines changed

9 files changed

+232
-151
lines changed

packages/browser/test/backend.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('BrowserBackend', () => {
1818
backend = new BrowserBackend({ dsn });
1919

2020
try {
21-
await backend.sendEvent(testEvent);
21+
backend.sendEvent(testEvent);
2222
} catch (e) {
2323
expect(e.message).equal('Cannot sendEvent without a valid Dsn');
2424
}

packages/browser/test/integrations/linkederrors.test.ts

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,23 @@ describe('LinkedErrors', () => {
6464

6565
const originalException = one;
6666
const backend = new BrowserBackend({});
67-
const event = await backend.eventFromException(originalException);
68-
const result = linkedErrors.handler(event, {
69-
originalException,
67+
return backend.eventFromException(originalException).then(event => {
68+
const result = linkedErrors.handler(event, {
69+
originalException,
70+
});
71+
72+
// It shouldn't include root exception, as it's already processed in the event by the main error handler
73+
expect(result!.exception!.values!.length).equal(3);
74+
expect(result!.exception!.values![0].type).equal('SyntaxError');
75+
expect(result!.exception!.values![0].value).equal('three');
76+
expect(result!.exception!.values![0].stacktrace).to.have.property('frames');
77+
expect(result!.exception!.values![1].type).equal('TypeError');
78+
expect(result!.exception!.values![1].value).equal('two');
79+
expect(result!.exception!.values![1].stacktrace).to.have.property('frames');
80+
expect(result!.exception!.values![2].type).equal('Error');
81+
expect(result!.exception!.values![2].value).equal('one');
82+
expect(result!.exception!.values![2].stacktrace).to.have.property('frames');
7083
});
71-
72-
// It shouldn't include root exception, as it's already processed in the event by the main error handler
73-
expect(result!.exception!.values!.length).equal(3);
74-
expect(result!.exception!.values![0].type).equal('SyntaxError');
75-
expect(result!.exception!.values![0].value).equal('three');
76-
expect(result!.exception!.values![0].stacktrace).to.have.property('frames');
77-
expect(result!.exception!.values![1].type).equal('TypeError');
78-
expect(result!.exception!.values![1].value).equal('two');
79-
expect(result!.exception!.values![1].stacktrace).to.have.property('frames');
80-
expect(result!.exception!.values![2].type).equal('Error');
81-
expect(result!.exception!.values![2].value).equal('one');
82-
expect(result!.exception!.values![2].stacktrace).to.have.property('frames');
8384
});
8485

8586
it('should allow to change walk key', async () => {
@@ -97,21 +98,22 @@ describe('LinkedErrors', () => {
9798

9899
const originalException = one;
99100
const backend = new BrowserBackend({});
100-
const event = await backend.eventFromException(originalException);
101-
const result = linkedErrors.handler(event, {
102-
originalException,
101+
return backend.eventFromException(originalException).then(event => {
102+
const result = linkedErrors.handler(event, {
103+
originalException,
104+
});
105+
106+
expect(result!.exception!.values!.length).equal(3);
107+
expect(result!.exception!.values![0].type).equal('SyntaxError');
108+
expect(result!.exception!.values![0].value).equal('three');
109+
expect(result!.exception!.values![0].stacktrace).to.have.property('frames');
110+
expect(result!.exception!.values![1].type).equal('TypeError');
111+
expect(result!.exception!.values![1].value).equal('two');
112+
expect(result!.exception!.values![1].stacktrace).to.have.property('frames');
113+
expect(result!.exception!.values![2].type).equal('Error');
114+
expect(result!.exception!.values![2].value).equal('one');
115+
expect(result!.exception!.values![2].stacktrace).to.have.property('frames');
103116
});
104-
105-
expect(result!.exception!.values!.length).equal(3);
106-
expect(result!.exception!.values![0].type).equal('SyntaxError');
107-
expect(result!.exception!.values![0].value).equal('three');
108-
expect(result!.exception!.values![0].stacktrace).to.have.property('frames');
109-
expect(result!.exception!.values![1].type).equal('TypeError');
110-
expect(result!.exception!.values![1].value).equal('two');
111-
expect(result!.exception!.values![1].stacktrace).to.have.property('frames');
112-
expect(result!.exception!.values![2].type).equal('Error');
113-
expect(result!.exception!.values![2].value).equal('one');
114-
expect(result!.exception!.values![2].stacktrace).to.have.property('frames');
115117
});
116118

117119
it('should allow to change stack size limit', async () => {
@@ -126,18 +128,19 @@ describe('LinkedErrors', () => {
126128
two.cause = three;
127129

128130
const backend = new BrowserBackend({});
129-
const event = await backend.eventFromException(one);
130-
const result = linkedErrors.handler(event, {
131-
originalException: one,
131+
return backend.eventFromException(one).then(event => {
132+
const result = linkedErrors.handler(event, {
133+
originalException: one,
134+
});
135+
136+
expect(result!.exception!.values!.length).equal(2);
137+
expect(result!.exception!.values![0].type).equal('TypeError');
138+
expect(result!.exception!.values![0].value).equal('two');
139+
expect(result!.exception!.values![0].stacktrace).to.have.property('frames');
140+
expect(result!.exception!.values![1].type).equal('Error');
141+
expect(result!.exception!.values![1].value).equal('one');
142+
expect(result!.exception!.values![1].stacktrace).to.have.property('frames');
132143
});
133-
134-
expect(result!.exception!.values!.length).equal(2);
135-
expect(result!.exception!.values![0].type).equal('TypeError');
136-
expect(result!.exception!.values![0].value).equal('two');
137-
expect(result!.exception!.values![0].stacktrace).to.have.property('frames');
138-
expect(result!.exception!.values![1].type).equal('Error');
139-
expect(result!.exception!.values![1].value).equal('one');
140-
expect(result!.exception!.values![1].stacktrace).to.have.property('frames');
141144
});
142145
});
143146
});

packages/core/src/baseclient.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -329,19 +329,23 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
329329
if ((typeof beforeSendResult as any) === 'undefined') {
330330
logger.error('`beforeSend` method has to return `null` or a valid event.');
331331
} else if (isThenable(beforeSendResult)) {
332-
(beforeSendResult as Promise<SentryEvent | null>).then(processedEvent => {
333-
finalEvent = processedEvent;
334-
335-
if (finalEvent === null) {
336-
logger.log('`beforeSend` returned `null`, will not send event.');
337-
resolve(null);
338-
return;
339-
}
340-
341-
// From here on we are really async
342-
this.getBackend().sendEvent(finalEvent);
343-
resolve(finalEvent);
344-
});
332+
(beforeSendResult as Promise<SentryEvent | null>)
333+
.then(processedEvent => {
334+
finalEvent = processedEvent;
335+
336+
if (finalEvent === null) {
337+
logger.log('`beforeSend` returned `null`, will not send event.');
338+
resolve(null);
339+
return;
340+
}
341+
342+
// From here on we are really async
343+
this.getBackend().sendEvent(finalEvent);
344+
resolve(finalEvent);
345+
})
346+
.catch(e => {
347+
logger.error(`beforeSend rejected with ${e}`);
348+
});
345349
} else {
346350
finalEvent = beforeSendResult as SentryEvent | null;
347351

packages/core/test/lib/base.test.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,9 @@ describe('BaseClient', () => {
383383
const beforeSend = jest.fn(
384384
async event =>
385385
new Promise<SentryEvent>(resolve => {
386-
setTimeout(() => resolve(event), 1);
386+
setTimeout(() => {
387+
resolve(event);
388+
}, 1);
387389
}),
388390
);
389391
const client = new TestClient({ dsn: PUBLIC_DSN, beforeSend });
@@ -402,9 +404,11 @@ describe('BaseClient', () => {
402404
jest.useFakeTimers();
403405
expect.assertions(1);
404406
const beforeSend = jest.fn(
405-
() =>
407+
async () =>
406408
new Promise<SentryEvent>(resolve => {
407-
setTimeout(() => resolve({ message: 'changed2' }), 1);
409+
setTimeout(() => {
410+
resolve({ message: 'changed2' });
411+
}, 1);
408412
}),
409413
);
410414

@@ -424,9 +428,11 @@ describe('BaseClient', () => {
424428
jest.useFakeTimers();
425429
expect.assertions(1);
426430
const beforeSend = jest.fn(
427-
() =>
431+
async () =>
428432
new Promise<null>(resolve => {
429-
setTimeout(() => resolve(null));
433+
setTimeout(() => {
434+
resolve(null);
435+
});
430436
}),
431437
);
432438
const client = new TestClient({ dsn: PUBLIC_DSN, beforeSend });

packages/core/test/mocks/backend.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export class TestBackend extends BaseBackend<TestOptions> {
4545

4646
public sendEvent(event: SentryEvent): void {
4747
this.event = event;
48+
// tslint:disable-next-line
4849
TestBackend.sendEventCalled && TestBackend.sendEventCalled(event);
4950
}
5051
}
Lines changed: 82 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { NodeBackend } from '../../src';
1+
import { NodeBackend, SentryEvent } from '../../src';
22
import { LinkedErrors } from '../../src/integrations/linkederrors';
33

44
let linkedErrors: LinkedErrors;
@@ -14,26 +14,37 @@ describe('LinkedErrors', () => {
1414

1515
describe('handler', () => {
1616
it('should bail out if event doesnt contain exception', async () => {
17+
expect.assertions(2);
1718
const spy = jest.spyOn(linkedErrors, 'walkErrorTree');
1819
const event = {
1920
message: 'foo',
2021
};
21-
const result = await linkedErrors.handler(event);
22-
expect(spy.mock.calls.length).toEqual(0);
23-
expect(result).toEqual(event);
22+
return linkedErrors.handler(event).then(result => {
23+
expect(spy.mock.calls.length).toEqual(0);
24+
expect(result).toEqual(event);
25+
});
2426
});
2527

2628
it('should bail out if event contains exception, but no hint', async () => {
29+
expect.assertions(2);
2730
const spy = jest.spyOn(linkedErrors, 'walkErrorTree');
2831
const one = new Error('originalException');
2932
const backend = new NodeBackend({});
30-
const event = await backend.eventFromException(one);
31-
const result = await linkedErrors.handler(event);
32-
expect(spy.mock.calls.length).toEqual(0);
33-
expect(result).toEqual(event);
33+
let event: SentryEvent | undefined;
34+
return backend
35+
.eventFromException(one)
36+
.then(eventFromException => {
37+
event = eventFromException;
38+
return linkedErrors.handler(eventFromException);
39+
})
40+
.then(result => {
41+
expect(spy.mock.calls.length).toEqual(0);
42+
expect(result).toEqual(event);
43+
});
3444
});
3545

3646
it('should call walkErrorTree if event contains exception and hint with originalException', async () => {
47+
expect.assertions(1);
3748
const spy = jest.spyOn(linkedErrors, 'walkErrorTree').mockImplementation(
3849
async () =>
3950
new Promise<[]>(resolve => {
@@ -42,39 +53,48 @@ describe('LinkedErrors', () => {
4253
);
4354
const one = new Error('originalException');
4455
const backend = new NodeBackend({});
45-
const event = await backend.eventFromException(one);
46-
await linkedErrors.handler(event, {
47-
originalException: one,
48-
});
49-
expect(spy.mock.calls.length).toEqual(1);
56+
return backend.eventFromException(one).then(event =>
57+
linkedErrors
58+
.handler(event, {
59+
originalException: one,
60+
})
61+
.then(_ => {
62+
expect(spy.mock.calls.length).toEqual(1);
63+
}),
64+
);
5065
});
5166

5267
it('should recursively walk error to find linked exceptions and assign them to the event', async () => {
68+
expect.assertions(10);
5369
const one: ExtendedError = new Error('one');
5470
const two: ExtendedError = new TypeError('two');
5571
const three: ExtendedError = new SyntaxError('three');
5672
one.cause = two;
5773
two.cause = three;
5874

5975
const backend = new NodeBackend({});
60-
const event = await backend.eventFromException(one);
61-
const result = await linkedErrors.handler(event, {
62-
originalException: one,
63-
});
64-
65-
expect(result!.exception!.values!.length).toEqual(3);
66-
expect(result!.exception!.values![0].type).toEqual('SyntaxError');
67-
expect(result!.exception!.values![0].value).toEqual('three');
68-
expect(result!.exception!.values![0].stacktrace).toHaveProperty('frames');
69-
expect(result!.exception!.values![1].type).toEqual('TypeError');
70-
expect(result!.exception!.values![1].value).toEqual('two');
71-
expect(result!.exception!.values![1].stacktrace).toHaveProperty('frames');
72-
expect(result!.exception!.values![2].type).toEqual('Error');
73-
expect(result!.exception!.values![2].value).toEqual('one');
74-
expect(result!.exception!.values![2].stacktrace).toHaveProperty('frames');
76+
return backend.eventFromException(one).then(event =>
77+
linkedErrors
78+
.handler(event, {
79+
originalException: one,
80+
})
81+
.then(result => {
82+
expect(result!.exception!.values!.length).toEqual(3);
83+
expect(result!.exception!.values![0].type).toEqual('SyntaxError');
84+
expect(result!.exception!.values![0].value).toEqual('three');
85+
expect(result!.exception!.values![0].stacktrace).toHaveProperty('frames');
86+
expect(result!.exception!.values![1].type).toEqual('TypeError');
87+
expect(result!.exception!.values![1].value).toEqual('two');
88+
expect(result!.exception!.values![1].stacktrace).toHaveProperty('frames');
89+
expect(result!.exception!.values![2].type).toEqual('Error');
90+
expect(result!.exception!.values![2].value).toEqual('one');
91+
expect(result!.exception!.values![2].stacktrace).toHaveProperty('frames');
92+
}),
93+
);
7594
});
7695

7796
it('should allow to change walk key', async () => {
97+
expect.assertions(10);
7898
linkedErrors = new LinkedErrors({
7999
key: 'reason',
80100
});
@@ -86,24 +106,28 @@ describe('LinkedErrors', () => {
86106
two.reason = three;
87107

88108
const backend = new NodeBackend({});
89-
const event = await backend.eventFromException(one);
90-
const result = await linkedErrors.handler(event, {
91-
originalException: one,
92-
});
93-
94-
expect(result!.exception!.values!.length).toEqual(3);
95-
expect(result!.exception!.values![0].type).toEqual('SyntaxError');
96-
expect(result!.exception!.values![0].value).toEqual('three');
97-
expect(result!.exception!.values![0].stacktrace).toHaveProperty('frames');
98-
expect(result!.exception!.values![1].type).toEqual('TypeError');
99-
expect(result!.exception!.values![1].value).toEqual('two');
100-
expect(result!.exception!.values![1].stacktrace).toHaveProperty('frames');
101-
expect(result!.exception!.values![2].type).toEqual('Error');
102-
expect(result!.exception!.values![2].value).toEqual('one');
103-
expect(result!.exception!.values![2].stacktrace).toHaveProperty('frames');
109+
return backend.eventFromException(one).then(event =>
110+
linkedErrors
111+
.handler(event, {
112+
originalException: one,
113+
})
114+
.then(result => {
115+
expect(result!.exception!.values!.length).toEqual(3);
116+
expect(result!.exception!.values![0].type).toEqual('SyntaxError');
117+
expect(result!.exception!.values![0].value).toEqual('three');
118+
expect(result!.exception!.values![0].stacktrace).toHaveProperty('frames');
119+
expect(result!.exception!.values![1].type).toEqual('TypeError');
120+
expect(result!.exception!.values![1].value).toEqual('two');
121+
expect(result!.exception!.values![1].stacktrace).toHaveProperty('frames');
122+
expect(result!.exception!.values![2].type).toEqual('Error');
123+
expect(result!.exception!.values![2].value).toEqual('one');
124+
expect(result!.exception!.values![2].stacktrace).toHaveProperty('frames');
125+
}),
126+
);
104127
});
105128

106129
it('should allow to change stack size limit', async () => {
130+
expect.assertions(7);
107131
linkedErrors = new LinkedErrors({
108132
limit: 2,
109133
});
@@ -115,18 +139,21 @@ describe('LinkedErrors', () => {
115139
two.cause = three;
116140

117141
const backend = new NodeBackend({});
118-
const event = await backend.eventFromException(one);
119-
const result = await linkedErrors.handler(event, {
120-
originalException: one,
121-
});
122-
123-
expect(result!.exception!.values!.length).toEqual(2);
124-
expect(result!.exception!.values![0].type).toEqual('TypeError');
125-
expect(result!.exception!.values![0].value).toEqual('two');
126-
expect(result!.exception!.values![0].stacktrace).toHaveProperty('frames');
127-
expect(result!.exception!.values![1].type).toEqual('Error');
128-
expect(result!.exception!.values![1].value).toEqual('one');
129-
expect(result!.exception!.values![1].stacktrace).toHaveProperty('frames');
142+
return backend.eventFromException(one).then(event =>
143+
linkedErrors
144+
.handler(event, {
145+
originalException: one,
146+
})
147+
.then(result => {
148+
expect(result!.exception!.values.length).toEqual(2);
149+
expect(result!.exception!.values![0].type).toEqual('TypeError');
150+
expect(result!.exception!.values![0].value).toEqual('two');
151+
expect(result!.exception!.values![0].stacktrace).toHaveProperty('frames');
152+
expect(result!.exception!.values![1].type).toEqual('Error');
153+
expect(result!.exception!.values![1].value).toEqual('one');
154+
expect(result!.exception!.values![1].stacktrace).toHaveProperty('frames');
155+
}),
156+
);
130157
});
131158
});
132159
});

0 commit comments

Comments
 (0)
0