|
| 1 | +import { describe, it, expect, beforeAll, afterAll } from "vitest"; |
| 2 | +import { createServer } from "node:http"; |
| 3 | +import { readFileSync } from "node:fs"; |
| 4 | +import { fileURLToPath } from "node:url"; |
| 5 | +import { dirname, join } from "node:path"; |
| 6 | +import { runSnippets, VIEWPORT_PRESETS } from "../../src/runner.js"; |
| 7 | +import { loadSnippet } from "../../src/load-snippet.js"; |
| 8 | + |
| 9 | +const HERE = dirname(fileURLToPath(import.meta.url)); |
| 10 | +const FIXTURE_HTML = readFileSync(join(HERE, "../fixtures/index.html"), "utf8"); |
| 11 | + |
| 12 | +let server; |
| 13 | +let baseUrl; |
| 14 | + |
| 15 | +beforeAll( |
| 16 | + () => |
| 17 | + new Promise((resolve) => { |
| 18 | + server = createServer((_req, res) => { |
| 19 | + res.writeHead(200, { "Content-Type": "text/html" }); |
| 20 | + res.end(FIXTURE_HTML); |
| 21 | + }); |
| 22 | + server.listen(0, "127.0.0.1", () => { |
| 23 | + const { port } = server.address(); |
| 24 | + baseUrl = `http://127.0.0.1:${port}`; |
| 25 | + resolve(); |
| 26 | + }); |
| 27 | + }), |
| 28 | + 10000 |
| 29 | +); |
| 30 | + |
| 31 | +afterAll( |
| 32 | + () => |
| 33 | + new Promise((resolve) => { |
| 34 | + server.close(resolve); |
| 35 | + }) |
| 36 | +); |
| 37 | + |
| 38 | +function makeItems(ids) { |
| 39 | + return ids.map((id) => ({ |
| 40 | + id, |
| 41 | + path: `CoreWebVitals/${id}`, |
| 42 | + source: loadSnippet(`CoreWebVitals/${id}`), |
| 43 | + })); |
| 44 | +} |
| 45 | + |
| 46 | +describe("Core Web Vitals snippets on a local page", () => { |
| 47 | + it( |
| 48 | + "LCP returns a numeric value with a rating", |
| 49 | + async () => { |
| 50 | + const { results } = await runSnippets({ |
| 51 | + url: baseUrl, |
| 52 | + items: makeItems(["LCP"]), |
| 53 | + waitMs: 500, |
| 54 | + viewport: VIEWPORT_PRESETS.mobile, |
| 55 | + }); |
| 56 | + |
| 57 | + const lcp = results.find((r) => r.id === "LCP"); |
| 58 | + expect(lcp.status).toBe("ok"); |
| 59 | + expect(typeof lcp.value).toBe("number"); |
| 60 | + expect(lcp.value).toBeGreaterThanOrEqual(0); |
| 61 | + expect(["good", "needs-improvement", "poor"]).toContain(lcp.rating); |
| 62 | + expect(lcp.unit).toBe("ms"); |
| 63 | + }, |
| 64 | + 30000 |
| 65 | + ); |
| 66 | + |
| 67 | + it( |
| 68 | + "CLS returns 0 on a stable page", |
| 69 | + async () => { |
| 70 | + const { results } = await runSnippets({ |
| 71 | + url: baseUrl, |
| 72 | + items: makeItems(["CLS"]), |
| 73 | + waitMs: 500, |
| 74 | + viewport: VIEWPORT_PRESETS.mobile, |
| 75 | + }); |
| 76 | + |
| 77 | + const cls = results.find((r) => r.id === "CLS"); |
| 78 | + expect(cls.status).toBe("ok"); |
| 79 | + expect(cls.value).toBe(0); |
| 80 | + expect(cls.rating).toBe("good"); |
| 81 | + expect(cls.unit).toBe("score"); |
| 82 | + }, |
| 83 | + 30000 |
| 84 | + ); |
| 85 | + |
| 86 | + it( |
| 87 | + "respects the viewport preset passed to runSnippets", |
| 88 | + async () => { |
| 89 | + const { results: mobileResults } = await runSnippets({ |
| 90 | + url: baseUrl, |
| 91 | + items: makeItems(["LCP"]), |
| 92 | + waitMs: 500, |
| 93 | + viewport: VIEWPORT_PRESETS.mobile, |
| 94 | + }); |
| 95 | + const { results: desktopResults } = await runSnippets({ |
| 96 | + url: baseUrl, |
| 97 | + items: makeItems(["LCP"]), |
| 98 | + waitMs: 500, |
| 99 | + viewport: VIEWPORT_PRESETS.desktop, |
| 100 | + }); |
| 101 | + |
| 102 | + expect(mobileResults[0].status).toBe("ok"); |
| 103 | + expect(desktopResults[0].status).toBe("ok"); |
| 104 | + }, |
| 105 | + 60000 |
| 106 | + ); |
| 107 | +}); |
0 commit comments