|
| 1 | +import { readFileSync, writeFileSync } from "node:fs"; |
| 2 | +import { join } from "node:path"; |
| 3 | +import { describe, expect, test, vi } from "vitest"; |
| 4 | +import { isBuild, page, viteTestUrl } from "../../../__test-utils__"; |
| 5 | + |
| 6 | +describe( |
| 7 | + "react-spa (with experimental support)", |
| 8 | + { sequential: true, concurrent: false }, |
| 9 | + () => { |
| 10 | + test("returns the correct home page", async () => { |
| 11 | + const content = await page.textContent("h1"); |
| 12 | + expect(content).toBe("Vite + React"); |
| 13 | + }); |
| 14 | + |
| 15 | + test("allows updating state", async () => { |
| 16 | + const button = page.getByRole("button", { name: "increment" }); |
| 17 | + const contentBefore = await button.innerText(); |
| 18 | + expect(contentBefore).toBe("count is 0"); |
| 19 | + await button.click(); |
| 20 | + const contentAfter = await button.innerText(); |
| 21 | + expect(contentAfter).toBe("count is 1"); |
| 22 | + }); |
| 23 | + |
| 24 | + test("returns the home page for not found routes", async () => { |
| 25 | + await page.goto(`${viteTestUrl}/random-page`); |
| 26 | + const content = await page.textContent("h1"); |
| 27 | + expect(content).toBe("Vite + React"); |
| 28 | + }); |
| 29 | + |
| 30 | + // All these tests will fail without experimental support turned on |
| 31 | + describe("_headers", () => { |
| 32 | + test("applies _headers to HTML responses", async ({}) => { |
| 33 | + const response = await fetch(viteTestUrl); |
| 34 | + expect(response.headers.get("X-Header")).toBe("Custom-Value!!!"); |
| 35 | + }); |
| 36 | + |
| 37 | + // Since Vite will return static assets immediately without invoking the Worker at all |
| 38 | + // such requests will not have _headers applied. |
| 39 | + failsIf(!isBuild)("applies _headers to static assets", async ({}) => { |
| 40 | + const response = await fetch(`${viteTestUrl}/vite.svg`); |
| 41 | + expect(response.headers.get("X-Header")).toBe("Custom-Value!!!"); |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + // All these tests will fail without experimental support turned on |
| 46 | + describe("_redirects", () => { |
| 47 | + test("applies _redirects to HTML responses", async ({}) => { |
| 48 | + const response = await fetch(`${viteTestUrl}/foo`, { |
| 49 | + redirect: "manual", |
| 50 | + }); |
| 51 | + expect(response.status).toBe(302); |
| 52 | + expect(response.headers.get("Location")).toBe("/bar"); |
| 53 | + }); |
| 54 | + |
| 55 | + // Since Vite will return static assets immediately without invoking the Worker at all |
| 56 | + // such requests will not have _redirects applied. |
| 57 | + failsIf(!isBuild)("applies _redirects to static assets", async ({}) => { |
| 58 | + const response = await fetch(`${viteTestUrl}/redirect.svg`, { |
| 59 | + redirect: "manual", |
| 60 | + }); |
| 61 | + expect(response.status).toBe(302); |
| 62 | + expect(response.headers.get("Location")).toBe("/target.svg"); |
| 63 | + }); |
| 64 | + |
| 65 | + // Since Vite will return static assets immediately without invoking the Worker at all |
| 66 | + // such requests will not have _redirects applied. |
| 67 | + failsIf(!isBuild)( |
| 68 | + "supports proxying static assets to rewritten contents with _redirects", |
| 69 | + async ({}) => { |
| 70 | + const response = await fetch(`${viteTestUrl}/rewrite.svg`); |
| 71 | + expect(response.status).toBe(200); |
| 72 | + expect(await response.text()).toContain("target.svg"); |
| 73 | + } |
| 74 | + ); |
| 75 | + }); |
| 76 | + } |
| 77 | +); |
| 78 | + |
| 79 | +describe("reloading the server", () => { |
| 80 | + test.skipIf(isBuild)( |
| 81 | + "reloads config when the _headers or _redirects files change", |
| 82 | + async ({ onTestFinished }) => { |
| 83 | + const headersPath = join(__dirname, "../../public/_headers"); |
| 84 | + const originalHeaders = readFileSync(headersPath, "utf8"); |
| 85 | + const redirectsPath = join(__dirname, "../../public/_redirects"); |
| 86 | + const originalRedirects = readFileSync(redirectsPath, "utf8"); |
| 87 | + onTestFinished(async () => { |
| 88 | + writeFileSync(headersPath, originalHeaders); |
| 89 | + writeFileSync(redirectsPath, originalRedirects); |
| 90 | + }); |
| 91 | + |
| 92 | + const headersBefore = await fetch(viteTestUrl); |
| 93 | + expect(headersBefore.headers.get("X-Header")).toBe("Custom-Value!!!"); |
| 94 | + const redirectBefore = await fetch(`${viteTestUrl}/foo`, { |
| 95 | + redirect: "manual", |
| 96 | + }); |
| 97 | + expect(redirectBefore.status).toBe(302); |
| 98 | + |
| 99 | + // We make both these changes at the same time because there is something strange about the test setup |
| 100 | + // where fetches result in 500 errors, due to Miniflare stubs being reused after disposal. |
| 101 | + writeFileSync(headersPath, ""); |
| 102 | + writeFileSync(redirectsPath, ""); |
| 103 | + |
| 104 | + // Wait for Vite to reload |
| 105 | + await vi.waitFor(async () => { |
| 106 | + const headersAfter = await fetch(viteTestUrl); |
| 107 | + expect(headersAfter.headers.get("X-Header")).not.toBe( |
| 108 | + "Custom-Value!!!" |
| 109 | + ); |
| 110 | + const redirectAfter = await fetch(`${viteTestUrl}/foo`, { |
| 111 | + redirect: "manual", |
| 112 | + }); |
| 113 | + expect(redirectAfter.status).not.toBe(302); |
| 114 | + }); |
| 115 | + } |
| 116 | + ); |
| 117 | +}); |
| 118 | + |
| 119 | +function failsIf(condition: boolean) { |
| 120 | + return condition ? test.fails : test; |
| 121 | +} |
0 commit comments