From dca9c0344f93c820044ec9e763accb9c3b4a4823 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Thu, 19 May 2022 18:22:35 +0100 Subject: [PATCH 1/3] Start adding tests --- packages/core/test/lib/hint.test.ts | 86 +++++++++++++++++++++++++ packages/core/test/mocks/integration.ts | 16 +++++ 2 files changed, 102 insertions(+) create mode 100644 packages/core/test/lib/hint.test.ts diff --git a/packages/core/test/lib/hint.test.ts b/packages/core/test/lib/hint.test.ts new file mode 100644 index 000000000000..7ef09655e372 --- /dev/null +++ b/packages/core/test/lib/hint.test.ts @@ -0,0 +1,86 @@ +import { getDefaultTestClientOptions, TestClient } from '../mocks/client'; +import { AddAttachmentTestIntegration } from '../mocks/integration'; +import { initAndBind } from '../../src/sdk'; +import { captureEvent } from '@sentry/hub'; + +const PUBLIC_DSN = 'https://username@domain/123'; +const sendEvent = jest.spyOn(TestClient.prototype, 'sendEvent'); + +describe('Hint', () => { + beforeEach(() => { + TestClient.sendEventCalled = undefined; + TestClient.instance = undefined; + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + + test('can be mutated in beforeSend', () => { + expect.assertions(1); + + const options = getDefaultTestClientOptions({ + dsn: PUBLIC_DSN, + beforeSend: (event, hint) => { + if (hint) { + hint.attachments = [...(hint?.attachments || []), { filename: 'another.file', data: 'more text' }]; + } + + return event; + }, + }); + + const client = new TestClient(options); + client.captureEvent({}); + + const [, hint] = sendEvent.mock.calls[0]; + + expect(hint).toEqual({ + attachments: [{ filename: 'another.file', data: 'more text' }], + }); + }); + + test('gets passed through to beforeSend and can be further mutated', () => { + expect.assertions(1); + + const options = getDefaultTestClientOptions({ + dsn: PUBLIC_DSN, + beforeSend: (event, hint) => { + if (hint) { + hint.attachments = [...(hint?.attachments || []), { filename: 'another.file', data: 'more text' }]; + } + + return event; + }, + }); + + const client = new TestClient(options); + client.captureEvent({}, { attachments: [{ filename: 'some-file.txt', data: 'Hello' }] }); + + const [, hint] = sendEvent.mock.calls[0]; + + expect(hint).toEqual({ + attachments: [ + { filename: 'some-file.txt', data: 'Hello' }, + { filename: 'another.file', data: 'more text' }, + ], + }); + }); + + test('can be mutated by an integration via event processor', () => { + expect.assertions(1); + + const options = getDefaultTestClientOptions({ + dsn: PUBLIC_DSN, + integrations: [new AddAttachmentTestIntegration()], + }); + + initAndBind(TestClient, options); + + captureEvent({}); + + const [, hint] = sendEvent.mock.calls[0]; + + expect(hint?.attachments).toEqual([{ filename: 'integration.file', data: 'great content!' }]); + }); +}); diff --git a/packages/core/test/mocks/integration.ts b/packages/core/test/mocks/integration.ts index 2e192fb12227..bd9812c7ddeb 100644 --- a/packages/core/test/mocks/integration.ts +++ b/packages/core/test/mocks/integration.ts @@ -22,3 +22,19 @@ export class TestIntegration implements Integration { }); } } + +export class AddAttachmentTestIntegration implements Integration { + public static id: string = 'AddAttachmentTestIntegration'; + + public name: string = 'AddAttachmentTestIntegration'; + + public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void): void { + addGlobalEventProcessor((event, hint) => { + if (hint) { + hint.attachments = [...(hint?.attachments || []), { filename: 'integration.file', data: 'great content!' }]; + } + + return event; + }); + } +} From a05c202a90082b5e001afdc92873e9ffb9e355a3 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Thu, 19 May 2022 20:52:26 +0100 Subject: [PATCH 2/3] Improve tests --- packages/core/test/lib/hint.test.ts | 188 +++++++++++++++------------ packages/serverless/src/awslambda.ts | 2 +- packages/utils/src/dsn.ts | 1 - 3 files changed, 103 insertions(+), 88 deletions(-) diff --git a/packages/core/test/lib/hint.test.ts b/packages/core/test/lib/hint.test.ts index 7ef09655e372..88bf97c5a813 100644 --- a/packages/core/test/lib/hint.test.ts +++ b/packages/core/test/lib/hint.test.ts @@ -1,86 +1,102 @@ -import { getDefaultTestClientOptions, TestClient } from '../mocks/client'; -import { AddAttachmentTestIntegration } from '../mocks/integration'; -import { initAndBind } from '../../src/sdk'; -import { captureEvent } from '@sentry/hub'; - -const PUBLIC_DSN = 'https://username@domain/123'; -const sendEvent = jest.spyOn(TestClient.prototype, 'sendEvent'); - -describe('Hint', () => { - beforeEach(() => { - TestClient.sendEventCalled = undefined; - TestClient.instance = undefined; - }); - - afterEach(() => { - jest.clearAllMocks(); - }); - - test('can be mutated in beforeSend', () => { - expect.assertions(1); - - const options = getDefaultTestClientOptions({ - dsn: PUBLIC_DSN, - beforeSend: (event, hint) => { - if (hint) { - hint.attachments = [...(hint?.attachments || []), { filename: 'another.file', data: 'more text' }]; - } - - return event; - }, - }); - - const client = new TestClient(options); - client.captureEvent({}); - - const [, hint] = sendEvent.mock.calls[0]; - - expect(hint).toEqual({ - attachments: [{ filename: 'another.file', data: 'more text' }], - }); - }); - - test('gets passed through to beforeSend and can be further mutated', () => { - expect.assertions(1); - - const options = getDefaultTestClientOptions({ - dsn: PUBLIC_DSN, - beforeSend: (event, hint) => { - if (hint) { - hint.attachments = [...(hint?.attachments || []), { filename: 'another.file', data: 'more text' }]; - } - - return event; - }, - }); - - const client = new TestClient(options); - client.captureEvent({}, { attachments: [{ filename: 'some-file.txt', data: 'Hello' }] }); - - const [, hint] = sendEvent.mock.calls[0]; - - expect(hint).toEqual({ - attachments: [ - { filename: 'some-file.txt', data: 'Hello' }, - { filename: 'another.file', data: 'more text' }, - ], - }); - }); - - test('can be mutated by an integration via event processor', () => { - expect.assertions(1); - - const options = getDefaultTestClientOptions({ - dsn: PUBLIC_DSN, - integrations: [new AddAttachmentTestIntegration()], - }); - - initAndBind(TestClient, options); - - captureEvent({}); - - const [, hint] = sendEvent.mock.calls[0]; - - expect(hint?.attachments).toEqual([{ filename: 'integration.file', data: 'great content!' }]); - }); -}); +import { captureEvent, configureScope } from '@sentry/hub'; +import { getGlobalObject } from '@sentry/utils'; + +import { initAndBind } from '../../src/sdk'; +import { getDefaultTestClientOptions, TestClient } from '../mocks/client'; +import { AddAttachmentTestIntegration } from '../mocks/integration'; + +const PUBLIC_DSN = 'https://username@domain/123'; +const sendEvent = jest.spyOn(TestClient.prototype, 'sendEvent'); + +describe('Hint', () => { + beforeEach(() => { + TestClient.sendEventCalled = undefined; + TestClient.instance = undefined; + }); + + afterEach(() => { + jest.clearAllMocks(); + delete getGlobalObject().__SENTRY__; + }); + + describe('attachments', () => { + test('can be mutated in beforeSend', () => { + expect.assertions(1); + + const options = getDefaultTestClientOptions({ + dsn: PUBLIC_DSN, + beforeSend: (event, hint) => { + if (hint) { + hint.attachments = [...(hint?.attachments || []), { filename: 'another.file', data: 'more text' }]; + } + + return event; + }, + }); + + const client = new TestClient(options); + client.captureEvent({}); + + const [, hint] = sendEvent.mock.calls[0]; + expect(hint).toEqual({ attachments: [{ filename: 'another.file', data: 'more text' }] }); + }); + + test('gets passed through to beforeSend and can be further mutated', () => { + expect.assertions(1); + + const options = getDefaultTestClientOptions({ + dsn: PUBLIC_DSN, + beforeSend: (event, hint) => { + if (hint) { + hint.attachments = [...(hint?.attachments || []), { filename: 'another.file', data: 'more text' }]; + } + + return event; + }, + }); + + const client = new TestClient(options); + client.captureEvent({}, { attachments: [{ filename: 'some-file.txt', data: 'Hello' }] }); + + const [, hint] = sendEvent.mock.calls[0]; + expect(hint).toEqual({ + attachments: [ + { filename: 'some-file.txt', data: 'Hello' }, + { filename: 'another.file', data: 'more text' }, + ], + }); + }); + + test('can be mutated by an integration via event processor', () => { + expect.assertions(1); + + const options = getDefaultTestClientOptions({ + dsn: PUBLIC_DSN, + integrations: [new AddAttachmentTestIntegration()], + }); + + initAndBind(TestClient, options); + captureEvent({}); + + const [, hint] = sendEvent.mock.calls[0]; + expect(hint?.attachments).toEqual([{ filename: 'integration.file', data: 'great content!' }]); + }); + + test('get copied from scope to hint', () => { + expect.assertions(1); + + const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN }); + initAndBind(TestClient, options); + + configureScope(scope => scope.addAttachment({ filename: 'scope.file', data: 'great content!' })); + + captureEvent({}, { attachments: [{ filename: 'some-file.txt', data: 'Hello' }] }); + + const [, hint] = sendEvent.mock.calls[0]; + expect(hint?.attachments).toEqual([ + { filename: 'some-file.txt', data: 'Hello' }, + { filename: 'scope.file', data: 'great content!' }, + ]); + }); + }); +}); diff --git a/packages/serverless/src/awslambda.ts b/packages/serverless/src/awslambda.ts index 88af7f66c3b5..5e00e3419153 100644 --- a/packages/serverless/src/awslambda.ts +++ b/packages/serverless/src/awslambda.ts @@ -79,7 +79,7 @@ export function init(options: Sentry.NodeOptions = {}): void { version: Sentry.SDK_VERSION, }; - options.dsn = extensionRelayDSN(options.dsn) + options.dsn = extensionRelayDSN(options.dsn); Sentry.init(options); Sentry.addGlobalEventProcessor(serverlessEventProcessor); diff --git a/packages/utils/src/dsn.ts b/packages/utils/src/dsn.ts index b99c983aad71..2c43c2934fb4 100644 --- a/packages/utils/src/dsn.ts +++ b/packages/utils/src/dsn.ts @@ -108,7 +108,6 @@ export function makeDsn(from: DsnLike): DsnComponents { return components; } - /** * Changes a Dsn to point to the `relay` server running in the Lambda Extension. * From c9289a0bdc9417b4ea286937427a9e838b7b831b Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Wed, 25 May 2022 11:56:35 +0100 Subject: [PATCH 3/3] Minor cleanup after improved types --- packages/core/test/lib/hint.test.ts | 10 ++-------- packages/core/test/mocks/integration.ts | 5 +---- 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/packages/core/test/lib/hint.test.ts b/packages/core/test/lib/hint.test.ts index 88bf97c5a813..03b755a63c71 100644 --- a/packages/core/test/lib/hint.test.ts +++ b/packages/core/test/lib/hint.test.ts @@ -26,10 +26,7 @@ describe('Hint', () => { const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN, beforeSend: (event, hint) => { - if (hint) { - hint.attachments = [...(hint?.attachments || []), { filename: 'another.file', data: 'more text' }]; - } - + hint.attachments = [...(hint.attachments || []), { filename: 'another.file', data: 'more text' }]; return event; }, }); @@ -47,10 +44,7 @@ describe('Hint', () => { const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN, beforeSend: (event, hint) => { - if (hint) { - hint.attachments = [...(hint?.attachments || []), { filename: 'another.file', data: 'more text' }]; - } - + hint.attachments = [...(hint.attachments || []), { filename: 'another.file', data: 'more text' }]; return event; }, }); diff --git a/packages/core/test/mocks/integration.ts b/packages/core/test/mocks/integration.ts index bd9812c7ddeb..868446665949 100644 --- a/packages/core/test/mocks/integration.ts +++ b/packages/core/test/mocks/integration.ts @@ -30,10 +30,7 @@ export class AddAttachmentTestIntegration implements Integration { public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void): void { addGlobalEventProcessor((event, hint) => { - if (hint) { - hint.attachments = [...(hint?.attachments || []), { filename: 'integration.file', data: 'great content!' }]; - } - + hint.attachments = [...(hint.attachments || []), { filename: 'integration.file', data: 'great content!' }]; return event; }); }