UNPKG

@sentry/wizard

Version:

Sentry wizard helping you to configure your project

229 lines (215 loc) 10.2 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const path = __importStar(require("node:path")); const Constants_1 = require("../../lib/Constants"); const utils_1 = require("../utils"); const vitest_1 = require("vitest"); //@ts-expect-error - clifty is ESM only const clifty_1 = require("clifty"); const SERVER_HOOK_TEMPLATE = `import type { Handle } from '@sveltejs/kit'; export const handle: Handle = async ({ event, resolve }) => { if (event.url.pathname.startsWith('/custom')) { return new Response('custom response'); } const response = await resolve(event); return response; }; `; const CLIENT_HOOK_TEMPLATE = ` export async function handleError({ error, event }) { // you can capture the \`error\` and \`event\` from the client // but it only runs if the unexpected error comes from \`+ page.ts\` console.log(error) return { // don't show sensitive data to the user message: 'Yikes! 💩', } } `; vitest_1.describe.sequential('Sveltekit', () => { (0, vitest_1.describe)('without existing hooks', () => { const integration = Constants_1.Integration.sveltekit; const projectDir = path.resolve(__dirname, '../test-applications/sveltekit-hooks-test-app'); (0, vitest_1.beforeAll)(async () => { (0, utils_1.initGit)(projectDir); (0, utils_1.revertLocalChanges)(projectDir); await runWizardOnSvelteKitProject(projectDir, integration); }); (0, vitest_1.afterAll)(() => { (0, utils_1.revertLocalChanges)(projectDir); (0, utils_1.cleanupGit)(projectDir); }); checkSvelteKitProject(projectDir, integration); (0, vitest_1.test)('hooks.client.ts contains sentry', () => { (0, utils_1.checkFileContents)(path.resolve(projectDir, 'src/hooks.client.ts'), [ `import * as Sentry from '@sentry/sveltekit';`, `Sentry.init({ dsn: '${utils_1.TEST_ARGS.PROJECT_DSN}', tracesSampleRate: 1.0, // Enable logs to be sent to Sentry enableLogs: true, // This sets the sample rate to be 10%. You may want this to be 100% while // in development and sample at a lower rate in production replaysSessionSampleRate: 0.1, // If the entire session is not sampled, use the below sample rate to sample // sessions when an error occurs. replaysOnErrorSampleRate: 1.0, // 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(', ]); }); (0, vitest_1.test)('hooks.server.ts contains sentry', () => { (0, utils_1.checkFileContents)(path.resolve(projectDir, 'src/hooks.server.ts'), [ `import * as Sentry from '@sentry/sveltekit';`, `Sentry.init({ dsn: '${utils_1.TEST_ARGS.PROJECT_DSN}', tracesSampleRate: 1.0, // 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, });`, 'export const handleError = handleErrorWithSentry();', ]); }); (0, vitest_1.test)('creates an example route and page', () => { (0, utils_1.checkFileExists)(path.resolve(projectDir, 'src/routes/sentry-example-page/+page.svelte')); (0, utils_1.checkFileContents)(path.resolve(projectDir, 'src/routes/sentry-example-page/+page.svelte'), // Svelte <5 specific syntax ['let hasSentError = false;', 'on:click={getSentryData}']); (0, utils_1.checkFileExists)(path.resolve(projectDir, 'src/routes/sentry-example-page/+server.js')); }); }); (0, vitest_1.describe)('with existing hooks', () => { const integration = Constants_1.Integration.sveltekit; const projectDir = path.resolve(__dirname, '../test-applications/sveltekit-hooks-test-app'); (0, vitest_1.beforeAll)(async () => { (0, utils_1.initGit)(projectDir); (0, utils_1.revertLocalChanges)(projectDir); await runWizardOnSvelteKitProject(projectDir, integration, (projectDir) => { (0, utils_1.createFile)(path.resolve(projectDir, 'src/hooks.server.ts'), SERVER_HOOK_TEMPLATE); (0, utils_1.createFile)(path.resolve(projectDir, 'src/hooks.client.ts'), CLIENT_HOOK_TEMPLATE); }); }); (0, vitest_1.afterAll)(() => { (0, utils_1.revertLocalChanges)(projectDir); (0, utils_1.cleanupGit)(projectDir); }); checkSvelteKitProject(projectDir, integration); // These are removed from the common tests as the content is different // when the hooks are merged instead of created from the template (0, vitest_1.test)('hooks.client.ts contains sentry instrumentation', () => { (0, utils_1.checkFileContents)(path.resolve(projectDir, 'src/hooks.client.ts'), [ `import * as Sentry from '@sentry/sveltekit';`, `Sentry.init({ dsn: "${utils_1.TEST_ARGS.PROJECT_DSN}", tracesSampleRate: 1, replaysSessionSampleRate: 0.1, replaysOnErrorSampleRate: 1, integrations: [Sentry.replayIntegration()], enableLogs: true, sendDefaultPii: true })`, 'export const handleError = Sentry.handleErrorWithSentry(', ]); }); (0, vitest_1.test)('hooks.server.ts contains sentry init', () => { (0, utils_1.checkFileContents)(path.resolve(projectDir, 'src/hooks.server.ts'), [ `import * as Sentry from '@sentry/sveltekit';`, `Sentry.init({ dsn: "${utils_1.TEST_ARGS.PROJECT_DSN}", tracesSampleRate: 1, enableLogs: true, sendDefaultPii: true })`, 'export const handleError = Sentry.handleErrorWithSentry();', ]); }); }); }); async function runWizardOnSvelteKitProject(projectDir, integration, fileModificationFn) { const wizardInteraction = (0, clifty_1.withEnv)({ cwd: projectDir, }).defineInteraction(); if (fileModificationFn) { fileModificationFn(projectDir, integration); wizardInteraction .whenAsked('Do you want to continue anyway?') .respondWith(clifty_1.KEYS.ENTER); } wizardInteraction .whenAsked("It seems you're using a SvelteKit version") .respondWith(clifty_1.KEYS.DOWN, clifty_1.KEYS.DOWN, clifty_1.KEYS.ENTER) .whenAsked('Please select your package manager.') .respondWith(clifty_1.KEYS.DOWN, clifty_1.KEYS.ENTER) .whenAsked('Do you want to enable Tracing', { timeout: 90000, // package installation can take a while in CI }) .respondWith(clifty_1.KEYS.ENTER) .whenAsked('Do you want to enable Session Replay') .respondWith(clifty_1.KEYS.ENTER) .whenAsked('Do you want to enable Logs') .respondWith(clifty_1.KEYS.ENTER) .whenAsked('Do you want to create an example page') .respondWith(clifty_1.KEYS.ENTER) .whenAsked('Optionally add a project-scoped MCP server configuration for the Sentry MCP?') .respondWith(clifty_1.KEYS.DOWN, clifty_1.KEYS.ENTER) .expectOutput('Successfully installed the Sentry SvelteKit SDK!'); await wizardInteraction.run((0, utils_1.getWizardCommand)(Constants_1.Integration.sveltekit)); } function checkSvelteKitProject(projectDir, integration, options) { (0, vitest_1.test)('should have the correct package.json', () => { (0, utils_1.checkPackageJson)(projectDir, integration); }); (0, vitest_1.test)('should have the correct .env.sentry-build-plugin', () => { (0, utils_1.checkEnvBuildPlugin)(projectDir); }); (0, vitest_1.test)('vite.config contains sentry plugin', () => { (0, utils_1.checkFileContents)(path.resolve(projectDir, 'vite.config.ts'), `plugins: [sentrySvelteKit({`); }); (0, vitest_1.test)('hook files created', () => { (0, utils_1.checkFileExists)(path.resolve(projectDir, 'src/hooks.server.ts')); (0, utils_1.checkFileExists)(path.resolve(projectDir, 'src/hooks.client.ts')); }); (0, vitest_1.test)('builds successfully', async () => { await (0, utils_1.checkIfBuilds)(projectDir); }); (0, vitest_1.test)('runs on dev mode correctly', async () => { await (0, utils_1.checkIfRunsOnDevMode)(projectDir, options?.devModeExpectedOutput || 'ready in'); }); (0, vitest_1.test)('runs on prod mode correctly', async () => { await (0, utils_1.checkIfRunsOnProdMode)(projectDir, options?.prodModeExpectedOutput || 'to expose', 'preview'); }); } //# sourceMappingURL=sveltekit-hooks.test.js.map