UNPKG

one

Version:

One is a new React Framework that makes Vite serve both native and web.

89 lines (88 loc) 2.8 kB
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; vi.mock("vite", async () => { const actual = await vi.importActual("vite"); return { ...actual, createServerModuleRunner: vi.fn(() => ({ clearCache: vi.fn(), import: vi.fn() })) }; }); describe("createFileSystemRouterPlugin", () => { const previousIsVxrnCli = process.env.IS_VXRN_CLI; let previousVxrnPluginConfig; let tempRoot; beforeEach(() => { previousVxrnPluginConfig = globalThis.__vxrnPluginConfig__; }); afterEach(() => { if (tempRoot) { rmSync(tempRoot, { recursive: true, force: true }); tempRoot = void 0; } if (previousIsVxrnCli === void 0) { delete process.env.IS_VXRN_CLI; } else { process.env.IS_VXRN_CLI = previousIsVxrnCli; } if (previousVxrnPluginConfig === void 0) { delete globalThis.__vxrnPluginConfig__; } else { ; globalThis.__vxrnPluginConfig__ = previousVxrnPluginConfig; } vi.restoreAllMocks(); }); it("keeps route watcher rebuild errors handled", async () => { process.env.IS_VXRN_CLI = "1"; tempRoot = mkdtempSync(path.join(tmpdir(), "one-router-watch-")); const appDir = path.join(tempRoot, "app"); writeFileSync(path.join(tempRoot, "package.json"), "{}\n"); mkdirSync(appDir); writeFileSync(path.join(appDir, "index.tsx"), "export default function Index() { return null }\n"); globalThis.__vxrnPluginConfig__ = { web: { defaultRenderMode: "ssg" } }; const { createFileSystemRouterPlugin } = await import("./fileSystemRouterPlugin.mjs"); const plugin = createFileSystemRouterPlugin({ router: { root: appDir } }); let watcherListener; const server = { environments: { ssr: {} }, watcher: { addListener: vi.fn((event, listener) => { if (event === "all") { watcherListener = listener; } }), on: vi.fn() } }; plugin.configureServer(server); expect(watcherListener).toBeDefined(); const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); delete globalThis.__vxrnPluginConfig__; if (!watcherListener) { throw new Error("Expected route watcher listener to be registered"); } await expect(Promise.resolve(watcherListener("add", path.join(appDir, "new-route.tsx")))).resolves.toBeUndefined(); expect(warn).toHaveBeenCalledWith(expect.stringContaining("[one] Failed to rebuild routes"), expect.any(Error)); }); }); //# sourceMappingURL=fileSystemRouterPlugin.test.mjs.map