Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- feat(sveltekit): Set `sendDefaultPii: true` by default ([#1055](https://github.com/getsentry/sentry-wizard/pull/1055))
- feat(cocoa): Add structured logs opt-in option ([#1051](https://github.com/getsentry/sentry-wizard/pull/1051))
- feat(electron): Show `sendDefaultPii: true` inside `Sentry.init` example ([#1062](https://github.com/getsentry/sentry-wizard/pull/1062))
- feat(flutter): Add structured logs opt-in #1050 ([#1050](https://github.com/getsentry/sentry-wizard/pull/1050))
Expand Down
15 changes: 13 additions & 2 deletions e2e-tests/tests/sveltekit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ describe('Sveltekit', () => {

// If you don't want to use Session Replay, just remove the line below:
integrations: [replayIntegration()],

// Enable sending user PII (Personally Identifiable Information)
// https://docs.sentry.io/platforms/javascript/guides/sveltekit/configuration/options/#sendDefaultPii
sendDefaultPii: true,
});`,
'export const handleError = handleErrorWithSentry(',
]);
Expand All @@ -228,6 +232,11 @@ describe('Sveltekit', () => {
// Enable logs to be sent to Sentry
enableLogs: true,


// Enable sending user PII (Personally Identifiable Information)
// https://docs.sentry.io/platforms/javascript/guides/sveltekit/configuration/options/#sendDefaultPii
sendDefaultPii: true,

// uncomment the line below to enable Spotlight (https://spotlightjs.com)
// spotlight: import.meta.env.DEV,
});`,
Expand Down Expand Up @@ -279,7 +288,8 @@ describe('Sveltekit', () => {
replaysSessionSampleRate: 0.1,
replaysOnErrorSampleRate: 1,
integrations: [Sentry.replayIntegration()],
enableLogs: true
enableLogs: true,
sendDefaultPii: true
})`,
'export const handleError = Sentry.handleErrorWithSentry(',
]);
Expand All @@ -291,7 +301,8 @@ describe('Sveltekit', () => {
`Sentry.init({
dsn: "${TEST_ARGS.PROJECT_DSN}",
tracesSampleRate: 1,
enableLogs: true
enableLogs: true,
sendDefaultPii: true
})`,
'export const handleError = Sentry.handleErrorWithSentry();',
]);
Expand Down
8 changes: 7 additions & 1 deletion src/sveltekit/sdk-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ Skipping adding Sentry functionality to.`,
Sentry.setTag(`modified-${hookType}-hooks`, 'success');
}

function insertClientInitCall(
export function insertClientInitCall(
dsn: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
originalHooksMod: ProxifiedModule<any>,
Expand All @@ -299,6 +299,7 @@ function insertClientInitCall(
replaysOnErrorSampleRate?: number;
integrations?: string[];
enableLogs?: boolean;
sendDefaultPii?: boolean;
} = {
dsn,
};
Expand All @@ -317,6 +318,8 @@ function insertClientInitCall(
initArgs.enableLogs = true;
}

initArgs.sendDefaultPii = true;

// This assignment of any values is fine because we're just creating a function call in magicast
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const initCall = builders.functionCall('Sentry.init', initArgs);
Expand Down Expand Up @@ -353,6 +356,7 @@ function insertServerInitCall(
dsn: string;
tracesSampleRate?: number;
enableLogs?: boolean;
sendDefaultPii?: boolean;
} = {
dsn,
};
Expand All @@ -365,6 +369,8 @@ function insertServerInitCall(
initArgs.enableLogs = true;
}

initArgs.sendDefaultPii = true;

// This assignment of any values is fine because we're just creating a function call in magicast
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const initCall = builders.functionCall('Sentry.init', initArgs);
Expand Down
9 changes: 9 additions & 0 deletions src/sveltekit/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ ${
integrations: [replayIntegration()],`
: ''
}

// Enable sending user PII (Personally Identifiable Information)
// https://docs.sentry.io/platforms/javascript/guides/sveltekit/configuration/options/#sendDefaultPii
sendDefaultPii: true,
});

// If you have a custom error handler, pass it to \`handleErrorWithSentry\`
Expand Down Expand Up @@ -74,6 +78,11 @@ ${
`
: ''
}

// Enable sending user PII (Personally Identifiable Information)
// https://docs.sentry.io/platforms/javascript/guides/sveltekit/configuration/options/#sendDefaultPii
sendDefaultPii: true,

// uncomment the line below to enable Spotlight (https://spotlightjs.com)
// spotlight: import.meta.env.DEV,
});
Expand Down
208 changes: 208 additions & 0 deletions test/sveltekit/templates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import {
getClientHooksTemplate,
getServerHooksTemplate,
} from '../../src/sveltekit/templates';
import { insertClientInitCall } from '../../src/sveltekit/sdk-setup';
// @ts-expect-error - magicast is ESM and TS complains about that. It works though
import { parseModule } from 'magicast';

describe('getClientHooksTemplate', () => {
it('should generate client hooks template with all features enabled', () => {
Expand Down Expand Up @@ -34,6 +37,10 @@ describe('getClientHooksTemplate', () => {

// If you don't want to use Session Replay, just remove the line below:
integrations: [replayIntegration()],

// Enable sending user PII (Personally Identifiable Information)
// https://docs.sentry.io/platforms/javascript/guides/sveltekit/configuration/options/#sendDefaultPii
sendDefaultPii: true,
});

// If you have a custom error handler, pass it to \`handleErrorWithSentry\`
Expand Down Expand Up @@ -67,6 +74,10 @@ describe('getClientHooksTemplate', () => {

// If you don't want to use Session Replay, just remove the line below:
integrations: [replayIntegration()],

// Enable sending user PII (Personally Identifiable Information)
// https://docs.sentry.io/platforms/javascript/guides/sveltekit/configuration/options/#sendDefaultPii
sendDefaultPii: true,
});

// If you have a custom error handler, pass it to \`handleErrorWithSentry\`
Expand All @@ -93,6 +104,10 @@ describe('getClientHooksTemplate', () => {




// Enable sending user PII (Personally Identifiable Information)
// https://docs.sentry.io/platforms/javascript/guides/sveltekit/configuration/options/#sendDefaultPii
sendDefaultPii: true,
});

// If you have a custom error handler, pass it to \`handleErrorWithSentry\`
Expand All @@ -119,6 +134,10 @@ describe('getClientHooksTemplate', () => {
enableLogs: true,



// Enable sending user PII (Personally Identifiable Information)
// https://docs.sentry.io/platforms/javascript/guides/sveltekit/configuration/options/#sendDefaultPii
sendDefaultPii: true,
});

// If you have a custom error handler, pass it to \`handleErrorWithSentry\`
Expand Down Expand Up @@ -149,6 +168,11 @@ describe('getServerHooksTemplate', () => {
// Enable logs to be sent to Sentry
enableLogs: true,


// Enable sending user PII (Personally Identifiable Information)
// https://docs.sentry.io/platforms/javascript/guides/sveltekit/configuration/options/#sendDefaultPii
sendDefaultPii: true,

// uncomment the line below to enable Spotlight (https://spotlightjs.com)
// spotlight: import.meta.env.DEV,
});
Expand Down Expand Up @@ -178,6 +202,11 @@ describe('getServerHooksTemplate', () => {
dsn: 'https://sentry.io/123',



// Enable sending user PII (Personally Identifiable Information)
// https://docs.sentry.io/platforms/javascript/guides/sveltekit/configuration/options/#sendDefaultPii
sendDefaultPii: true,

// uncomment the line below to enable Spotlight (https://spotlightjs.com)
// spotlight: import.meta.env.DEV,
});
Expand Down Expand Up @@ -209,6 +238,11 @@ describe('getServerHooksTemplate', () => {
// Enable logs to be sent to Sentry
enableLogs: true,


// Enable sending user PII (Personally Identifiable Information)
// https://docs.sentry.io/platforms/javascript/guides/sveltekit/configuration/options/#sendDefaultPii
sendDefaultPii: true,

// uncomment the line below to enable Spotlight (https://spotlightjs.com)
// spotlight: import.meta.env.DEV,
});
Expand All @@ -222,3 +256,177 @@ describe('getServerHooksTemplate', () => {
`);
});
});

describe('insertClientInitCall', () => {
it('should insert client init call with all features enabled', () => {
const originalHooksMod = parseModule(`
import { handleErrorWithSentry } from "@sentry/sveltekit";
import * as Sentry from "@sentry/sveltekit";

export const handleError = handleErrorWithSentry();
`);

insertClientInitCall('https://sentry.io/123', originalHooksMod, {
performance: true,
replay: true,
logs: true,
});

const result = originalHooksMod.generate().code;

expect(result).toContain('import * as Sentry from "@sentry/sveltekit";');
expect(result).toContain('dsn: "https://sentry.io/123"');
expect(result).toContain('tracesSampleRate: 1');
expect(result).toContain('replaysSessionSampleRate: 0.1');
expect(result).toContain('replaysOnErrorSampleRate: 1');
expect(result).toContain('enableLogs: true');
expect(result).toContain('sendDefaultPii: true');
expect(result).toContain('integrations: [Sentry.replayIntegration()]');
expect(result).toContain('Sentry.init({');
});

it('should insert client init call with performance disabled', () => {
const originalHooksMod = parseModule(`
import { handleErrorWithSentry } from "@sentry/sveltekit";
import * as Sentry from "@sentry/sveltekit";

export const handleError = handleErrorWithSentry();
`);

insertClientInitCall('https://sentry.io/456', originalHooksMod, {
performance: false,
replay: true,
logs: false,
});

const result = originalHooksMod.generate().code;

expect(result).toContain('dsn: "https://sentry.io/456"');
expect(result).not.toContain('tracesSampleRate');
expect(result).toContain('replaysSessionSampleRate: 0.1');
expect(result).toContain('replaysOnErrorSampleRate: 1');
expect(result).not.toContain('enableLogs: true');
expect(result).toContain('sendDefaultPii: true');
expect(result).toContain('integrations: [Sentry.replayIntegration()]');
});

it('should insert client init call with replay disabled', () => {
const originalHooksMod = parseModule(`
import { handleErrorWithSentry } from "@sentry/sveltekit";
import * as Sentry from "@sentry/sveltekit";

export const handleError = handleErrorWithSentry();
`);

insertClientInitCall('https://sentry.io/789', originalHooksMod, {
performance: true,
replay: false,
logs: true,
});

const result = originalHooksMod.generate().code;

expect(result).toContain('dsn: "https://sentry.io/789"');
expect(result).toContain('tracesSampleRate: 1');
expect(result).not.toContain('replaysSessionSampleRate: 0.1');
expect(result).not.toContain('replaysOnErrorSampleRate: 1');
expect(result).not.toContain('integrations: [Sentry.replayIntegration()]');
expect(result).toContain('enableLogs: true');
expect(result).toContain('sendDefaultPii: true');
// Note: The comment mentions replaysSessionSampleRate even when replay is disabled
// This is current behavior of the function
expect(result).toContain('replaysSessionSampleRate');
});

it('should insert client init call with only logs enabled', () => {
const originalHooksMod = parseModule(`
import { handleErrorWithSentry } from "@sentry/sveltekit";
import * as Sentry from "@sentry/sveltekit";

export const handleError = handleErrorWithSentry();
`);

insertClientInitCall('https://sentry.io/xyz', originalHooksMod, {
performance: false,
replay: false,
logs: true,
});

const result = originalHooksMod.generate().code;

expect(result).toContain('dsn: "https://sentry.io/xyz"');
expect(result).not.toContain('tracesSampleRate: 1');
expect(result).not.toContain('replaysSessionSampleRate: 0.1');
expect(result).not.toContain('replaysOnErrorSampleRate: 1');
expect(result).not.toContain('integrations: [Sentry.replayIntegration()]');
expect(result).toContain('enableLogs: true');
expect(result).toContain('sendDefaultPii: true');
// Note: The comment mentions replaysSessionSampleRate even when replay is disabled
expect(result).toContain('replaysSessionSampleRate');
});

it('should insert client init call with all features disabled', () => {
const originalHooksMod = parseModule(`
import { handleErrorWithSentry } from "@sentry/sveltekit";
import * as Sentry from "@sentry/sveltekit";

export const handleError = handleErrorWithSentry();
`);

insertClientInitCall('https://sentry.io/minimal', originalHooksMod, {
performance: false,
replay: false,
logs: false,
});

const result = originalHooksMod.generate().code;

expect(result).toContain('dsn: "https://sentry.io/minimal"');
expect(result).not.toContain('tracesSampleRate: 1');
expect(result).not.toContain('replaysSessionSampleRate: 0.1');
expect(result).not.toContain('replaysOnErrorSampleRate: 1');
expect(result).not.toContain('integrations: [Sentry.replayIntegration()]');
expect(result).not.toContain('enableLogs: true');
expect(result).toContain('sendDefaultPii: true');
// Note: The comment mentions replaysSessionSampleRate even when replay is disabled
expect(result).toContain('replaysSessionSampleRate');
});

it('should insert init call after imports', () => {
const originalHooksMod = parseModule(`
import { handleErrorWithSentry } from "@sentry/sveltekit";
import { somethingElse } from "some-package";
import * as Sentry from "@sentry/sveltekit";

export const handleError = handleErrorWithSentry();
export const someOtherExport = somethingElse();
`);

insertClientInitCall('https://sentry.io/order-test', originalHooksMod, {
performance: true,
replay: false,
logs: false,
});

const result = originalHooksMod.generate().code;
const lines = result
.split('\n')
.map((line) => line.trim())
.filter((line) => line);

// Find the index of the last import and the Sentry.init call
const lastImportIndex = Math.max(
lines.findIndex((line) =>
line.includes('import { handleErrorWithSentry }'),
),
lines.findIndex((line) => line.includes('import { somethingElse }')),
lines.findIndex((line) => line.includes('import * as Sentry')),
);
const sentryInitIndex = lines.findIndex((line) =>
line.includes('Sentry.init({'),
);

expect(sentryInitIndex).toBeGreaterThan(lastImportIndex);
expect(result).toContain('Sentry.init({');
});
});
Loading