|
| 1 | +import { expect, test } from "@playwright/test"; |
| 2 | +import path from "node:path"; |
| 3 | +import { x } from "tinyexec"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Issue #587 — build-level coverage through the real rango Vite plugin. |
| 7 | + * |
| 8 | + * Uses a DEDICATED fixture app (e2e/prerender-onerror) with its own projectRoot, so |
| 9 | + * its dist/ and staged-asset dir are never shared with — or raced by — any other e2e |
| 10 | + * app's build. (The rango plugin's closeBundle writes the prerender/static manifests |
| 11 | + * straight into <projectRoot>/dist/rsc regardless of Vite's build.write, so a shared |
| 12 | + * app can't be isolated by write:false alone — only a separate projectRoot works.) |
| 13 | + * |
| 14 | + * The app exports an erroring Prerender route (PrerenderBoom, registered only when |
| 15 | + * RANGO_TEST_PRERENDER_ERROR is set; its render reads the unavailable ctx.env) and a |
| 16 | + * Static handler (StaticBoom, throws only when RANGO_TEST_STATIC_ERROR is set). |
| 17 | + * vite.config flips prerender.onError to "warn" when RANGO_TEST_PRERENDER_ONERROR=warn. |
| 18 | + * |
| 19 | + * Before the #587 fix the router error boundary swallowed the throw and the build |
| 20 | + * baked the error page as a healthy artifact (logged OK, served as a 200). These pin |
| 21 | + * the post-fix contract end-to-end through the compiled plugin: |
| 22 | + * - default: the build FAILS, naming the URL/handler and the original error; |
| 23 | + * - prerender.onError "warn": the build SUCCEEDS and the entry is skipped (not baked); |
| 24 | + * - both Prerender routes and Static() handlers go through the same policy. |
| 25 | + * |
| 26 | + * Build-only by design (explicit gap): #587 is a build-time bug and prerender.onError |
| 27 | + * is build config. The dev `/__rsc_prerender` endpoint tweak (router-discovery.ts) is |
| 28 | + * a cosmetic log refinement (stay quiet on Skip) on a path that serves ONLY non-Node |
| 29 | + * runtimes — and the repo's only non-Node dev fixture (cloudflare-basic) uses |
| 30 | + * buildEnv:"auto", where ctx.env is available at build, so the render-error repro |
| 31 | + * cannot occur there. The behavioral change (matchForPrerender re-throwing instead of |
| 32 | + * baking) is unit-covered (src/router/__tests__/prerender-render-error.test.tsx). |
| 33 | + * The endpoint's catch/404-fallthrough is therefore a deliberate untested gap. |
| 34 | + */ |
| 35 | +const cwd = path.resolve("./e2e/prerender-onerror"); |
| 36 | + |
| 37 | +function buildEnv(gates: Record<string, string>): Record<string, string> { |
| 38 | + return { ...process.env, ...gates } as Record<string, string>; |
| 39 | +} |
| 40 | + |
| 41 | +test.describe("prerender render error (production)", () => { |
| 42 | + test.describe.configure({ timeout: 180_000 }); |
| 43 | + |
| 44 | + test("fails the build by default, naming the URL and the original error", async () => { |
| 45 | + const res = await x("pnpm", ["build"], { |
| 46 | + nodeOptions: { cwd, env: buildEnv({ RANGO_TEST_PRERENDER_ERROR: "1" }) }, |
| 47 | + throwOnError: false, |
| 48 | + }); |
| 49 | + const out = `${res.stdout}\n${res.stderr}`; |
| 50 | + expect( |
| 51 | + res.exitCode, |
| 52 | + `build should exit non-zero; output:\n${out}`, |
| 53 | + ).not.toBe(0); |
| 54 | + // The ORIGINAL render error reaches the build (not a baked error page). |
| 55 | + expect(out).toContain("ctx.env is not available during pre-rendering"); |
| 56 | + expect(out).toMatch(/FAIL\s+\/prerender-boom/); |
| 57 | + }); |
| 58 | + |
| 59 | + test('prerender.onError "warn" skips the route and the build succeeds', async () => { |
| 60 | + const res = await x("pnpm", ["build"], { |
| 61 | + nodeOptions: { |
| 62 | + cwd, |
| 63 | + env: buildEnv({ |
| 64 | + RANGO_TEST_PRERENDER_ERROR: "1", |
| 65 | + RANGO_TEST_PRERENDER_ONERROR: "warn", |
| 66 | + }), |
| 67 | + }, |
| 68 | + throwOnError: false, |
| 69 | + }); |
| 70 | + const out = `${res.stdout}\n${res.stderr}`; |
| 71 | + expect( |
| 72 | + res.exitCode, |
| 73 | + `warn build should succeed (exit 0); output:\n${out}`, |
| 74 | + ).toBe(0); |
| 75 | + expect(out).toMatch(/WARN\s+\/prerender-boom.*prerender\.onError: "warn"/); |
| 76 | + // Skipped, not baked. |
| 77 | + expect(out).toContain("Pre-render complete: 0 done, 1 skipped"); |
| 78 | + }); |
| 79 | + |
| 80 | + test("a Static() handler render error also fails the build by default", async () => { |
| 81 | + // No prerender error this run, so the prerender phase succeeds and the build |
| 82 | + // reaches renderStaticHandlers, where StaticBoom throws and the same |
| 83 | + // prerender.onError policy (default "fail") fails the build. |
| 84 | + const res = await x("pnpm", ["build"], { |
| 85 | + nodeOptions: { cwd, env: buildEnv({ RANGO_TEST_STATIC_ERROR: "1" }) }, |
| 86 | + throwOnError: false, |
| 87 | + }); |
| 88 | + const out = `${res.stdout}\n${res.stderr}`; |
| 89 | + expect( |
| 90 | + res.exitCode, |
| 91 | + `static build should exit non-zero; output:\n${out}`, |
| 92 | + ).not.toBe(0); |
| 93 | + expect(out).toContain("static build-time render failure"); |
| 94 | + expect(out).toMatch(/FAIL\s+StaticBoom/); |
| 95 | + }); |
| 96 | + |
| 97 | + test('a Static() handler under prerender.onError "warn" is skipped, not baked', async () => { |
| 98 | + const res = await x("pnpm", ["build"], { |
| 99 | + nodeOptions: { |
| 100 | + cwd, |
| 101 | + env: buildEnv({ |
| 102 | + RANGO_TEST_STATIC_ERROR: "1", |
| 103 | + RANGO_TEST_PRERENDER_ONERROR: "warn", |
| 104 | + }), |
| 105 | + }, |
| 106 | + throwOnError: false, |
| 107 | + }); |
| 108 | + const out = `${res.stdout}\n${res.stderr}`; |
| 109 | + expect( |
| 110 | + res.exitCode, |
| 111 | + `static warn build should succeed (exit 0); output:\n${out}`, |
| 112 | + ).toBe(0); |
| 113 | + expect(out).toMatch(/WARN\s+StaticBoom.*prerender\.onError: "warn"/); |
| 114 | + // Skipped, not baked: the static render step reports it as skipped. |
| 115 | + expect(out).toContain("Static render complete: 0 done, 1 skipped"); |
| 116 | + }); |
| 117 | +}); |
0 commit comments