Skip to content

Commit dca9c03

Browse files
committed
Start adding tests
1 parent 457b806 commit dca9c03

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

packages/core/test/lib/hint.test.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { getDefaultTestClientOptions, TestClient } from '../mocks/client';
2+
import { AddAttachmentTestIntegration } from '../mocks/integration';
3+
import { initAndBind } from '../../src/sdk';
4+
import { captureEvent } from '@sentry/hub';
5+
6+
const PUBLIC_DSN = 'https://username@domain/123';
7+
const sendEvent = jest.spyOn(TestClient.prototype, 'sendEvent');
8+
9+
describe('Hint', () => {
10+
beforeEach(() => {
11+
TestClient.sendEventCalled = undefined;
12+
TestClient.instance = undefined;
13+
});
14+
15+
afterEach(() => {
16+
jest.clearAllMocks();
17+
});
18+
19+
test('can be mutated in beforeSend', () => {
20+
expect.assertions(1);
21+
22+
const options = getDefaultTestClientOptions({
23+
dsn: PUBLIC_DSN,
24+
beforeSend: (event, hint) => {
25+
if (hint) {
26+
hint.attachments = [...(hint?.attachments || []), { filename: 'another.file', data: 'more text' }];
27+
}
28+
29+
return event;
30+
},
31+
});
32+
33+
const client = new TestClient(options);
34+
client.captureEvent({});
35+
36+
const [, hint] = sendEvent.mock.calls[0];
37+
38+
expect(hint).toEqual({
39+
attachments: [{ filename: 'another.file', data: 'more text' }],
40+
});
41+
});
42+
43+
test('gets passed through to beforeSend and can be further mutated', () => {
44+
expect.assertions(1);
45+
46+
const options = getDefaultTestClientOptions({
47+
dsn: PUBLIC_DSN,
48+
beforeSend: (event, hint) => {
49+
if (hint) {
50+
hint.attachments = [...(hint?.attachments || []), { filename: 'another.file', data: 'more text' }];
51+
}
52+
53+
return event;
54+
},
55+
});
56+
57+
const client = new TestClient(options);
58+
client.captureEvent({}, { attachments: [{ filename: 'some-file.txt', data: 'Hello' }] });
59+
60+
const [, hint] = sendEvent.mock.calls[0];
61+
62+
expect(hint).toEqual({
63+
attachments: [
64+
{ filename: 'some-file.txt', data: 'Hello' },
65+
{ filename: 'another.file', data: 'more text' },
66+
],
67+
});
68+
});
69+
70+
test('can be mutated by an integration via event processor', () => {
71+
expect.assertions(1);
72+
73+
const options = getDefaultTestClientOptions({
74+
dsn: PUBLIC_DSN,
75+
integrations: [new AddAttachmentTestIntegration()],
76+
});
77+
78+
initAndBind(TestClient, options);
79+
80+
captureEvent({});
81+
82+
const [, hint] = sendEvent.mock.calls[0];
83+
84+
expect(hint?.attachments).toEqual([{ filename: 'integration.file', data: 'great content!' }]);
85+
});
86+
});

packages/core/test/mocks/integration.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,19 @@ export class TestIntegration implements Integration {
2222
});
2323
}
2424
}
25+
26+
export class AddAttachmentTestIntegration implements Integration {
27+
public static id: string = 'AddAttachmentTestIntegration';
28+
29+
public name: string = 'AddAttachmentTestIntegration';
30+
31+
public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void): void {
32+
addGlobalEventProcessor((event, hint) => {
33+
if (hint) {
34+
hint.attachments = [...(hint?.attachments || []), { filename: 'integration.file', data: 'great content!' }];
35+
}
36+
37+
return event;
38+
});
39+
}
40+
}

0 commit comments

Comments
 (0)