@ogygia/testing
Setup
Wire @ogygia/testing into an app and a Playwright suite. The testing() plugin, the ogygia fixture and its options, the dev server and builds, lean testing, what measuring costs, and engine versions.
On this page
A test measures a page through the testing engine, a script the test side puts into every page before the page’s own scripts run. The engine can only see what ogygia’s runtime tells it, so the app under test has to hand the runtime’s hooks over. That’s the job of the testing() plugin. Your tests then use the ogygia fixture, which injects the engine and turns measuring on for the browser it drives.
import {
test as base,
expect as baseExpect
} from '@playwright/test';
import { withOgygia } from '@ogygia/testing';
export const { test, expect } = withOgygia(base, baseExpect);Vite plugin
List testing() from @ogygia/testing/vite beside ogygia(), before sveltekit(). It adds two things:
- a browser half that passes each runtime hook (an island mounted, woke, hydrated or failed; a hole answered; a navigation started) to the engine. Without an engine in the page, each hook reads one global and stops;
- on the dev server only, the route
/__ogygia_devtools_meta, which tells the engine each island’s component name. A build lends the names from the app’s own chunk, on the engine’s first ask.
The dock feeds the engine too, so an app that lists devtools() with the dock on works without testing(). With devtools({ dock: false }), or with no devtools at all, list testing(). With neither, every call waits for the testing API and then throws testing_api_absent.
What else you list decides what a build can measure:
| The app lists | What works on a build |
|---|---|
testing() alone | page() without cpu and route, hydration(), record() without the session’s cpu, settled(). |
devtools(), the dock on (with or without testing()) | All of that, plus page().cpu, the session’s cpu and page().route: the dock makes the server send Document-Policy: js-profiling and the og-route Server-Timing to a browser with the switch on. |
profiler: true on either | Adds profile() and record(…, { server: true }), which read the SSR profiler. |
So devtools({ dock: false, profiler: true }) with testing() gives you profile(), but page().cpu and page().route stay null on a build. The dev server is the same for cpu, which needs the dock there too, and never sets route. The installation lists testing() with the dock on, which covers it all. The smallest setup, for hydration and session checks only:
import { sveltekit } from '@sveltejs/kit/vite';
import { ogygia } from 'ogygia/vite';
import { testing } from '@ogygia/testing/vite';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [
ogygia(),
...(process.env.E2E ? [testing()] : []),
sveltekit()
]
});Playwright fixture
withOgygia(test, expect) takes your Playwright test and expect and returns both extended. The ogygia fixture is made fresh for each test, and before your test body runs it:
- turns measuring on for the browser context: the
og_devtools=oncookie for yourbaseURL(andog_testing=leanwith lean testing). Without abaseURL, it sets the cookie on the first page load the test makes; - builds a driver for the test’s
page(playwrightDriver); - injects the engine with
page.addInitScript, and waits for that, so nopage.gotocan run first.
After the test, it attaches every report the test took as ogygia.json (CI reports). The returned expect carries the three matchers, with their types.
import { test, expect } from './fixtures';
test('home hydrates cleanly', async ({ page, ogygia }) => {
await page.goto('/');
expect(await ogygia.hydration()).toHydrateCleanly();
expect(ogygia.reports).toHaveLength(1);
});Fixture options
Set options with Playwright’s use, as ogygiaOptions, for the whole config, a project or a test.describe. Type the config with defineConfig<…> so the key is checked. ogygiaOptions is one option, so a nearer use replaces the whole object rather than merging into it: a spec file that sets { lean: true } loses the config’s profilerKey. Keep shared options in a module and spread them, as Lean testing does.
| Option | Default | What it does |
|---|---|---|
profilerKey | none | The profiler’s secret, sent as x-profiler-key by profile() and the server window. A build needs it; the dev server doesn’t. |
profilerBase | /__profiler | Where the profiler is mounted, when you moved it with its path option. |
timeout | 15000 | How long each call waits, in ms, for the page’s testing API before it throws testing_api_absent. |
islandKey | none | An attribute that names an island instance in hydration() (Instance keys). |
ignoreAttributes | [] | Attribute changes hydration() leaves out (Ignoring attributes). |
cpuFunctions | 15 | How many of the page load’s hottest functions page().cpu.fns keeps, up to 500. |
attach | true | Attach the test’s reports as ogygia.json. Set false when your harness writes its own. |
lean | false | Lean testing: the page runs the testing engine and nothing else of the devtools. |
import type { OgygiaTestOptions } from '@ogygia/testing';
export const ogygiaOptions: OgygiaTestOptions = {
profilerKey: process.env.OGYGIA_PROFILER_SECRET,
islandKey: 'data-block-id'
};import { defineConfig } from '@playwright/test';
import type { OgygiaTestOptions } from '@ogygia/testing';
import { ogygiaOptions } from './tests/ogygia-options';
export default defineConfig<{
ogygiaOptions: OgygiaTestOptions;
}>({
use: { baseURL: 'http://127.0.0.1:4173', ogygiaOptions }
});Builds
Every call works on the dev server and on a build: a local vite preview or a deployed preview, as long as that build listed testing() or the dock. Measure on a build when the numbers matter. The dev server’s timings include Vite’s transforms, and the page’s weight is only known for a build.
A build measures a page load only when the devtools switch was on before the page loaded. The fixture sets the switch’s cookie for you. If you drive pages without the fixture and forget it, page() throws “the page load was not measured”; call turnOnDevtools() first. With the dock listed, a browser with the switch on is also sent Document-Policy: js-profiling, so the main-thread CPU sampler runs without the app sending the header (Vite plugin).
On a build, the profiler is closed until you start the server with a secret of 16 bytes or more and give the tests the same string as profilerKey (Secret). A suite that builds and serves the app itself:
import { defineConfig } from '@playwright/test';
import type { OgygiaTestOptions } from '@ogygia/testing';
const secret =
process.env.OGYGIA_PROFILER_SECRET ??
'local-secret-of-32-characters-xx';
export default defineConfig<{
ogygiaOptions: OgygiaTestOptions;
}>({
webServer: {
command:
'pnpm vite build && pnpm vite preview --port 4173 --strictPort',
url: 'http://127.0.0.1:4173',
env: { E2E: '1', OGYGIA_PROFILER_SECRET: secret }
},
use: {
baseURL: 'http://127.0.0.1:4173',
ogygiaOptions: { profilerKey: secret }
}
});Lean testing
The fixture turns the devtools switch on, and on a build that lists the full devtools, that means the page also runs the launcher, the dock’s watchers and the server’s event side channel. With lean: true, the page runs the testing engine and nothing else: no launcher, no dock, no watcher of its timers and no event side channel in its HTML. It loads what it would load with the devtools off, so what you measure is closer to what a visitor gets.
Lean testing sets the og_testing=lean cookie next to og_devtools=on. Outside the fixture, ?og-testing=lean on a page’s address does the same, as does turnOnDevtools(page, baseURL, { lean: true }). Findings that need the server’s events aren’t available in this mode. Every report says which mode ran in engine.lean. An engine older than version 3 ignores the cookie and runs the full devtools.
import { test, expect } from './fixtures';
import { ogygiaOptions } from './ogygia-options';
test.use({
ogygiaOptions: {
...ogygiaOptions,
lean: true
}
});
test('the landing page, as a visitor loads it', async ({
page,
ogygia
}) => {
await page.goto('/');
const load = await ogygia.page();
expect(load.engine.lean).toBe(true);
expect(load).toMeetBudget({ lcp: 2500 });
});Measuring cost
Measuring has a cost a visitor never pays: the engine checks each island’s markup as it hydrates and builds the reports on the main thread. page().self says what it cost so far, so you can subtract it from a CPU measurement:
script_ms: the main-thread time ogygia spent on its own measuring;phases: the same time by phase, such asislandsandengine(the injected engine’s own start);bytes: the engine’s files the page downloaded. It’s0with the fixture, because the engine is injected rather than fetched.
self comes from engine version 3. page().cpu needs the dock listed, and stays null while the load’s sampler records: about 8 seconds, longer (up to 20) while islands are still waking. Let the page settle, wait past the window, and check it’s there:
import { test, expect } from './fixtures';
test('the product page keeps its main thread free', async ({
page,
ogygia
}) => {
await page.goto('/products/1');
await ogygia.settled();
await page.waitForTimeout(9_000); // past the page load's CPU sampler
const load = await ogygia.page();
expect(load.cpu).not.toBeNull();
const appMs =
(load.cpu?.busy_ms ?? 0) - (load.self?.script_ms ?? 0);
expect(appMs).toBeLessThan(300);
});Engine versions
The reports are built by the page’s testing engine, and the engine that answers ships with the site’s build, not with your test’s @ogygia/testing. A check that measures an older deploy (a baseline) next to a new preview therefore gets older reports from the baseline. Every report names the engine in engine.version (3 is current), and the first older report logs testing_engine_older, once per site and version for the whole run.
The test side fills in what it knows: navigations from the loads it held back (with approx: true), and names: [] on findings. Instance keys and counts.cls are missing from older reports, so compare then counts hydration by island name and skips the session’s CLS, rather than report a change that didn’t happen.
| Version | Added | Detection |
|---|---|---|
| 1 (0.8.2) | the first engine: page(), hydration() and record() with their vitals, statuses and findings | The baseline the later versions are described against. |
| 2 | instance key, t0 and done, navigations, finding names, counts.cls, engine on every report | A router navigation that never swapped the page no longer starts a new page for page(). Hydration status as in 1. |
| 3 | attributesOnly, attributes, inputDuringHydration, ignoreAttributes, self, route, lean testing | Hydration status as in 1 and 2. The new fields only say what kind of change it was. |
Hydration status is detected the same way in every version, so an island that reads clean on one engine and changed on another really did differ in that run. Input that landed while it hydrated is the usual cause, and version 3 flags it.
Error handling
| What you see | Why |
|---|---|
testing_api_absent | The engine is in the page, but no app runtime fed it within timeout: the app lists neither testing() nor the dock, or the page isn’t an ogygia page. The message still says to list devtools() and open a csr = false page; listing testing() is the fix when the dock is off. |
| “the page load was not measured” | page() on a build where the devtools switch was off when the page loaded. Use the fixture, or call turnOnDevtools() before the first goto. |
testing_engine_older | A warning: the site was built with an older ogygia. See Engine versions. |
| “the profiler answered 401 for /cart” | profile() on a build without the right profilerKey. A 404 means the profiler isn’t listed, or the server has no secret. |
An addInitScript error | The driver couldn’t inject the engine. That error is what the first call throws. |
The engine measures ogygia’s pages, the csr = false ones with islands. On the dev server a Kit-hydrated page (csr = true) answers too, with its vitals and no islands.