Skip to content

@ogygia/testing

Matchers and budgets

The three matchers @ogygia/testing adds to expect, toHaveNoFindings, toMeetBudget and toHydrateCleanly, every budget line for each kind of report, and checkBudget() for your own logic.

On this page

Every report @ogygia/testing returns is plain JSON, so you can assert on any field with ordinary expect. For the common checks there are three matchers. withOgygia adds them to the expect it returns, and ogygiaMatchers adds them to any runner’s expect.extend (Other runners).

import { test, expect } from './fixtures';

test('the pricing page holds its budget', async ({
  page,
  ogygia
}) => {
  await page.goto('/pricing');
  const load = await ogygia.page();

  expect(load).toMeetBudget({
    lcp: 2500,
    cls: 0.1,
    longTaskMs: 200
  });
  expect(load).toHaveNoFindings({ severity: 'warn' });
  expect(await ogygia.hydration()).toHydrateCleanly();
});

toHaveNoFindings

toHaveNoFindings({ severity?, ignore? }) passes when the report has no finding at or above severity (info, warn or error; by default any finding fails), except the codes listed in ignore. It reads the findings of a page, session or profile report, so the codes are the page findings, the session findings and the profiler’s findings. A hydration report has no findings, so it always passes there.

The failure lists each finding:

expected no ogygia findings at warn or above, found 1:
  - [warn] slow-interaction: A click on button "Apply" took 412 ms …
import { test, expect } from './fixtures';

test('the search box has no warnings but a known slow suggest', async ({
  page,
  ogygia
}) => {
  await page.goto('/');
  const session = await ogygia.record(() =>
    page.getByRole('searchbox').fill('shoes')
  );
  expect(session).toHaveNoFindings({
    severity: 'warn',
    ignore: ['slow-requests']
  });
});

toMeetBudget

toMeetBudget(budget) passes when every line of the budget is met. A line is checked only on the report kind it belongs to, so lcp on a session report or score on a page report is skipped, not failed. A hydration report has no budget lines, so toMeetBudget() always passes on one. A line whose value wasn’t measured (no LCP in WebKit, no jsBytes on the dev server) fails with “not measured”. The failure lists each missed line:

Every line, by report:

ReportLinePasses when
page()lcpLargest Contentful Paint ≤ the limit, in ms.
fcpFirst Contentful Paint ≤ the limit, in ms.
clsCumulative Layout Shift ≤ the limit.
inpInteraction to Next Paint ≤ the limit, in ms.
ttfbTime to first byte ≤ the limit, in ms.
longTaskMsThe page load’s long tasks add up to ≤ the limit, in ms.
islandWakeMsThe slowest island’s wake-to-ready ≤ the limit, in ms.
record()maxInteractionMsThe slowest interaction ≤ the limit, in ms.
slowInteractionsInteractions of 100 ms or more ≤ the limit.
errorsErrors, unhandled rejections and console.error calls ≤ the limit.
clsShifts with no input add up to ≤ the limit.
longTaskMsThe session’s long tasks add up to ≤ the limit, in ms.
failedRequestsFailed requests among the session’s requests (the twelve slowest) ≤ the limit.
serverBusyMsWith server: true, the server’s busy CPU ≤ the limit, in ms.
profile()scoreThe page score is at least the limit.
renderMsThe median render ≤ the limit, in ms.
jsBytesJavaScript at start ≤ the limit, in bytes (a build only).

The Budget type has every line, so one object can hold a page’s whole budget and you pass it to each report.

ogygia budget missed (page):
  - lcp: 3120 (budget 2500)
  - cls: not measured (budget 0.1)
import type { Budget } from '@ogygia/testing';

export const home: Budget = {
  lcp: 2500,
  cls: 0.1,
  islandWakeMs: 300,
  maxInteractionMs: 200,
  errors: 0,
  score: 85,
  renderMs: 150
};

toHydrateCleanly

toHydrateCleanly({ allow?, allowAttributeChanges?, allowInput? }) passes when no island failed, recovered, was healed or changed its markup. allow lets more statuses through, allowAttributeChanges passes a change only in attributes, and allowInput passes a change in an island that input landed on while it hydrated. It takes only a hydration() report. Hydration checks has the statuses and an example of each option.

import { test, expect } from './fixtures';

test('islands hydrate, a consent script aside', async ({
  page,
  ogygia
}) => {
  await page.goto('/');
  expect(await ogygia.hydration()).toHydrateCleanly({
    allow: ['healed']
  });
});

Negation

Each matcher works with .not, and its message then says what it expected to miss: “expected ogygia findings, found none”, “expected the ogygia budget to be missed, it was met”, “expected an island to hydrate badly, all were clean”. It’s useful for a test that proves a planted problem is caught.

import { test, expect } from './fixtures';

test('the broken lab page is caught', async ({
  page,
  ogygia
}) => {
  await page.goto('/lab/broken');
  expect(await ogygia.hydration()).not.toHydrateCleanly();
});

checkBudget

checkBudget(report, budget) returns the lines toMeetBudget() checks, as data: { key, measured, limit, ok }[], with measured null when nothing was measured. Use it to log every line, to warn rather than fail, or to build your own report.

import { test, expect } from './fixtures';
import { checkBudget } from '@ogygia/testing';

test('the blog index, with a soft LCP line', async ({
  page,
  ogygia
}) => {
  await page.goto('/blog');
  const lines = checkBudget(await ogygia.page(), {
    lcp: 2500,
    cls: 0.1
  });

  for (const line of lines.filter((l) => !l.ok))
    test
      .info()
      .annotations.push({
        type: 'budget',
        description: `${line.key}: ${line.measured}`
      });
  expect(lines.find((l) => l.key === 'cls')?.ok).toBe(true);
});