Skip to content

Commit 46d4b0f

Browse files
committed
Use toMatchInlineSnapshot for assertions
1 parent 2968437 commit 46d4b0f

File tree

1 file changed

+93
-63
lines changed

1 file changed

+93
-63
lines changed

test/sveltekit/templates.test.ts

Lines changed: 93 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
getInstrumentationServerTemplate,
55
getServerHooksTemplate,
66
} from '../../src/sveltekit/templates';
7-
import { insertClientInitCall } from '../../src/sveltekit/sdk-setup';
7+
import { insertClientInitCall } from '../../src/sveltekit/sdk-setup/setup';
88
// @ts-expect-error - magicast is ESM and TS complains about that. It works though
99
import { parseModule } from 'magicast';
1010

@@ -398,15 +398,24 @@ describe('insertClientInitCall', () => {
398398

399399
const result = originalHooksMod.generate().code;
400400

401-
expect(result).toContain('import * as Sentry from "@sentry/sveltekit";');
402-
expect(result).toContain('dsn: "https://sentry.io/123"');
403-
expect(result).toContain('tracesSampleRate: 1');
404-
expect(result).toContain('replaysSessionSampleRate: 0.1');
405-
expect(result).toContain('replaysOnErrorSampleRate: 1');
406-
expect(result).toContain('enableLogs: true');
407-
expect(result).toContain('sendDefaultPii: true');
408-
expect(result).toContain('integrations: [Sentry.replayIntegration()]');
409-
expect(result).toContain('Sentry.init({');
401+
expect(result).toMatchInlineSnapshot(`
402+
"import { handleErrorWithSentry } from "@sentry/sveltekit";
403+
import * as Sentry from "@sentry/sveltekit";
404+
405+
// If you don't want to use Session Replay, remove the \`Replay\` integration,
406+
// \`replaysSessionSampleRate\` and \`replaysOnErrorSampleRate\` options.
407+
Sentry.init({
408+
dsn: "https://sentry.io/123",
409+
tracesSampleRate: 1,
410+
replaysSessionSampleRate: 0.1,
411+
replaysOnErrorSampleRate: 1,
412+
integrations: [Sentry.replayIntegration()],
413+
enableLogs: true,
414+
sendDefaultPii: true
415+
})
416+
417+
export const handleError = handleErrorWithSentry();"
418+
`);
410419
});
411420

412421
it('should insert client init call with performance disabled', () => {
@@ -425,13 +434,22 @@ describe('insertClientInitCall', () => {
425434

426435
const result = originalHooksMod.generate().code;
427436

428-
expect(result).toContain('dsn: "https://sentry.io/456"');
429-
expect(result).not.toContain('tracesSampleRate');
430-
expect(result).toContain('replaysSessionSampleRate: 0.1');
431-
expect(result).toContain('replaysOnErrorSampleRate: 1');
432-
expect(result).not.toContain('enableLogs: true');
433-
expect(result).toContain('sendDefaultPii: true');
434-
expect(result).toContain('integrations: [Sentry.replayIntegration()]');
437+
expect(result).toMatchInlineSnapshot(`
438+
"import { handleErrorWithSentry } from "@sentry/sveltekit";
439+
import * as Sentry from "@sentry/sveltekit";
440+
441+
// If you don't want to use Session Replay, remove the \`Replay\` integration,
442+
// \`replaysSessionSampleRate\` and \`replaysOnErrorSampleRate\` options.
443+
Sentry.init({
444+
dsn: "https://sentry.io/456",
445+
replaysSessionSampleRate: 0.1,
446+
replaysOnErrorSampleRate: 1,
447+
integrations: [Sentry.replayIntegration()],
448+
sendDefaultPii: true
449+
})
450+
451+
export const handleError = handleErrorWithSentry();"
452+
`);
435453
});
436454

437455
it('should insert client init call with replay disabled', () => {
@@ -450,16 +468,21 @@ describe('insertClientInitCall', () => {
450468

451469
const result = originalHooksMod.generate().code;
452470

453-
expect(result).toContain('dsn: "https://sentry.io/789"');
454-
expect(result).toContain('tracesSampleRate: 1');
455-
expect(result).not.toContain('replaysSessionSampleRate: 0.1');
456-
expect(result).not.toContain('replaysOnErrorSampleRate: 1');
457-
expect(result).not.toContain('integrations: [Sentry.replayIntegration()]');
458-
expect(result).toContain('enableLogs: true');
459-
expect(result).toContain('sendDefaultPii: true');
460-
// Note: The comment mentions replaysSessionSampleRate even when replay is disabled
461-
// This is current behavior of the function
462-
expect(result).toContain('replaysSessionSampleRate');
471+
expect(result).toMatchInlineSnapshot(`
472+
"import { handleErrorWithSentry } from "@sentry/sveltekit";
473+
import * as Sentry from "@sentry/sveltekit";
474+
475+
// If you don't want to use Session Replay, remove the \`Replay\` integration,
476+
// \`replaysSessionSampleRate\` and \`replaysOnErrorSampleRate\` options.
477+
Sentry.init({
478+
dsn: "https://sentry.io/789",
479+
tracesSampleRate: 1,
480+
enableLogs: true,
481+
sendDefaultPii: true
482+
})
483+
484+
export const handleError = handleErrorWithSentry();"
485+
`);
463486
});
464487

465488
it('should insert client init call with only logs enabled', () => {
@@ -478,15 +501,20 @@ describe('insertClientInitCall', () => {
478501

479502
const result = originalHooksMod.generate().code;
480503

481-
expect(result).toContain('dsn: "https://sentry.io/xyz"');
482-
expect(result).not.toContain('tracesSampleRate: 1');
483-
expect(result).not.toContain('replaysSessionSampleRate: 0.1');
484-
expect(result).not.toContain('replaysOnErrorSampleRate: 1');
485-
expect(result).not.toContain('integrations: [Sentry.replayIntegration()]');
486-
expect(result).toContain('enableLogs: true');
487-
expect(result).toContain('sendDefaultPii: true');
488-
// Note: The comment mentions replaysSessionSampleRate even when replay is disabled
489-
expect(result).toContain('replaysSessionSampleRate');
504+
expect(result).toMatchInlineSnapshot(`
505+
"import { handleErrorWithSentry } from "@sentry/sveltekit";
506+
import * as Sentry from "@sentry/sveltekit";
507+
508+
// If you don't want to use Session Replay, remove the \`Replay\` integration,
509+
// \`replaysSessionSampleRate\` and \`replaysOnErrorSampleRate\` options.
510+
Sentry.init({
511+
dsn: "https://sentry.io/xyz",
512+
enableLogs: true,
513+
sendDefaultPii: true
514+
})
515+
516+
export const handleError = handleErrorWithSentry();"
517+
`);
490518
});
491519

492520
it('should insert client init call with all features disabled', () => {
@@ -505,15 +533,19 @@ describe('insertClientInitCall', () => {
505533

506534
const result = originalHooksMod.generate().code;
507535

508-
expect(result).toContain('dsn: "https://sentry.io/minimal"');
509-
expect(result).not.toContain('tracesSampleRate: 1');
510-
expect(result).not.toContain('replaysSessionSampleRate: 0.1');
511-
expect(result).not.toContain('replaysOnErrorSampleRate: 1');
512-
expect(result).not.toContain('integrations: [Sentry.replayIntegration()]');
513-
expect(result).not.toContain('enableLogs: true');
514-
expect(result).toContain('sendDefaultPii: true');
515-
// Note: The comment mentions replaysSessionSampleRate even when replay is disabled
516-
expect(result).toContain('replaysSessionSampleRate');
536+
expect(result).toMatchInlineSnapshot(`
537+
"import { handleErrorWithSentry } from "@sentry/sveltekit";
538+
import * as Sentry from "@sentry/sveltekit";
539+
540+
// If you don't want to use Session Replay, remove the \`Replay\` integration,
541+
// \`replaysSessionSampleRate\` and \`replaysOnErrorSampleRate\` options.
542+
Sentry.init({
543+
dsn: "https://sentry.io/minimal",
544+
sendDefaultPii: true
545+
})
546+
547+
export const handleError = handleErrorWithSentry();"
548+
`);
517549
});
518550

519551
it('should insert init call after imports', () => {
@@ -533,24 +565,22 @@ describe('insertClientInitCall', () => {
533565
});
534566

535567
const result = originalHooksMod.generate().code;
536-
const lines = result
537-
.split('\n')
538-
.map((line) => line.trim())
539-
.filter((line) => line);
540-
541-
// Find the index of the last import and the Sentry.init call
542-
const lastImportIndex = Math.max(
543-
lines.findIndex((line) =>
544-
line.includes('import { handleErrorWithSentry }'),
545-
),
546-
lines.findIndex((line) => line.includes('import { somethingElse }')),
547-
lines.findIndex((line) => line.includes('import * as Sentry')),
548-
);
549-
const sentryInitIndex = lines.findIndex((line) =>
550-
line.includes('Sentry.init({'),
551-
);
552568

553-
expect(sentryInitIndex).toBeGreaterThan(lastImportIndex);
554-
expect(result).toContain('Sentry.init({');
569+
expect(result).toMatchInlineSnapshot(`
570+
"import { handleErrorWithSentry } from "@sentry/sveltekit";
571+
import { somethingElse } from "some-package";
572+
import * as Sentry from "@sentry/sveltekit";
573+
574+
// If you don't want to use Session Replay, remove the \`Replay\` integration,
575+
// \`replaysSessionSampleRate\` and \`replaysOnErrorSampleRate\` options.
576+
Sentry.init({
577+
dsn: "https://sentry.io/order-test",
578+
tracesSampleRate: 1,
579+
sendDefaultPii: true
580+
})
581+
582+
export const handleError = handleErrorWithSentry();
583+
export const someOtherExport = somethingElse();"
584+
`);
555585
});
556586
});

0 commit comments

Comments
 (0)