UNPKG

@sentry/wizard

Version:

Sentry wizard helping you to configure your project

158 lines (154 loc) 7.7 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 fs = __importStar(require("fs")); const vitest_1 = require("vitest"); const vite_1 = require("../../../src/react-router/codemods/vite"); vitest_1.vi.mock('@clack/prompts', () => ({ default: { log: { info: vitest_1.vi.fn(), warn: vitest_1.vi.fn(), success: vitest_1.vi.fn(), }, }, })); vitest_1.vi.mock('fs', async () => { const actual = await vitest_1.vi.importActual('fs'); return { ...actual, existsSync: vitest_1.vi.fn(), promises: { ...actual.promises, readFile: vitest_1.vi.fn(), writeFile: vitest_1.vi.fn(), }, }; }); (0, vitest_1.describe)('Vite Config Instrumentation', () => { const mockCwd = '/mock/project'; (0, vitest_1.beforeEach)(() => { vitest_1.vi.clearAllMocks(); vitest_1.vi.spyOn(process, 'cwd').mockReturnValue(mockCwd); }); (0, vitest_1.afterEach)(() => { vitest_1.vi.restoreAllMocks(); }); (0, vitest_1.describe)('instrumentViteConfig', () => { (0, vitest_1.it)('should throw error if vite config file does not exist', async () => { vitest_1.vi.mocked(fs.existsSync).mockReturnValue(false); await (0, vitest_1.expect)((0, vite_1.instrumentViteConfig)('my-org', 'my-project')).rejects.toThrow('Could not find vite.config.ts or vite.config.js'); }); (0, vitest_1.it)('should detect and skip if Sentry content already exists', async () => { const existingConfig = `import { defineConfig } from 'vite'; import { sentryReactRouter } from '@sentry/react-router'; export default defineConfig({ plugins: [sentryReactRouter({ org: "my-org", project: "my-project" }, config)] });`; vitest_1.vi.mocked(fs.existsSync).mockReturnValue(true); vitest_1.vi.mocked(fs.promises.readFile).mockResolvedValue(existingConfig); const result = await (0, vite_1.instrumentViteConfig)('my-org', 'my-project'); (0, vitest_1.expect)(result.wasConverted).toBe(false); }); (0, vitest_1.it)('should add sentryReactRouter plugin and convert to function form', async () => { const simpleConfig = `import { defineConfig } from 'vite'; export default defineConfig({ plugins: [] });`; const writtenFiles = {}; vitest_1.vi.mocked(fs.existsSync).mockReturnValue(true); vitest_1.vi.mocked(fs.promises.readFile).mockResolvedValue(simpleConfig); vitest_1.vi.mocked(fs.promises.writeFile).mockImplementation((filePath, content) => { writtenFiles[filePath] = content; return Promise.resolve(); }); const result = await (0, vite_1.instrumentViteConfig)('my-org', 'my-project'); (0, vitest_1.expect)(result.wasConverted).toBe(true); const writtenConfig = Object.values(writtenFiles)[0]; (0, vitest_1.expect)(writtenConfig).toContain('sentryReactRouter'); (0, vitest_1.expect)(writtenConfig).toContain('org: "my-org"'); (0, vitest_1.expect)(writtenConfig).toContain('project: "my-project"'); (0, vitest_1.expect)(writtenConfig).toContain('config =>'); }); (0, vitest_1.it)('should work with existing function form', async () => { const functionConfig = `import { defineConfig } from 'vite'; export default defineConfig(config => ({ plugins: [] }));`; const writtenFiles = {}; vitest_1.vi.mocked(fs.existsSync).mockReturnValue(true); vitest_1.vi.mocked(fs.promises.readFile).mockResolvedValue(functionConfig); vitest_1.vi.mocked(fs.promises.writeFile).mockImplementation((filePath, content) => { writtenFiles[filePath] = content; return Promise.resolve(); }); const result = await (0, vite_1.instrumentViteConfig)('my-org', 'my-project'); (0, vitest_1.expect)(result.wasConverted).toBe(false); const writtenConfig = Object.values(writtenFiles)[0]; (0, vitest_1.expect)(writtenConfig).toContain('sentryReactRouter'); (0, vitest_1.expect)(writtenConfig).toContain('org: "my-org"'); (0, vitest_1.expect)(writtenConfig).toContain('project: "my-project"'); }); (0, vitest_1.it)('should prefer vite.config.ts over vite.config.js', async () => { const configContent = `import { defineConfig } from 'vite'; export default defineConfig({ plugins: [] });`; const writtenFiles = {}; // First call checks for vite.config.ts (returns true), second call validates it exists vitest_1.vi.mocked(fs.existsSync).mockReturnValue(true); vitest_1.vi.mocked(fs.promises.readFile).mockResolvedValue(configContent); vitest_1.vi.mocked(fs.promises.writeFile).mockImplementation((filePath, content) => { writtenFiles[filePath] = content; return Promise.resolve(); }); await (0, vite_1.instrumentViteConfig)('my-org', 'my-project'); // Should write to vite.config.ts, not vite.config.js const writtenPath = Object.keys(writtenFiles)[0]; (0, vitest_1.expect)(writtenPath).toContain('vite.config.ts'); (0, vitest_1.expect)(writtenPath).not.toContain('vite.config.js'); }); (0, vitest_1.it)('should work with function expression form', async () => { const functionConfig = `import { defineConfig } from 'vite'; export default defineConfig(function(config) { return { plugins: [] }; });`; const writtenFiles = {}; vitest_1.vi.mocked(fs.existsSync).mockReturnValue(true); vitest_1.vi.mocked(fs.promises.readFile).mockResolvedValue(functionConfig); vitest_1.vi.mocked(fs.promises.writeFile).mockImplementation((filePath, content) => { writtenFiles[filePath] = content; return Promise.resolve(); }); const result = await (0, vite_1.instrumentViteConfig)('my-org', 'my-project'); (0, vitest_1.expect)(result.wasConverted).toBe(false); const writtenConfig = Object.values(writtenFiles)[0]; (0, vitest_1.expect)(writtenConfig).toContain('sentryReactRouter'); (0, vitest_1.expect)(writtenConfig).toContain('org: "my-org"'); (0, vitest_1.expect)(writtenConfig).toContain('project: "my-project"'); }); }); }); //# sourceMappingURL=vite.test.js.map