@ogygia/testing
Hydration checks
ogygia.hydration() returns each island's hydration status with the difference for one whose markup changed, and toHydrateCleanly() fails a test on the island that failed, recovered, was healed or changed.
An island should wake from the markup the server sent. When the browser’s first render differs, the visitor sees a flash, loses what they typed, or a click lands on something that moved. ogygia.hydration() checks every island on the page the way the dock’s Hydration tab does, and toHydrateCleanly() fails the test on the ones that didn’t wake cleanly, with the first difference in the message.
A failure reads like this:
import { test, expect } from './fixtures';
test('every island hydrates from the server markup', async ({
page,
ogygia
}) => {
await page.goto('/');
expect(await ogygia.hydration()).toHydrateCleanly();
});islands that did not hydrate cleanly:
- Clock: changed — −rendered on the server +rendered on the browserStatuses
Each island gets one status:
| Status | What happened | Fails toHydrateCleanly() |
|---|---|---|
failed | Its module threw while it loaded or mounted. reason has the error’s first line. | yes |
recovered | Svelte threw the server’s DOM away and rendered it again, with the runtime’s reason. | yes |
healed | Another script edited the island before it woke, and the runtime put the server’s markup back. | yes |
changed | The browser’s first render differs from the server’s. diff holds the difference. | yes |
asleep | It hasn’t woken yet, such as a visible island below the fold. | no |
clean | It woke from the server’s markup with nothing changed. | no |
An asleep island passes because there’s nothing to check yet. To check one, wake it first (scroll it into view, click it) and call hydration() again.
Report
hydration() waits for the page to settle, then returns { kind: 'hydration', measured, engine, islands }. measured is false on a build whose devtools switch was off when the page loaded, since the before-and-after markup needs a measured load.
Each entry of islands has:
| Field | What it holds |
|---|---|
name, fp | The island’s component name and its fingerprint. |
key | The instance, such as Carousel#2 (Instance keys). |
wake | Its wake schedule. |
status, reason | One of the statuses, and why. |
diff | For any status but clean, when the server’s markup was snapshotted: { hunks, removed, added, partial }, else null. partial means there were too many changes and only where they start is shown. |
t0, done | When it began to wake and when it could respond, in ms on the page’s clock, or null if it hasn’t woken. |
attributesOnly | For changed: true when the elements and text are the same and only attributes differ. |
attributes | With attributesOnly, each difference as { tag, name, from, to } (null for absent). |
inputDuringHydration | true when a pointer, a key press or focus landed on the island while it hydrated. |
ignored | The attribute changes ignoreAttributes left out. |
Since t0 and done share the page’s clock with a recorded session, one report tells you which step of a test woke an island.
import { test, expect } from './fixtures';
test('the cart wakes only when opened', async ({
page,
ogygia
}) => {
await page.goto('/');
const before = await ogygia.hydration();
expect(
before.islands.find((i) => i.name === 'Cart')?.status
).toBe('asleep');
await page.getByRole('button', { name: 'Cart' }).click();
const after = await ogygia.hydration();
expect(
after.islands.find((i) => i.name === 'Cart')?.status
).toBe('clean');
});Instance keys
Each island has a key for its instance, so a failure or a comparison can say which carousel broke. By default it’s Name#n, the n-th island of that name in page order. That number shifts when an island is added earlier on the page.
For a key that survives such changes, set islandKey to an attribute your app renders, such as a CMS block’s id. Each island is then keyed by that attribute on itself or on its nearest ancestor that has it, across shadow roots, so an island inside a web component finds the block around the component. Two islands under the same keyed element become id and id#2. An island with no such ancestor keeps Name#n.
import { defineConfig } from '@playwright/test';
import type { OgygiaTestOptions } from '@ogygia/testing';
export default defineConfig<{
ogygiaOptions: OgygiaTestOptions;
}>({
use: {
ogygiaOptions: {
islandKey: 'data-block-id'
}
}
});Changed markup
A changed island says what kind of change it was, because not every change is the app rendering differently:
attributesOnly: truemeans the elements and text match and only attributes differ. Often that’s a web component setting its owntabindexoraria-*as it upgrades, or an inline style set and cleared.inputDuringHydration: truemeans input landed on the island while it hydrated, so its DOM may have answered that input. A test that clicks before the page settled causes this.
Both fields come from engine version 3. toHydrateCleanly() can let either case through:
import { test, expect } from './fixtures';
test('the signup form hydrates cleanly', async ({
page,
ogygia
}) => {
await page.goto('/signup');
expect(await ogygia.hydration()).toHydrateCleanly({
allowAttributeChanges: true,
allowInput: true
});
});Ignoring attributes
To leave chosen attribute changes out of the report altogether, list them in ignoreAttributes. A rule matches the element’s tag, the attribute’s name and its new value (or its old value when the attribute was removed); tag and value are optional. A string matches exactly and a RegExp matches by test.
Rules apply to attribute-only changes. An island whose every difference matches reads clean, with what was left out in ignored. An island with other differences left stays changed, its attributes listing only those.
import { defineConfig } from '@playwright/test';
import type { OgygiaTestOptions } from '@ogygia/testing';
export default defineConfig<{
ogygiaOptions: OgygiaTestOptions;
}>({
use: {
ogygiaOptions: {
ignoreAttributes: [
{ tag: /^ds-/, name: /^(tabindex|aria-)/ },
{ name: 'style', value: '' }
]
}
}
});toHydrateCleanly
toHydrateCleanly(options?) passes when no island has a status other than clean or asleep. Its options:
allow: more statuses to let through, such as['healed']on a page where a consent script edits the DOM;allowAttributeChanges: let achangedisland through whenattributesOnlyistrue;allowInput: let achangedisland through wheninputDuringHydrationistrue.
It takes only a hydration() report. Given any other report, it fails and says so.
import { test, expect } from './fixtures';
test('the legacy page hydrates, healed islands aside', async ({
page,
ogygia
}) => {
await page.goto('/legacy');
expect(await ogygia.hydration()).toHydrateCleanly({
allow: ['healed']
});
});