Files
LivingDexTracker/tests/build/build.test.ts
T
Josh Creek f9b7bbdf0a fix(pwa): honour the adapter flag and precache an offline entry point
Three defects that together meant `npm run test:build` could not pass in any of
its four variants:

- svelte.config.js constructed adapter-netlify inline and never used the
  `adapter` export from adapter.mjs, so NODE_ADAPTER=true still produced a flat
  build/ directory while the build test expects the node adapter's build/client
  layout. adapter.mjs now returns netlify (the deployment target) or node, and
  svelte.config.js consumes it.

- No route is prerendered, so workbox's glob found no HTML document and a
  generateSW build precached nothing navigable: the app had no offline support
  in that mode at all. Adds the root entry and a navigation fallback, matching
  what prompt-sw.ts already did by hand for injectManifest builds.

- The build scripts used by the tests skipped the tailwind step that `build`
  runs, so static/output.css was never generated on a clean checkout and the
  app under test had no stylesheet.

The offline entry point assertion now also accepts the unquoted object key that
prompt-sw.ts's own precache call survives minification as.
2026-09-13 17:38:42 +01:00

45 lines
1.9 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { existsSync, readFileSync } from 'node:fs';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import { generateSW } from '../../pwa.mjs';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import { nodeAdapter } from '../../adapter.mjs';
describe(`test-build: ${nodeAdapter ? 'node' : 'static'} adapter`, () => {
it(`service worker is generated: ${generateSW ? 'sw.js' : 'prompt-sw.js'}`, () => {
const swName = `./build/${nodeAdapter ? 'client/' : ''}${generateSW ? 'sw.js' : 'prompt-sw.js'}`;
expect(existsSync(swName), `${swName} doesn't exist`).toBeTruthy();
const webManifest = `./build/${nodeAdapter ? 'client/' : ''}manifest.webmanifest`;
expect(existsSync(webManifest), `${webManifest} doesn't exist`).toBeTruthy();
const swContent = readFileSync(swName, 'utf-8');
let match: RegExpMatchArray | null;
if (generateSW) {
match = swContent.match(/define\(\['\.\/(workbox-\w+)'/);
expect(
match && match.length === 2,
`workbox-***.js entry not found in ${swName}`
).toBeTruthy();
const workboxName = `./build/${nodeAdapter ? 'client/' : ''}${match?.[1]}.js`;
expect(existsSync(workboxName), `${workboxName} doesn't exist`).toBeTruthy();
}
match = swContent.match(/"url":\s*"manifest\.webmanifest"/);
expect(
match && match.length === 1,
'missing manifest.webmanifest in sw precache manifest'
).toBeTruthy();
// The generateSW manifest is emitted as JSON ("url": "/"), while prompt-sw.ts's own
// `precache([{ url: '/' }])` survives minification as an unquoted key (url:"/").
match = swContent.match(/"?url"?:\s*"(?:\/|index\.html)"/);
expect(
match && match.length === 1,
'missing offline entry point in sw precache manifest'
).toBeTruthy();
if (nodeAdapter) {
match = swContent.match(/"url":\s*"server\//);
expect(match === null, 'found server/ entries in sw precache manifest').toBeTruthy();
}
});
});