Skip to content

Commit d1bee91

Browse files
SG60Lms24
authored andcommitted
test(sveltekit): fix sveltekit unit tests
Need to use imports from @sentry/core, otherwise the scope contexts are not matching.
1 parent ce417ab commit d1bee91

File tree

4 files changed

+12
-11
lines changed

4 files changed

+12
-11
lines changed

packages/sveltekit/test/server/handle.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ import {
99
} from '@sentry/core';
1010
import type { EventEnvelopeHeaders, Span } from '@sentry/core';
1111
import { NodeClient, setCurrentClient } from '@sentry/node';
12-
import * as SentryNode from '@sentry/node';
12+
import * as SentryCore from '@sentry/core';
1313
import type { Handle } from '@sveltejs/kit';
1414
import { redirect } from '@sveltejs/kit';
1515
import { vi } from 'vitest';
1616

17-
import { FETCH_PROXY_SCRIPT, addSentryCodeToPage, isFetchProxyRequired, sentryHandle } from '../../src/server/handle';
17+
import { FETCH_PROXY_SCRIPT, addSentryCodeToPage, isFetchProxyRequired } from '../../src/server-common/handle';
18+
import { sentryHandle } from '../../src/server-common/handle';
1819
import { getDefaultNodeClientOptions } from '../utils';
1920

20-
const mockCaptureException = vi.spyOn(SentryNode, 'captureException').mockImplementation(() => 'xx');
21+
const mockCaptureException = vi.spyOn(SentryCore, 'captureException').mockImplementation(() => 'xx');
2122

2223
function mockEvent(override: Record<string, unknown> = {}): Parameters<Handle>[0]['event'] {
2324
const event: Parameters<Handle>[0]['event'] = {
@@ -443,7 +444,7 @@ describe('addSentryCodeToPage', () => {
443444

444445
it('adds meta tags and the fetch proxy script if there is an active transaction', () => {
445446
const transformPageChunk = addSentryCodeToPage({ injectFetchProxyScript: true });
446-
SentryNode.startSpan({ name: 'test' }, () => {
447+
SentryCore.startSpan({ name: 'test' }, () => {
447448
const transformed = transformPageChunk({ html, done: true }) as string;
448449

449450
expect(transformed).toContain('<meta name="sentry-trace"');

packages/sveltekit/test/server/handleError.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { beforeEach, describe, expect, it, vi } from 'vitest';
22

3-
import * as SentryNode from '@sentry/node';
3+
import * as SentryCore from '@sentry/core';
44
import type { HandleServerError, RequestEvent } from '@sveltejs/kit';
55

66
import { handleErrorWithSentry } from '../../src/server-common/handleError';
77

8-
const mockCaptureException = vi.spyOn(SentryNode, 'captureException').mockImplementation(() => 'xx');
8+
const mockCaptureException = vi.spyOn(SentryCore, 'captureException').mockImplementation(() => 'xx');
99

1010
const captureExceptionEventHint = {
1111
mechanism: { handled: false, type: 'sveltekit' },

packages/sveltekit/test/server/load.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import {
77
} from '@sentry/core';
88
import type { Event } from '@sentry/core';
99
import { NodeClient, getCurrentScope, getIsolationScope, setCurrentClient } from '@sentry/node';
10-
import * as SentryNode from '@sentry/node';
10+
import * as SentryCore from '@sentry/core';
1111
import type { Load, ServerLoad } from '@sveltejs/kit';
1212
import { error, redirect } from '@sveltejs/kit';
1313

1414
import { wrapLoadWithSentry, wrapServerLoadWithSentry } from '../../src/server-common/load';
1515
import { getDefaultNodeClientOptions } from '../utils';
1616

17-
const mockCaptureException = vi.spyOn(SentryNode, 'captureException').mockImplementation(() => 'xx');
17+
const mockCaptureException = vi.spyOn(SentryCore, 'captureException').mockImplementation(() => 'xx');
1818

1919
const mockStartSpan = vi.fn();
2020

packages/sveltekit/test/server/serverRoute.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as SentryNode from '@sentry/node';
1+
import * as SentryCore from '@sentry/core';
22
import type { NumericRange } from '@sveltejs/kit';
33
import { type RequestEvent, error, redirect } from '@sveltejs/kit';
44
import { beforeEach, describe, expect, it, vi } from 'vitest';
@@ -26,7 +26,7 @@ describe('wrapServerRouteWithSentry', () => {
2626
});
2727

2828
describe('wraps a server route span around the original server route handler', () => {
29-
const startSpanSpy = vi.spyOn(SentryNode, 'startSpan');
29+
const startSpanSpy = vi.spyOn(SentryCore, 'startSpan');
3030

3131
it('assigns the route id as name if available', () => {
3232
const wrappedRouteHandler = wrapServerRouteWithSentry(originalRouteHandler);
@@ -71,7 +71,7 @@ describe('wrapServerRouteWithSentry', () => {
7171
});
7272
});
7373

74-
const captureExceptionSpy = vi.spyOn(SentryNode, 'captureException');
74+
const captureExceptionSpy = vi.spyOn(SentryCore, 'captureException');
7575
describe('captures server route errors', () => {
7676
it('captures and rethrows normal server route error', async () => {
7777
const error = new Error('Server Route Error');

0 commit comments

Comments
 (0)