Skip to content

@ogygia/rewrite

Testing

A rewrite's .check() runs the whole round trip on one piece of markup in a unit test, and in dev ogygia compares every restored element with what Svelte rendered and names the first node that differs.

On this page

A render that drops a mark or reorders a child breaks hydration only in the browser, so you want to catch it before it ships. Every rewrite has .check() for a unit test, and dev checks each restored element on every page load.

check()

.check(html, { kind, csr, event }) runs the whole round trip on one piece of markup: mark, your render, settle, then (with a DOM, as in Vitest browser mode or jsdom) parse, restore, and compare each restored element with Svelte’s original. kind defaults to 'region', csr to false, and event (the request your render sees) to a GET for /. It restores into a document of its own and leaves nothing in the page running the test: no sheets in its head, no logged restore warnings.

It returns:

  • html: what ogygia would send;
  • head: what went into <head> (for a document) or what the answer carries for the head;
  • restored: how many elements ogygia restores;
  • failed: each element whose render failed, as { name, error }, where error starts with the message code;
  • dom: the restored DOM, or null without a DOM;
  • mismatches: each restored element whose light DOM isn’t exactly what Svelte rendered (empty without a DOM).

Run it in Vitest browser mode or under jsdom. In Node there’s no DOM to restore into, so dom is null, mismatches is always empty, and a broken restore still passes.

In the example, a test checks that every element renders and comes back exactly. kind: 'region' makes every element a restored one, the strictest case.

// @vitest-environment jsdom
import { expect, it } from 'vitest';
import { components } from './components';

const markup = '<my-button><span>Sign in</span></my-button>';

it('restores the light DOM exactly', async () => {
  const r = await components.check(markup, { kind: 'region' });
  expect(r.failed).toEqual([]);
  expect(r.mismatches).toEqual([]);
  expect(r.restored).toBeGreaterThan(0);
});

Dev check

In dev, ogygia sends each restored element’s original children with the page, and the restore compares them: node types, tags, text, comments and attributes. A difference is an error in the console that names the first node that differs (restore_markup_broken), and an ogygia:restore-mismatch event. The event is dispatched on the element and bubbles, and its detail is { host, diff }: the tag name and the same text.

[ogygia] the rewrite broke hydration markup: <my-button> > [1]: Svelte has <i>, the restored markup has nothing

Message codes

The messages you can meet with a rewrite, and where each is explained. handle_transform_moved comes from ogygia itself; the rest are @ogygia/rewrite‘s.

CodeKindWhen
rewrite_plugin_missingerrorA rewrite handle runs, but rewrite() isn’t in the Vite config (Installation).
rewrite_after_ogygiaerrorA rewrite is listed after ogygia.handle() (Server handle).
handle_transform_movederrorogygia.handle({ transform }) was passed (Why not transformPageChunk).
rewrite_element_failedwarningAn element’s render still failed alone (Failures).
rewrite_marks_lostwarningA render dropped og-h (Failures).
rewrite_element_splitwarningA matched element spans two chunks of a streamed page (Streaming).
rewrite_page_failederrorA page function threw or dropped marks (Failures).
restorer_unloadederrorThe restorer chunk failed to download (Restoration).
restore_markup_brokenerrorDev: a restored element’s light DOM differs from Svelte’s (Dev check).
restore_latewarningA component runtime upgraded the element first (Browser warnings).
restore_host_invalidwarningA marked element can’t have a shadow root (Browser warnings).
restore_sheet_missingwarningog-shadow names a key no sheet carries (Stylesheets).
restore_handoff_unknownwarningog-handoff is neither adopt nor replace (Handoff).
rule_rewrite_after_cancelrule (dev)A cancelled request’s rewrite kept going. A bug in ogygia: please report it.