UNPKG

@sentry/wizard

Version:

Sentry wizard helping you to configure your project

216 lines (205 loc) 9.91 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"); 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! 💩', } } `; async function runWizardOnSvelteKitProject(projectDir, integration, fileModificationFn) { const wizardInstance = (0, utils_1.startWizardInstance)(integration, projectDir); let packageManagerPrompted = false; if (fileModificationFn) { fileModificationFn(projectDir, integration); // As we modified project, we have a warning prompt before we get the package manager prompt await wizardInstance.waitForOutput('Do you want to continue anyway?'); packageManagerPrompted = await wizardInstance.sendStdinAndWaitForOutput([utils_1.KEYS.ENTER], 'Please select your package manager.'); } else { packageManagerPrompted = await wizardInstance.waitForOutput('Please select your package manager'); } const tracingOptionPrompted = packageManagerPrompted && (await wizardInstance.sendStdinAndWaitForOutput( // Selecting `yarn` as the package manager [utils_1.KEYS.DOWN, utils_1.KEYS.ENTER], // "Do you want to enable Tracing", sometimes doesn't work as `Tracing` can be printed in bold. 'to track the performance of your application?', { timeout: 240000, })); const replayOptionPrompted = tracingOptionPrompted && (await wizardInstance.sendStdinAndWaitForOutput([utils_1.KEYS.ENTER], // "Do you want to enable Sentry Session Replay", sometimes doesn't work as `Sentry Session Replay` can be printed in bold. 'to get a video-like reproduction of errors during a user session?')); const logsOptionPrompted = replayOptionPrompted && (await wizardInstance.sendStdinAndWaitForOutput([utils_1.KEYS.ENTER], // "Do you want to enable Logs", sometimes doesn't work as `Logs` can be printed in bold. 'to send your application logs to Sentry?')); logsOptionPrompted && (await wizardInstance.sendStdinAndWaitForOutput([utils_1.KEYS.ENTER], 'Do you want to create an example page', { optional: true, })); await wizardInstance.sendStdinAndWaitForOutput([utils_1.KEYS.ENTER, utils_1.KEYS.ENTER], 'Successfully installed the Sentry SvelteKit SDK!'); wizardInstance.kill(); } 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)('example page exists', () => { (0, utils_1.checkFileExists)(path.resolve(projectDir, 'src/routes/sentry-example-page/+page.svelte')); (0, utils_1.checkFileExists)(path.resolve(projectDir, 'src/routes/sentry-example-page/+server.js')); }); (0, vitest_1.test)('vite.config contains sentry plugin', () => { (0, utils_1.checkFileContents)(path.resolve(projectDir, 'vite.config.ts'), `plugins: [sentrySvelteKit({ sourceMapsUploadOptions: { `); }); (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'); }); } (0, vitest_1.describe)('Sveltekit', () => { (0, vitest_1.describe)('without existing hooks', () => { const integration = Constants_1.Integration.sveltekit; const projectDir = path.resolve(__dirname, '../test-applications/sveltekit-test-app'); (0, vitest_1.beforeAll)(async () => { 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()], });`, '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, // uncomment the line below to enable Spotlight (https://spotlightjs.com) // spotlight: import.meta.env.DEV, });`, 'export const handleError = handleErrorWithSentry();', ]); }); }); (0, vitest_1.describe)('with existing hooks', () => { const integration = Constants_1.Integration.sveltekit; const projectDir = path.resolve(__dirname, '../test-applications/sveltekit-test-app'); (0, vitest_1.beforeAll)(async () => { 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 })`, '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 })`, 'export const handleError = Sentry.handleErrorWithSentry();', ]); }); }); }); //# sourceMappingURL=sveltekit.test.js.map