Skip to content

Commit 9b23ef3

Browse files
timfishAbhiPrasad
authored andcommitted
ref(core): Test mutation of attachments in hint (#5140)
1 parent 918d470 commit 9b23ef3

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

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

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

packages/core/test/mocks/integration.ts

+13
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,16 @@ 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+
hint.attachments = [...(hint.attachments || []), { filename: 'integration.file', data: 'great content!' }];
34+
return event;
35+
});
36+
}
37+
}

0 commit comments

Comments
 (0)