diff --git a/bun.lockb b/bun.lockb index 30fb9bf1..61d3bb85 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index c36bd24a..fb87a1aa 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "@eslint/js": "9.17.0", "@maxmilton/eslint-config": "0.0.8", "@maxmilton/stylelint-config": "0.1.2", - "@maxmilton/test-utils": "0.0.6", + "@maxmilton/test-utils": "0.0.7", "@playwright/test": "1.49.1", "@types/bun": "1.1.14", "@types/chrome": "0.0.287", diff --git a/test/e2e/fixtures.ts b/test/e2e/fixtures.ts index 529961f2..404e87f8 100644 --- a/test/e2e/fixtures.ts +++ b/test/e2e/fixtures.ts @@ -28,11 +28,11 @@ export const test = baseTest.extend<{ await context.close(); }, async extensionId({ context }, use) { - let [background] = context.serviceWorkers(); + let [sw] = context.serviceWorkers(); // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition - background ??= await context.waitForEvent('serviceworker'); + sw ??= await context.waitForEvent('serviceworker', { timeout: 200 }); - const extensionId = background.url().split('/')[2]; + const extensionId = sw.url().split('/')[2]; await use(extensionId); }, }); diff --git a/test/e2e/sw.spec.ts b/test/e2e/sw.spec.ts index 47b20940..f25018b8 100644 --- a/test/e2e/sw.spec.ts +++ b/test/e2e/sw.spec.ts @@ -1,11 +1,9 @@ import { expect, test } from './fixtures'; -test('background service worker', async ({ context }) => { - let [background] = context.serviceWorkers(); - // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition - background ??= await context.waitForEvent('serviceworker'); - - // FIXME: Better assertions - - expect(background).toBeTruthy(); +test('has a single background service worker (sw.js)', async ({ context, extensionId }) => { + const workers = context.serviceWorkers(); + expect(workers).toHaveLength(1); + expect(workers[0]?.url()).toBe(`chrome-extension://${extensionId}/sw.js`); }); + +// TODO: Check there are no console messages or unhandled errors in the worker. diff --git a/test/unit/index.test.ts b/test/unit/index.test.ts index 343d5c2b..6621dbe6 100644 --- a/test/unit/index.test.ts +++ b/test/unit/index.test.ts @@ -1,5 +1,6 @@ import { describe, expect, test } from 'bun:test'; import { readdir } from 'node:fs/promises'; +import { validate } from '@maxmilton/test-utils/html'; describe('dist files', () => { // FIXME: The bun file type is just inferred from the file extension, not the @@ -34,7 +35,7 @@ describe('dist files', () => { expect(file.type).toBe(type); // TODO: Keep this? Type seems to be resolved from the file extension, not the file data. }); - if (minBytes != null && maxBytes != null) { + if (minBytes !== undefined && maxBytes !== undefined) { test('is within expected file size limits', () => { expect.assertions(2); expect(file.size).toBeGreaterThan(minBytes); @@ -49,8 +50,17 @@ describe('dist files', () => { const distDir = await readdir('dist'); expect(distDir).toHaveLength(distFiles.length); }); + + test.each(distFiles.filter(([filename]) => filename.endsWith('.html')))( + '%s contains valid HTML', + async (filename) => { + const file = Bun.file(`dist/${filename}`); + const html = await file.text(); + const result = validate(html); + expect(result.valid).toBeTrue(); + }, + ); }); -// TODO: HTML files should be valid HTML // TODO: HTML files have correct title // TODO: HTML files have correct JS and CSS file references