diff --git a/test-e2e/byte-range.test.ts b/test-e2e/byte-range.test.ts index 8f1a199b..7348ff23 100644 --- a/test-e2e/byte-range.test.ts +++ b/test-e2e/byte-range.test.ts @@ -1,20 +1,12 @@ import { test, expect } from '@playwright/test' +import { waitForServiceWorker } from './fixtures/wait-for-service-worker.js' test.describe('byte-ranges', () => { test('should be able to get a single character', async ({ page }) => { await page.goto('http://localhost:3000', { waitUntil: 'networkidle' }) // wait for service worker to load - await page.evaluate(async () => { - const registration = await window.navigator.serviceWorker.getRegistration() + await waitForServiceWorker(page) - if (registration?.active?.state === 'activated') { - return - } - - await new Promise((resolve, reject) => { - window.navigator.serviceWorker.addEventListener('controllerchange', resolve) - }) - }) const partialText = await page.evaluate(async () => { const response = await fetch('/ipfs/bafkqaddimvwgy3zao5xxe3debi', { headers: { range: 'bytes=1-2' } }) const text = await response.text() diff --git a/test-e2e/fixtures/wait-for-service-worker.ts b/test-e2e/fixtures/wait-for-service-worker.ts new file mode 100644 index 00000000..d5dd6b19 --- /dev/null +++ b/test-e2e/fixtures/wait-for-service-worker.ts @@ -0,0 +1,15 @@ +import type { Page } from '@playwright/test' + +export async function waitForServiceWorker (page: Page): Promise { + await page.evaluate(async () => { + const registration = await window.navigator.serviceWorker.getRegistration() + + if (registration?.active?.state === 'activated') { + return + } + + await new Promise((resolve, reject) => { + window.navigator.serviceWorker.addEventListener('controllerchange', resolve) + }) + }) +}