|
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 | | -}); |
| 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 | + if (hint) { |
| 30 | + hint.attachments = [...(hint?.attachments || []), { filename: 'another.file', data: 'more text' }]; |
| 31 | + } |
| 32 | + |
| 33 | + return event; |
| 34 | + }, |
| 35 | + }); |
| 36 | + |
| 37 | + const client = new TestClient(options); |
| 38 | + client.captureEvent({}); |
| 39 | + |
| 40 | + const [, hint] = sendEvent.mock.calls[0]; |
| 41 | + expect(hint).toEqual({ attachments: [{ filename: 'another.file', data: 'more text' }] }); |
| 42 | + }); |
| 43 | + |
| 44 | + test('gets passed through to beforeSend and can be further mutated', () => { |
| 45 | + expect.assertions(1); |
| 46 | + |
| 47 | + const options = getDefaultTestClientOptions({ |
| 48 | + dsn: PUBLIC_DSN, |
| 49 | + beforeSend: (event, hint) => { |
| 50 | + if (hint) { |
| 51 | + hint.attachments = [...(hint?.attachments || []), { filename: 'another.file', data: 'more text' }]; |
| 52 | + } |
| 53 | + |
| 54 | + return event; |
| 55 | + }, |
| 56 | + }); |
| 57 | + |
| 58 | + const client = new TestClient(options); |
| 59 | + client.captureEvent({}, { attachments: [{ filename: 'some-file.txt', data: 'Hello' }] }); |
| 60 | + |
| 61 | + const [, hint] = sendEvent.mock.calls[0]; |
| 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 | + captureEvent({}); |
| 80 | + |
| 81 | + const [, hint] = sendEvent.mock.calls[0]; |
| 82 | + expect(hint?.attachments).toEqual([{ filename: 'integration.file', data: 'great content!' }]); |
| 83 | + }); |
| 84 | + |
| 85 | + test('get copied from scope to hint', () => { |
| 86 | + expect.assertions(1); |
| 87 | + |
| 88 | + const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN }); |
| 89 | + initAndBind(TestClient, options); |
| 90 | + |
| 91 | + configureScope(scope => scope.addAttachment({ filename: 'scope.file', data: 'great content!' })); |
| 92 | + |
| 93 | + captureEvent({}, { attachments: [{ filename: 'some-file.txt', data: 'Hello' }] }); |
| 94 | + |
| 95 | + const [, hint] = sendEvent.mock.calls[0]; |
| 96 | + expect(hint?.attachments).toEqual([ |
| 97 | + { filename: 'some-file.txt', data: 'Hello' }, |
| 98 | + { filename: 'scope.file', data: 'great content!' }, |
| 99 | + ]); |
| 100 | + }); |
| 101 | + }); |
| 102 | +}); |
0 commit comments