UNPKG

@sentry/wizard

Version:

Sentry wizard helping you to configure your project

156 lines (152 loc) 8.07 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 routes_config_1 = require("../../src/react-router/codemods/routes-config"); 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)('addRoutesToConfig codemod', () => { let tmpDir; let appDir; let routesConfigPath; (0, vitest_1.beforeEach)(() => { vitest_1.vi.clearAllMocks(); // Create unique tmp directory for each test tmpDir = path.join(__dirname, 'tmp', `test-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`); appDir = path.join(tmpDir, 'app'); routesConfigPath = path.join(appDir, 'routes.ts'); fs.mkdirSync(appDir, { recursive: true }); }); (0, vitest_1.afterEach)(() => { // Clean up if (fs.existsSync(tmpDir)) { fs.rmSync(tmpDir, { recursive: true, force: true }); } }); (0, vitest_1.it)('should add routes to existing configuration', async () => { // Create a routes.ts file const routesContent = `import type { RouteConfig } from "@react-router/dev/routes"; import { index, route } from "@react-router/dev/routes"; export default [ index("routes/home.tsx"), route("/about", "routes/about.tsx"), ] satisfies RouteConfig;`; fs.writeFileSync(routesConfigPath, routesContent); await (0, routes_config_1.addRoutesToConfig)(routesConfigPath, true); // Check that both routes were added const updatedContent = fs.readFileSync(routesConfigPath, 'utf-8'); (0, vitest_1.expect)(updatedContent).toContain('route("/sentry-example-page", "routes/sentry-example-page.tsx")'); (0, vitest_1.expect)(updatedContent).toContain('route("/api/sentry-example-api", "routes/api.sentry-example-api.ts")'); }); (0, vitest_1.it)('should handle JavaScript projects correctly', async () => { // Create a routes.ts file const routesContent = `import type { RouteConfig } from "@react-router/dev/routes"; import { index, route } from "@react-router/dev/routes"; export default [ index("routes/home.jsx"), ] satisfies RouteConfig;`; fs.writeFileSync(routesConfigPath, routesContent); await (0, routes_config_1.addRoutesToConfig)(routesConfigPath, false); // JavaScript project // Check that both routes were added with .jsx/.js extensions const updatedContent = fs.readFileSync(routesConfigPath, 'utf-8'); (0, vitest_1.expect)(updatedContent).toContain('route("/sentry-example-page", "routes/sentry-example-page.jsx")'); (0, vitest_1.expect)(updatedContent).toContain('route("/api/sentry-example-api", "routes/api.sentry-example-api.js")'); }); (0, vitest_1.it)('should not duplicate routes if they already exist', async () => { // Create a routes.ts file with both routes already present const routesContent = `import type { RouteConfig } from "@react-router/dev/routes"; import { index, route } from "@react-router/dev/routes"; export default [ index("routes/home.tsx"), route("/sentry-example-page", "routes/sentry-example-page.tsx"), route("/api/sentry-example-api", "routes/api.sentry-example-api.ts"), ] satisfies RouteConfig;`; fs.writeFileSync(routesConfigPath, routesContent); await (0, routes_config_1.addRoutesToConfig)(routesConfigPath, true); // Check that the routes were not duplicated const updatedContent = fs.readFileSync(routesConfigPath, 'utf-8'); const pageRouteMatches = updatedContent.match(/route\("\/sentry-example-page"/g); const apiRouteMatches = updatedContent.match(/route\("\/api\/sentry-example-api"/g); (0, vitest_1.expect)(pageRouteMatches).toHaveLength(1); (0, vitest_1.expect)(apiRouteMatches).toHaveLength(1); }); (0, vitest_1.it)('should add route import when it does not exist', async () => { // Create a routes.ts file without route import const routesContent = `import type { RouteConfig } from "@react-router/dev/routes"; import { index } from "@react-router/dev/routes"; export default [ index("routes/home.tsx"), ] satisfies RouteConfig;`; fs.writeFileSync(routesConfigPath, routesContent); await (0, routes_config_1.addRoutesToConfig)(routesConfigPath, true); // Check that the route import was added const updatedContent = fs.readFileSync(routesConfigPath, 'utf-8'); (0, vitest_1.expect)(updatedContent).toContain('route'); (0, vitest_1.expect)(updatedContent).toContain('route("/sentry-example-page", "routes/sentry-example-page.tsx")'); }); (0, vitest_1.it)('should create default export when it does not exist', async () => { // Create a routes.ts file without default export const routesContent = `import type { RouteConfig } from "@react-router/dev/routes"; import { index, route } from "@react-router/dev/routes";`; fs.writeFileSync(routesConfigPath, routesContent); await (0, routes_config_1.addRoutesToConfig)(routesConfigPath, true); // Check that the default export was created const updatedContent = fs.readFileSync(routesConfigPath, 'utf-8'); (0, vitest_1.expect)(updatedContent).toContain('export default ['); (0, vitest_1.expect)(updatedContent).toContain('route("/sentry-example-page", "routes/sentry-example-page.tsx")'); (0, vitest_1.expect)(updatedContent).toContain('route("/api/sentry-example-api", "routes/api.sentry-example-api.ts")'); }); (0, vitest_1.it)('should handle empty file gracefully', async () => { // Create an empty routes.ts file fs.writeFileSync(routesConfigPath, ''); await (0, routes_config_1.addRoutesToConfig)(routesConfigPath, true); // Check that everything was added from scratch const updatedContent = fs.readFileSync(routesConfigPath, 'utf-8'); (0, vitest_1.expect)(updatedContent).toContain('import { route } from "@react-router/dev/routes";'); (0, vitest_1.expect)(updatedContent).toContain('export default ['); (0, vitest_1.expect)(updatedContent).toContain('route("/sentry-example-page", "routes/sentry-example-page.tsx")'); (0, vitest_1.expect)(updatedContent).toContain('route("/api/sentry-example-api", "routes/api.sentry-example-api.ts")'); }); (0, vitest_1.it)('should skip if file does not exist', async () => { // Don't create the file await (0, routes_config_1.addRoutesToConfig)(routesConfigPath, true); // Should not create the file if it doesn't exist (0, vitest_1.expect)(fs.existsSync(routesConfigPath)).toBe(false); }); }); //# sourceMappingURL=routes-config.test.js.map