Something went wrong. Try again.
Website template for structured, accessible static websites, built with HTML, CSS, TypeScript and Vite. spa-template.liberacode.dev
Something went wrong. Try again.
spa-template vite.config.test.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140import assert from "node:assert/strict";import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";import { dirname, join } from "node:path";import { test } from "node:test";import { siteConfig } from "./src/site.config.ts";import type { RouteLoader } from "./src/types/routes.ts";import { applyRouteMeta, prerenderedRoutes, renderFontPreloads, renderRouteHtml, renderRoutePage, renderSitemap, sitemapPaths, stripHeroPreloads,} from "./vite.config.ts";
const template = `<!doctype html><html lang="en"> <head> <title>Site</title> <meta name="description" content="Default"> <meta name="robots" content="index, follow"> <link rel="canonical" href="https://example.com/"> <meta property="og:url" content="https://example.com/"> <meta property="og:title" content="Site"> <link rel="preload" as="image" href="/assets/images/hero/hero-900.avif"> </head> <body> <main id="app" tabindex="-1" data-prerendered="home"><h1>Home</h1></main> </body></html>`;
const siteUrl = siteConfig.siteUrl.replace(/\/$/, "");
const dollarText = 'echo "$$" $& $\' $` $1 grep "^foo$"';
test("renderRouteHtml inserts the page HTML literally", () => { const html = renderRouteHtml(template, "/post", `<p>${dollarText}</p>`);
assert.ok( html.includes( `<main id="app" tabindex="-1" data-prerendered="/post"><p>${dollarText}</p></main>`, ), ); assert.ok(!html.includes("<h1>Home</h1>"));});
test("applyRouteMeta inserts the title and description literally", () => { const html = applyRouteMeta(template, "/post", `Cost $& $$`, { description: "Price $'5" });
assert.ok(html.includes("<title>Cost $& $$</title>")); assert.ok(html.includes(`<meta name="description" content="Price $'5">`)); assert.ok(html.includes(`<meta property="og:title" content="Cost $& $$">`));});
test("prerenderedRoutes lists every route definition except home and lazy routes", () => { const page = { title: "Site | Page", html: "<h1>Page</h1>" }; const loader: RouteLoader = async () => page; const paths = prerenderedRoutes({ "/": { title: "Site", html: "<h1>Home</h1>" }, "/page": page, "/blog": loader, }).map(([path]) => path);
assert.deepEqual(paths, ["/page"]);});
test("renderRoutePage replaces the home content, hero preloads and route meta", () => { const html = renderRoutePage(stripHeroPreloads(template), "/contact", { title: "Site | Contact", html: "<h1>Contact</h1>", meta: { robots: "noindex, follow" }, });
assert.ok( html.includes( `<main id="app" tabindex="-1" data-prerendered="/contact"><h1>Contact</h1></main>`, ), ); assert.ok(!html.includes("<h1>Home</h1>")); assert.ok(!html.includes('rel="preload" as="image"')); assert.ok(html.includes("<title>Site | Contact</title>")); assert.ok(html.includes(`<meta name="robots" content="noindex, follow">`)); assert.ok(html.includes(`<link rel="canonical" href="${siteUrl}/contact">`)); assert.ok(html.includes(`<meta property="og:url" content="${siteUrl}/contact">`));});
test("sitemapPaths lists every index.html except pagination pages", () => { const distDir = mkdtempSync(join(tmpdir(), "sitemap-"));
try { for (const file of [ "index.html", "404.html", "contact/index.html", "blog/index.html", "blog/page/2/index.html", "blog/post/index.html", "assets/app.js", "sitemap.xml", ]) { mkdirSync(dirname(join(distDir, file)), { recursive: true }); writeFileSync(join(distDir, file), ""); }
assert.deepEqual(sitemapPaths(distDir), ["/", "/blog", "/blog/post", "/contact"]); } finally { rmSync(distDir, { recursive: true, force: true }); }});
test("renderSitemap writes absolute URLs from siteUrl", () => { assert.equal( renderSitemap(["/", "/a&b"]), `<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url><loc>${siteUrl}/</loc></url> <url><loc>${siteUrl}/a&b</loc></url></urlset>`, );});
test("renderFontPreloads preloads only the listed fonts", () => { const assets = [ { fileName: "assets/Body-a1.woff2", names: ["Body.woff2"] }, { fileName: "assets/Body-Italic-b2.woff2", names: ["Body-Italic.woff2"] }, { fileName: "assets/Heading-c3.woff2", names: ["Heading.woff2"] }, ];
assert.deepEqual(renderFontPreloads(assets, ["Heading.woff2", "Body.woff2"]), [ '<link rel="preload" href="/assets/Heading-c3.woff2" as="font" type="font/woff2" crossorigin>', '<link rel="preload" href="/assets/Body-a1.woff2" as="font" type="font/woff2" crossorigin>', ]); assert.deepEqual(renderFontPreloads(assets, []), []); assert.throws(() => renderFontPreloads(assets, ["Missing.woff2"]), /Missing\.woff2/);});