4
4
getInstrumentationServerTemplate ,
5
5
getServerHooksTemplate ,
6
6
} from '../../src/sveltekit/templates' ;
7
- import { insertClientInitCall } from '../../src/sveltekit/sdk-setup' ;
7
+ import { insertClientInitCall } from '../../src/sveltekit/sdk-setup/setup ' ;
8
8
// @ts -expect-error - magicast is ESM and TS complains about that. It works though
9
9
import { parseModule } from 'magicast' ;
10
10
@@ -398,15 +398,24 @@ describe('insertClientInitCall', () => {
398
398
399
399
const result = originalHooksMod . generate ( ) . code ;
400
400
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
+ ` ) ;
410
419
} ) ;
411
420
412
421
it ( 'should insert client init call with performance disabled' , ( ) => {
@@ -425,13 +434,22 @@ describe('insertClientInitCall', () => {
425
434
426
435
const result = originalHooksMod . generate ( ) . code ;
427
436
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
+ ` ) ;
435
453
} ) ;
436
454
437
455
it ( 'should insert client init call with replay disabled' , ( ) => {
@@ -450,16 +468,21 @@ describe('insertClientInitCall', () => {
450
468
451
469
const result = originalHooksMod . generate ( ) . code ;
452
470
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
+ ` ) ;
463
486
} ) ;
464
487
465
488
it ( 'should insert client init call with only logs enabled' , ( ) => {
@@ -478,15 +501,20 @@ describe('insertClientInitCall', () => {
478
501
479
502
const result = originalHooksMod . generate ( ) . code ;
480
503
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
+ ` ) ;
490
518
} ) ;
491
519
492
520
it ( 'should insert client init call with all features disabled' , ( ) => {
@@ -505,15 +533,19 @@ describe('insertClientInitCall', () => {
505
533
506
534
const result = originalHooksMod . generate ( ) . code ;
507
535
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
+ ` ) ;
517
549
} ) ;
518
550
519
551
it ( 'should insert init call after imports' , ( ) => {
@@ -533,24 +565,22 @@ describe('insertClientInitCall', () => {
533
565
} ) ;
534
566
535
567
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
- ) ;
552
568
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
+ ` ) ;
555
585
} ) ;
556
586
} ) ;
0 commit comments