UNPKG

@sentry/wizard

Version:

Sentry wizard helping you to configure your project

168 lines 10.4 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 vitest_1 = require("vitest"); const fs = __importStar(require("fs")); const path = __importStar(require("path")); const client_entry_1 = require("../../../src/react-router/codemods/client.entry"); vitest_1.vi.mock('@clack/prompts', () => { const mock = { log: { warn: vitest_1.vi.fn(), info: vitest_1.vi.fn(), success: vitest_1.vi.fn(), }, }; return { default: mock, ...mock, }; }); (0, vitest_1.describe)('instrumentClientEntry', () => { const fixturesDir = path.join(__dirname, 'fixtures', 'client-entry'); let tmpDir; let tmpFile; (0, vitest_1.beforeEach)(() => { vitest_1.vi.clearAllMocks(); // Create unique tmp directory for each test tmpDir = path.join(fixturesDir, 'tmp', `test-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`); tmpFile = path.join(tmpDir, 'entry.client.tsx'); // Ensure tmp directory exists fs.mkdirSync(tmpDir, { recursive: true }); }); (0, vitest_1.afterEach)(() => { // Clean up tmp directory if (fs.existsSync(tmpDir)) { fs.rmSync(tmpDir, { recursive: true }); } }); (0, vitest_1.it)('should add Sentry import and initialization with all features enabled', async () => { const basicContent = fs.readFileSync(path.join(fixturesDir, 'basic.tsx'), 'utf8'); fs.writeFileSync(tmpFile, basicContent); await (0, client_entry_1.instrumentClientEntry)(tmpFile, 'test-dsn', true, true, true); const modifiedContent = fs.readFileSync(tmpFile, 'utf8'); (0, vitest_1.expect)(modifiedContent).toContain('import * as Sentry from "@sentry/react-router";'); (0, vitest_1.expect)(modifiedContent).toContain('Sentry.init({'); (0, vitest_1.expect)(modifiedContent).toContain('dsn: "test-dsn"'); (0, vitest_1.expect)(modifiedContent).toContain('integrations: ['); (0, vitest_1.expect)(modifiedContent).toContain('Sentry.reactRouterTracingIntegration()'); (0, vitest_1.expect)(modifiedContent).toContain('Sentry.replayIntegration('); (0, vitest_1.expect)(modifiedContent).toContain('enableLogs: true'); }); (0, vitest_1.it)('should add Sentry initialization with only tracing enabled', async () => { const basicContent = fs.readFileSync(path.join(fixturesDir, 'basic.tsx'), 'utf8'); fs.writeFileSync(tmpFile, basicContent); await (0, client_entry_1.instrumentClientEntry)(tmpFile, 'test-dsn', true, false, false); const modifiedContent = fs.readFileSync(tmpFile, 'utf8'); (0, vitest_1.expect)(modifiedContent).toContain('import * as Sentry from "@sentry/react-router";'); (0, vitest_1.expect)(modifiedContent).toContain('Sentry.init({'); (0, vitest_1.expect)(modifiedContent).toContain('dsn: "test-dsn"'); (0, vitest_1.expect)(modifiedContent).toContain('integrations: ['); (0, vitest_1.expect)(modifiedContent).toContain('Sentry.reactRouterTracingIntegration()'); (0, vitest_1.expect)(modifiedContent).not.toContain('Sentry.replayIntegration()'); (0, vitest_1.expect)(modifiedContent).not.toContain('enableLogs: true'); }); (0, vitest_1.it)('should add Sentry initialization with only replay enabled', async () => { const basicContent = fs.readFileSync(path.join(fixturesDir, 'basic.tsx'), 'utf8'); fs.writeFileSync(tmpFile, basicContent); await (0, client_entry_1.instrumentClientEntry)(tmpFile, 'test-dsn', false, true, false); const modifiedContent = fs.readFileSync(tmpFile, 'utf8'); (0, vitest_1.expect)(modifiedContent).toContain('import * as Sentry from "@sentry/react-router";'); (0, vitest_1.expect)(modifiedContent).toContain('Sentry.init({'); (0, vitest_1.expect)(modifiedContent).toContain('dsn: "test-dsn"'); (0, vitest_1.expect)(modifiedContent).toContain('integrations: ['); (0, vitest_1.expect)(modifiedContent).not.toContain('Sentry.reactRouterTracingIntegration()'); (0, vitest_1.expect)(modifiedContent).toContain('Sentry.replayIntegration('); (0, vitest_1.expect)(modifiedContent).not.toContain('enableLogs: true'); }); (0, vitest_1.it)('should add Sentry initialization with only logs enabled', async () => { const basicContent = fs.readFileSync(path.join(fixturesDir, 'basic.tsx'), 'utf8'); fs.writeFileSync(tmpFile, basicContent); await (0, client_entry_1.instrumentClientEntry)(tmpFile, 'test-dsn', false, false, true); const modifiedContent = fs.readFileSync(tmpFile, 'utf8'); (0, vitest_1.expect)(modifiedContent).toContain('import * as Sentry from "@sentry/react-router";'); (0, vitest_1.expect)(modifiedContent).toContain('Sentry.init({'); (0, vitest_1.expect)(modifiedContent).toContain('dsn: "test-dsn"'); (0, vitest_1.expect)(modifiedContent).toContain('integrations: ['); (0, vitest_1.expect)(modifiedContent).not.toContain('Sentry.reactRouterTracingIntegration()'); (0, vitest_1.expect)(modifiedContent).not.toContain('Sentry.replayIntegration()'); (0, vitest_1.expect)(modifiedContent).toContain('enableLogs: true'); }); (0, vitest_1.it)('should add minimal Sentry initialization when all features are disabled', async () => { const basicContent = fs.readFileSync(path.join(fixturesDir, 'basic.tsx'), 'utf8'); fs.writeFileSync(tmpFile, basicContent); await (0, client_entry_1.instrumentClientEntry)(tmpFile, 'test-dsn', false, false, false); const modifiedContent = fs.readFileSync(tmpFile, 'utf8'); (0, vitest_1.expect)(modifiedContent).toContain('import * as Sentry from "@sentry/react-router";'); (0, vitest_1.expect)(modifiedContent).toContain('Sentry.init({'); (0, vitest_1.expect)(modifiedContent).toContain('dsn: "test-dsn"'); (0, vitest_1.expect)(modifiedContent).toContain('integrations: []'); (0, vitest_1.expect)(modifiedContent).not.toContain('Sentry.reactRouterTracingIntegration()'); (0, vitest_1.expect)(modifiedContent).not.toContain('Sentry.replayIntegration()'); (0, vitest_1.expect)(modifiedContent).not.toContain('enableLogs: true'); }); (0, vitest_1.it)('should not modify file when Sentry content already exists', async () => { const withSentryContent = fs.readFileSync(path.join(fixturesDir, 'with-sentry.tsx'), 'utf8'); fs.writeFileSync(tmpFile, withSentryContent); await (0, client_entry_1.instrumentClientEntry)(tmpFile, 'test-dsn', true, true, true); const modifiedContent = fs.readFileSync(tmpFile, 'utf8'); // Content should remain unchanged (0, vitest_1.expect)(modifiedContent).toBe(withSentryContent); }); (0, vitest_1.it)('should insert Sentry initialization after imports', async () => { const withImportsContent = fs.readFileSync(path.join(fixturesDir, 'with-imports.tsx'), 'utf8'); fs.writeFileSync(tmpFile, withImportsContent); await (0, client_entry_1.instrumentClientEntry)(tmpFile, 'test-dsn', true, false, false); const modifiedContent = fs.readFileSync(tmpFile, 'utf8'); (0, vitest_1.expect)(modifiedContent).toContain('import * as Sentry from "@sentry/react-router";'); (0, vitest_1.expect)(modifiedContent).toContain('Sentry.init({'); // Check that the Sentry import is before the init call const sentryImportIndex = modifiedContent.indexOf('import * as Sentry from "@sentry/react-router";'); const sentryInitIndex = modifiedContent.indexOf('Sentry.init({'); (0, vitest_1.expect)(sentryImportIndex).toBeLessThan(sentryInitIndex); }); (0, vitest_1.it)('should handle files with no imports', async () => { const noImportsContent = fs.readFileSync(path.join(fixturesDir, 'no-imports.tsx'), 'utf8'); fs.writeFileSync(tmpFile, noImportsContent); await (0, client_entry_1.instrumentClientEntry)(tmpFile, 'test-dsn', false, true, false); const modifiedContent = fs.readFileSync(tmpFile, 'utf8'); (0, vitest_1.expect)(modifiedContent).toContain('import * as Sentry from "@sentry/react-router";'); (0, vitest_1.expect)(modifiedContent).toContain('Sentry.init({'); (0, vitest_1.expect)(modifiedContent).toContain('Sentry.replayIntegration('); }); (0, vitest_1.it)('should preserve existing code structure', async () => { const complexContent = fs.readFileSync(path.join(fixturesDir, 'complex.tsx'), 'utf8'); fs.writeFileSync(tmpFile, complexContent); await (0, client_entry_1.instrumentClientEntry)(tmpFile, 'test-dsn', true, true, false); const modifiedContent = fs.readFileSync(tmpFile, 'utf8'); (0, vitest_1.expect)(modifiedContent).toContain('import * as Sentry from "@sentry/react-router";'); (0, vitest_1.expect)(modifiedContent).toContain('Sentry.init({'); // Original content should still be there (0, vitest_1.expect)(modifiedContent).toContain('startTransition'); (0, vitest_1.expect)(modifiedContent).toContain('hydrateRoot'); (0, vitest_1.expect)(modifiedContent).toContain('<StrictMode>'); }); }); //# sourceMappingURL=client-entry.test.js.map