|
| 1 | +import fs from "fs/promises"; |
| 2 | +import path from "path"; |
| 3 | +import { fileURLToPath } from "url"; |
| 4 | + |
| 5 | +import { createServer, ViteDevServer } from "vite"; |
| 6 | +import { afterAll, beforeAll, describe, expect, it } from "vitest"; |
| 7 | + |
| 8 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 9 | +const staticDir = path.resolve(__dirname, "..", "static"); |
| 10 | + |
| 11 | +describe("plugin integration", () => { |
| 12 | + let server: ViteDevServer; |
| 13 | + let baseUrl: string; |
| 14 | + |
| 15 | + beforeAll(async () => { |
| 16 | + const configFile = path.resolve(__dirname, "..", "vite.config.ts"); |
| 17 | + |
| 18 | + server = await createServer({ |
| 19 | + configFile, |
| 20 | + server: { |
| 21 | + port: 0, |
| 22 | + strictPort: false, |
| 23 | + }, |
| 24 | + }); |
| 25 | + |
| 26 | + await server.listen(); |
| 27 | + |
| 28 | + const address = server.httpServer?.address(); |
| 29 | + if (!address || typeof address === "string") throw new Error("Failed to start Vite dev server"); |
| 30 | + |
| 31 | + baseUrl = `http://localhost:${address.port}`; |
| 32 | + }); |
| 33 | + |
| 34 | + afterAll(async () => { |
| 35 | + await server?.close(); |
| 36 | + }); |
| 37 | + |
| 38 | + it("serves a configured static JSON file", async () => { |
| 39 | + const response = await fetch(`${baseUrl}/metadata.json`); |
| 40 | + |
| 41 | + const contentType = response.headers.get("content-type") ?? ""; |
| 42 | + |
| 43 | + expect(response.status).toBe(200); |
| 44 | + expect(contentType).toContain("application/json"); |
| 45 | + |
| 46 | + const expected = await fs.readFile(path.join(staticDir, "metadata.json"), "utf-8"); |
| 47 | + await expect(response.text()).resolves.toBe(expected); |
| 48 | + }); |
| 49 | + |
| 50 | + it("serves files matched by a wildcard pattern", async () => { |
| 51 | + const response = await fetch(`${baseUrl}/data/dog-treats`); |
| 52 | + |
| 53 | + const contentType = response.headers.get("content-type") ?? ""; |
| 54 | + |
| 55 | + expect(response.status).toBe(200); |
| 56 | + expect(contentType).toContain("text/csv"); |
| 57 | + |
| 58 | + const expected = await fs.readFile(path.join(staticDir, "data", "dog-treats.csv"), "utf-8"); |
| 59 | + await expect(response.text()).resolves.toBe(expected); |
| 60 | + }); |
| 61 | + |
| 62 | + it("serves files matched by a capture group pattern", async () => { |
| 63 | + const response = await fetch(`${baseUrl}/pages/index`); |
| 64 | + |
| 65 | + const contentType = response.headers.get("content-type") ?? ""; |
| 66 | + |
| 67 | + expect(response.status).toBe(200); |
| 68 | + expect(contentType).toContain("text/html"); |
| 69 | + |
| 70 | + const expected = await fs.readFile(path.join(staticDir, "pages", "index.html"), "utf-8"); |
| 71 | + await expect(response.text()).resolves.toBe(expected); |
| 72 | + }); |
| 73 | + |
| 74 | + it("returns 404 for missing files", async () => { |
| 75 | + const response = await fetch(`${baseUrl}/data/missing`); |
| 76 | + |
| 77 | + expect(response.status).toBe(404); |
| 78 | + await expect(response.text()).resolves.toBe("Not found"); |
| 79 | + }); |
| 80 | +}); |
0 commit comments