Unverified Commit 0c70a7c3 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

test: Fix service worker error test (#95548)

- One of them tests that an error is thrown. Don't run the deployment
test there
- The other tests are still broken when deployed, but we will fix that
separately
parent 3e11c728
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils' import { retry } from 'next-test-utils'
describe('app dir - service worker (multiple registrations error)', () => { // Compiling `navigator.serviceWorker.register(new URL(...))` is a Turbopack-only feature.
const { next, isTurbopack, isNextDev } = nextTestSetup({ ;(process.env.IS_TURBOPACK_TEST ? describe : describe.skip)(
files: __dirname, 'app dir - service worker (multiple registrations error)',
skipStart: true, () => {
}) const { next, skipped, isNextDev } = nextTestSetup({
files: __dirname,
if (!isTurbopack) { skipStart: true,
// Compiling `navigator.serviceWorker.register(new URL(...))` is a skipDeployment: true,
// Turbopack-only feature. })
it('skips on webpack', () => {})
return
}
it('errors when different service worker files are registered', async () => { if (skipped) {
// In production `next.start()` runs the build, which fails. In dev the server return
// boots and the failure surfaces when the registering page is compiled.
await next.start().catch(() => {})
if (isNextDev) {
await next.fetch('/').catch(() => {})
} }
await retry(async () => { it('errors when different service worker files are registered', async () => {
expect(next.cliOutput).toContain( // In production `next.start()` runs the build, which fails. In dev the server
'Multiple service workers with different source files' // boots and the failure surfaces when the registering page is compiled.
) await next.start().catch(() => {})
if (isNextDev) {
await next.fetch('/').catch(() => {})
}
await retry(async () => {
expect(next.cliOutput).toContain(
'Multiple service workers with different source files'
)
})
}) })
}) }
}) )
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils' import { retry } from 'next-test-utils'
describe('app dir - service worker (one worker per scope)', () => { // Compiling `navigator.serviceWorker.register(new URL(...))` is a
const { next, isTurbopack } = nextTestSetup({ // Turbopack-only feature.
files: __dirname, ;(process.env.IS_TURBOPACK_TEST ? describe : describe.skip)(
}) 'app dir - service worker (one worker per scope)',
() => {
const { next } = nextTestSetup({
files: __dirname,
})
if (!isTurbopack) { it('serves one worker per scope at scope-derived file names', async () => {
it('skips on webpack', () => {}) const browser = await next.browser('/')
return
}
it('serves one worker per scope at scope-derived file names', async () => { await retry(async () => {
const browser = await next.browser('/') expect(await browser.elementByCss('#root-scope').text()).toBe('/')
})
await retry(async () => {
expect(await browser.elementByCss('#offline-scope').text()).toBe(
'/offline/mode'
)
})
await retry(async () => { // The root scope "/" is served unhashed at /sw.js. Both workers are served
expect(await browser.elementByCss('#root-scope').text()).toBe('/') // as mutable, always-revalidated assets (never immutable).
}) const root = await next.fetch('/sw.js')
await retry(async () => { expect(root.status).toBe(200)
expect(await browser.elementByCss('#offline-scope').text()).toBe( expect(root.headers.get('cache-control')).toContain('max-age=0')
'/offline/mode' expect(root.headers.get('cache-control')).not.toContain('immutable')
)
})
// The root scope "/" is served unhashed at /sw.js. Both workers are served // scope "/offline/mode" -> a distinct, scope-derived file name. The slug is
// as mutable, always-revalidated assets (never immutable). // lossy, so a hash of the scope is appended to keep file names unique.
const root = await next.fetch('/sw.js') let offlineScript = ''
expect(root.status).toBe(200) await retry(async () => {
expect(root.headers.get('cache-control')).toContain('max-age=0') offlineScript = await browser.elementByCss('#offline-script').text()
expect(root.headers.get('cache-control')).not.toContain('immutable') expect(offlineScript).toMatch(/^\/sw-offline-mode-[0-9a-f]+\.js$/)
})
// scope "/offline/mode" -> a distinct, scope-derived file name. The slug is const offline = await next.fetch(offlineScript)
// lossy, so a hash of the scope is appended to keep file names unique. expect(offline.status).toBe(200)
let offlineScript = '' expect(offline.headers.get('cache-control')).toContain('max-age=0')
await retry(async () => { expect(offline.headers.get('cache-control')).not.toContain('immutable')
offlineScript = await browser.elementByCss('#offline-script').text()
expect(offlineScript).toMatch(/^\/sw-offline-mode-[0-9a-f]+\.js$/)
}) })
}
const offline = await next.fetch(offlineScript) )
expect(offline.status).toBe(200)
expect(offline.headers.get('cache-control')).toContain('max-age=0')
expect(offline.headers.get('cache-control')).not.toContain('immutable')
})
})
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils' import { retry } from 'next-test-utils'
describe('app dir - service worker register', () => { // Compiling `navigator.serviceWorker.register(new URL(...))` is a
const { next, isTurbopack } = nextTestSetup({ // Turbopack-only feature.
files: __dirname, ;(process.env.IS_TURBOPACK_TEST ? describe : describe.skip)(
}) 'app dir - service worker register',
() => {
if (!isTurbopack) { const { next } = nextTestSetup({
// Compiling `navigator.serviceWorker.register(new URL(...))` is a files: __dirname,
// Turbopack-only feature.
it('skips on webpack', () => {})
return
}
it('registers the service worker and controls the page at scope "/"', async () => {
const browser = await next.browser('/')
await retry(async () => {
expect(await browser.elementByCss('#sw-controller').text()).toBe(
'controlled'
)
}) })
// Served from the origin root, so the registration scope is the whole origin it('registers the service worker and controls the page at scope "/"', async () => {
// with no Service-Worker-Allowed header needed. const browser = await next.browser('/')
const scope = await browser.elementByCss('#sw-scope').text()
expect(new URL(scope).pathname).toBe('/')
// A single service worker per app is served at the fixed, root-scoped /sw.js. await retry(async () => {
const script = await browser.elementByCss('#sw-script').text() expect(await browser.elementByCss('#sw-controller').text()).toBe(
expect(script).toBe('/sw.js') 'controlled'
}) )
})
it('serves the worker chunk at the root as a revalidated, mutable asset', async () => { // Served from the origin root, so the registration scope is the whole origin
const browser = await next.browser('/') // with no Service-Worker-Allowed header needed.
await retry(async () => { const scope = await browser.elementByCss('#sw-scope').text()
expect(await browser.elementByCss('#sw-script').text()).toBe('/sw.js') expect(new URL(scope).pathname).toBe('/')
// A single service worker per app is served at the fixed, root-scoped /sw.js.
const script = await browser.elementByCss('#sw-script').text()
expect(script).toBe('/sw.js')
}) })
const res = await next.fetch('/sw.js') it('serves the worker chunk at the root as a revalidated, mutable asset', async () => {
expect(res.status).toBe(200) const browser = await next.browser('/')
// Stable URL across builds: it must be served as a mutable asset (never await retry(async () => {
// immutable) that is revalidated on every use, so a new worker ships expect(await browser.elementByCss('#sw-script').text()).toBe('/sw.js')
// immediately rather than being pinned by a long-lived cache entry. })
const cacheControl = res.headers.get('cache-control')
expect(cacheControl).not.toContain('immutable') const res = await next.fetch('/sw.js')
expect(cacheControl).toContain('max-age=0') expect(res.status).toBe(200)
// Stable URL across builds: it must be served as a mutable asset (never
// immutable) that is revalidated on every use, so a new worker ships
// immediately rather than being pinned by a long-lived cache entry.
const cacheControl = res.headers.get('cache-control')
expect(cacheControl).not.toContain('immutable')
expect(cacheControl).toContain('max-age=0')
// Revalidated on every use, so it must ship an ETag to turn those // Revalidated on every use, so it must ship an ETag to turn those
// revalidations into cheap 304s instead of re-downloading the worker body. // revalidations into cheap 304s instead of re-downloading the worker body.
const etag = res.headers.get('etag') const etag = res.headers.get('etag')
expect(etag).toBeTruthy() expect(etag).toBeTruthy()
// A conditional re-fetch with the ETag returns 304 Not Modified (no body), // A conditional re-fetch with the ETag returns 304 Not Modified (no body),
// confirming we don't over-fetch an unchanged worker. // confirming we don't over-fetch an unchanged worker.
const conditional = await next.fetch('/sw.js', { const conditional = await next.fetch('/sw.js', {
headers: { 'If-None-Match': etag }, headers: { 'If-None-Match': etag },
})
expect(conditional.status).toBe(304)
}) })
expect(conditional.status).toBe(304)
})
it('intercepts fetches within scope', async () => { it('intercepts fetches within scope', async () => {
const browser = await next.browser('/') const browser = await next.browser('/')
await retry(async () => { await retry(async () => {
expect(await browser.elementByCss('#sw-controller').text()).toBe( expect(await browser.elementByCss('#sw-controller').text()).toBe(
'controlled' 'controlled'
) )
}) })
await browser.elementByCss('button').click() await browser.elementByCss('button').click()
await retry(async () => { await retry(async () => {
expect(await browser.elementByCss('#fetch-result').text()).toBe( expect(await browser.elementByCss('#fetch-result').text()).toBe(
'intercepted-by-sw' 'intercepted-by-sw'
) )
})
}) })
}) }
}) )
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment