Skip to content

Commit f5d94c9

Browse files
committed
use async to detect sveltekit and add tests
1 parent 6407466 commit f5d94c9

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

packages/svelte/src/sdk.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { addGlobalEventProcessor, BrowserOptions, init as browserInit, SDK_VERSION } from '@sentry/browser';
2-
import { getDomElement } from '@sentry/utils';
2+
import { getDomElement, SyncPromise } from '@sentry/utils';
33
/**
44
* Inits the Svelte SDK
55
*/
@@ -18,15 +18,18 @@ export function init(options: BrowserOptions): void {
1818

1919
browserInit(options);
2020

21-
detectSvelteKit();
21+
void detectAndReportSvelteKit();
2222
}
2323

2424
/**
2525
* Detects if the SDK is initialized in a SvelteKit frontend, in which case
26-
* we add a global event processor to add it as a module to outgoing events.
26+
* we add a global event processor to add it as an event.module entry to
27+
* outgoing events.
2728
*/
28-
function detectSvelteKit(): void {
29-
setTimeout(() => {
29+
export function detectAndReportSvelteKit(): PromiseLike<void> {
30+
// We have to run this async because only after the SDK is initialized,
31+
// the HTML element is available
32+
return new SyncPromise(resolve => {
3033
if (isSvelteKitApp()) {
3134
addGlobalEventProcessor(event => {
3235
event.modules = {
@@ -36,7 +39,8 @@ function detectSvelteKit(): void {
3639
return event;
3740
});
3841
}
39-
}, 0);
42+
resolve();
43+
});
4044
}
4145

4246
/**

packages/svelte/test/sdk.test.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
import { init as browserInitRaw, SDK_VERSION } from '@sentry/browser';
1+
import { addGlobalEventProcessor, init as browserInitRaw, SDK_VERSION } from '@sentry/browser';
22

3-
import { init as svelteInit } from '../src/sdk';
3+
import { detectAndReportSvelteKit, init as svelteInit } from '../src/sdk';
44

55
const browserInit = browserInitRaw as jest.Mock;
6+
const addGlobalEventProcessorFunction = addGlobalEventProcessor as jest.Mock;
7+
68
jest.mock('@sentry/browser');
79

810
describe('Initialize Svelte SDk', () => {
9-
afterAll(() => {
11+
afterEach(() => {
1012
jest.clearAllMocks();
1113
});
1214

@@ -29,3 +31,25 @@ describe('Initialize Svelte SDk', () => {
2931
expect(browserInit).toHaveBeenCalledWith(expect.objectContaining(expectedMetadata));
3032
});
3133
});
34+
35+
describe('detectAndReportSvelteKit()', () => {
36+
afterEach(() => {
37+
jest.clearAllMocks();
38+
});
39+
40+
it('detects SvelteKit correctly and adds a global event processor', async () => {
41+
document.body.innerHTML += '<div id="svelte-announcer">Navigated to Home</div>';
42+
43+
await detectAndReportSvelteKit();
44+
45+
expect(addGlobalEventProcessorFunction).toHaveBeenCalledTimes(1);
46+
});
47+
48+
it("doesn't add global event processor, if SvelteKit was not detected", async () => {
49+
document.body.innerHTML = '';
50+
51+
await detectAndReportSvelteKit();
52+
53+
expect(addGlobalEventProcessorFunction).not.toHaveBeenCalled();
54+
});
55+
});

0 commit comments

Comments
 (0)