Skip to content

Commit

Permalink
rewrite using trace
Browse files Browse the repository at this point in the history
  • Loading branch information
AbhiPrasad committed Mar 22, 2023
1 parent 41c68b9 commit 6e30ae3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/sveltekit/src/client/load.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { trace } from '@sentry/core';
import { captureException, getCurrentHub } from '@sentry/svelte';
import { addExceptionMechanism, isThenable, objectify } from '@sentry/utils';
import { captureException } from '@sentry/svelte';
import { addExceptionMechanism, objectify } from '@sentry/utils';
import type { Load } from '@sveltejs/kit';

function sendErrorToSentry(e: unknown): unknown {
Expand Down
57 changes: 51 additions & 6 deletions packages/sveltekit/test/client/load.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Scope } from '@sentry/svelte';
import type { ServerLoad } from '@sveltejs/kit';
import { addTracingExtensions, Scope } from '@sentry/svelte';
import type { Load } from '@sveltejs/kit';
import { vi } from 'vitest';

import { wrapLoadWithSentry } from '../../src/client/load';
Expand All @@ -19,6 +19,19 @@ vi.mock('@sentry/svelte', async () => {
};
});

const mockTrace = vi.fn();

vi.mock('@sentry/core', async () => {
const original = (await vi.importActual('@sentry/core')) as any;
return {
...original,
trace: (...args: unknown[]) => {
mockTrace(...args);
return original.trace(...args);
},
};
});

const mockAddExceptionMechanism = vi.fn();

vi.mock('@sentry/utils', async () => {
Expand All @@ -33,22 +46,54 @@ function getById(_id?: string) {
throw new Error('error');
}

const MOCK_LOAD_ARGS: any = {
params: { id: '123' },
route: {
id: '/users/[id]',
},
url: new URL('http://localhost:3000/users/123'),
request: {
headers: {
get: (key: string) => {
if (key === 'sentry-trace') {
return '1234567890abcdef1234567890abcdef-1234567890abcdef-1';
}

if (key === 'baggage') {
return (
'sentry-environment=production,sentry-release=1.0.0,sentry-transaction=dogpark,' +
'sentry-user_segment=segmentA,sentry-public_key=dogsarebadatkeepingsecrets,' +
'sentry-trace_id=1234567890abcdef1234567890abcdef,sentry-sample_rate=1'
);
}

return null;
},
},
},
};

beforeAll(() => {
addTracingExtensions();
});

describe('wrapLoadWithSentry', () => {
beforeEach(() => {
mockCaptureException.mockClear();
mockAddExceptionMechanism.mockClear();
mockTrace.mockClear();
mockScope = new Scope();
});

it('calls captureException', async () => {
async function load({ params }: Parameters<ServerLoad>[0]): Promise<ReturnType<ServerLoad>> {
async function load({ params }: Parameters<Load>[0]): Promise<ReturnType<Load>> {
return {
post: getById(params.id),
};
}

const wrappedLoad = wrapLoadWithSentry(load);
const res = wrappedLoad({ params: { id: '1' } } as any);
const res = wrappedLoad(MOCK_LOAD_ARGS);
await expect(res).rejects.toThrow();

expect(mockCaptureException).toHaveBeenCalledTimes(1);
Expand All @@ -60,14 +105,14 @@ describe('wrapLoadWithSentry', () => {
return mockScope;
});

async function load({ params }: Parameters<ServerLoad>[0]): Promise<ReturnType<ServerLoad>> {
async function load({ params }: Parameters<Load>[0]): Promise<ReturnType<Load>> {
return {
post: getById(params.id),
};
}

const wrappedLoad = wrapLoadWithSentry(load);
const res = wrappedLoad({ params: { id: '1' } } as any);
const res = wrappedLoad(MOCK_LOAD_ARGS);
await expect(res).rejects.toThrow();

expect(addEventProcessorSpy).toBeCalledTimes(1);
Expand Down

0 comments on commit 6e30ae3

Please sign in to comment.