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 { retry } from 'next-test-utils'
describe('app dir - service worker (multiple registrations error)', () => {
const { next, isTurbopack, isNextDev } = nextTestSetup({
files: __dirname,
skipStart: true,
})
if (!isTurbopack) {
// Compiling `navigator.serviceWorker.register(new URL(...))` is a
// Turbopack-only feature.
it('skips on webpack', () => {})
return
}
// Compiling `navigator.serviceWorker.register(new URL(...))` is a Turbopack-only feature.
;(process.env.IS_TURBOPACK_TEST ? describe : describe.skip)(
'app dir - service worker (multiple registrations error)',
() => {
const { next, skipped, isNextDev } = nextTestSetup({
files: __dirname,
skipStart: true,
skipDeployment: true,
})
it('errors when different service worker files are registered', async () => {
// In production `next.start()` runs the build, which fails. In dev the server
// boots and the failure surfaces when the registering page is compiled.
await next.start().catch(() => {})
if (isNextDev) {
await next.fetch('/').catch(() => {})
if (skipped) {
return
}
await retry(async () => {
expect(next.cliOutput).toContain(
'Multiple service workers with different source files'
)
it('errors when different service worker files are registered', async () => {
// In production `next.start()` runs the build, which fails. In dev the server
// 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 { retry } from 'next-test-utils'
describe('app dir - service worker (one worker per scope)', () => {
const { next, isTurbopack } = nextTestSetup({
files: __dirname,
})
// Compiling `navigator.serviceWorker.register(new URL(...))` is a
// Turbopack-only feature.
;(process.env.IS_TURBOPACK_TEST ? describe : describe.skip)(
'app dir - service worker (one worker per scope)',
() => {
const { next } = nextTestSetup({
files: __dirname,
})
if (!isTurbopack) {
it('skips on webpack', () => {})
return
}
it('serves one worker per scope at scope-derived file names', async () => {
const browser = await next.browser('/')
it('serves one worker per scope at scope-derived file names', async () => {
const browser = await next.browser('/')
await retry(async () => {
expect(await browser.elementByCss('#root-scope').text()).toBe('/')
})
await retry(async () => {
expect(await browser.elementByCss('#offline-scope').text()).toBe(
'/offline/mode'
)
})
await retry(async () => {
expect(await browser.elementByCss('#root-scope').text()).toBe('/')
})
await retry(async () => {
expect(await browser.elementByCss('#offline-scope').text()).toBe(
'/offline/mode'
)
})
// The root scope "/" is served unhashed at /sw.js. Both workers are served
// as mutable, always-revalidated assets (never immutable).
const root = await next.fetch('/sw.js')
expect(root.status).toBe(200)
expect(root.headers.get('cache-control')).toContain('max-age=0')
expect(root.headers.get('cache-control')).not.toContain('immutable')
// The root scope "/" is served unhashed at /sw.js. Both workers are served
// as mutable, always-revalidated assets (never immutable).
const root = await next.fetch('/sw.js')
expect(root.status).toBe(200)
expect(root.headers.get('cache-control')).toContain('max-age=0')
expect(root.headers.get('cache-control')).not.toContain('immutable')
// scope "/offline/mode" -> a distinct, scope-derived file name. The slug is
// lossy, so a hash of the scope is appended to keep file names unique.
let offlineScript = ''
await retry(async () => {
offlineScript = await browser.elementByCss('#offline-script').text()
expect(offlineScript).toMatch(/^\/sw-offline-mode-[0-9a-f]+\.js$/)
})
// scope "/offline/mode" -> a distinct, scope-derived file name. The slug is
// lossy, so a hash of the scope is appended to keep file names unique.
let offlineScript = ''
await retry(async () => {
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')
})
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 { retry } from 'next-test-utils'
describe('app dir - service worker register', () => {
const { next, isTurbopack } = nextTestSetup({
files: __dirname,
})
if (!isTurbopack) {
// Compiling `navigator.serviceWorker.register(new URL(...))` is a
// 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'
)
// Compiling `navigator.serviceWorker.register(new URL(...))` is a
// Turbopack-only feature.
;(process.env.IS_TURBOPACK_TEST ? describe : describe.skip)(
'app dir - service worker register',
() => {
const { next } = nextTestSetup({
files: __dirname,
})
// Served from the origin root, so the registration scope is the whole origin
// with no Service-Worker-Allowed header needed.
const scope = await browser.elementByCss('#sw-scope').text()
expect(new URL(scope).pathname).toBe('/')
it('registers the service worker and controls the page at scope "/"', async () => {
const browser = await next.browser('/')
// 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')
})
await retry(async () => {
expect(await browser.elementByCss('#sw-controller').text()).toBe(
'controlled'
)
})
it('serves the worker chunk at the root as a revalidated, mutable asset', async () => {
const browser = await next.browser('/')
await retry(async () => {
expect(await browser.elementByCss('#sw-script').text()).toBe('/sw.js')
// Served from the origin root, so the registration scope is the whole origin
// with no Service-Worker-Allowed header needed.
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.
const script = await browser.elementByCss('#sw-script').text()
expect(script).toBe('/sw.js')
})
const res = await next.fetch('/sw.js')
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')
it('serves the worker chunk at the root as a revalidated, mutable asset', async () => {
const browser = await next.browser('/')
await retry(async () => {
expect(await browser.elementByCss('#sw-script').text()).toBe('/sw.js')
})
const res = await next.fetch('/sw.js')
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
// revalidations into cheap 304s instead of re-downloading the worker body.
const etag = res.headers.get('etag')
expect(etag).toBeTruthy()
// Revalidated on every use, so it must ship an ETag to turn those
// revalidations into cheap 304s instead of re-downloading the worker body.
const etag = res.headers.get('etag')
expect(etag).toBeTruthy()
// A conditional re-fetch with the ETag returns 304 Not Modified (no body),
// confirming we don't over-fetch an unchanged worker.
const conditional = await next.fetch('/sw.js', {
headers: { 'If-None-Match': etag },
// A conditional re-fetch with the ETag returns 304 Not Modified (no body),
// confirming we don't over-fetch an unchanged worker.
const conditional = await next.fetch('/sw.js', {
headers: { 'If-None-Match': etag },
})
expect(conditional.status).toBe(304)
})
expect(conditional.status).toBe(304)
})
it('intercepts fetches within scope', async () => {
const browser = await next.browser('/')
it('intercepts fetches within scope', async () => {
const browser = await next.browser('/')
await retry(async () => {
expect(await browser.elementByCss('#sw-controller').text()).toBe(
'controlled'
)
})
await retry(async () => {
expect(await browser.elementByCss('#sw-controller').text()).toBe(
'controlled'
)
})
await browser.elementByCss('button').click()
await browser.elementByCss('button').click()
await retry(async () => {
expect(await browser.elementByCss('#fetch-result').text()).toBe(
'intercepted-by-sw'
)
await retry(async () => {
expect(await browser.elementByCss('#fetch-result').text()).toBe(
'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