Testing
The layers of tests, the two environments they run in, the component tests and the regression suite.
This page explains how the library is tested: the layers of tests, the recording runtime, the two environments, the component tests and the regression suite.
The Layers
Section titled “The Layers”Every change is made test first, so each behaviour has the test that drove it. Two more layers stand on top of those tests and are not optional: the component tests and the regression suite.
| Layer | Question it answers | Where |
|---|---|---|
| Unit and scenario tests | Does this behaviour do what the contract says? Written first, with the change. | src/**/*.test.ts, scenarios/ |
| Schema tests | Do the two schemas accept and refuse what they should, and do they still agree? Every fixture under fixtures/ against the served payload schema, every fixture under fixtures/settings/ against the settings schema, and the link between the two: a valid fixture has a settings twin, the same file without the response part. |
src/contract/schema.test.ts, src/contract/settings-schema.test.ts |
| Component tests | Can a visitor use the whole widget, with a pointer and with the keyboard? | tests/component/ |
| Regression suite | Did something that already worked change without anyone meaning it to? | tests/regression/ |
| Package regression | Does the package that is published behave like the source? | tests/package/ |
A new behaviour is not finished with its first test. It also needs its component test when a visitor can reach it, and the golden files of the regression suite updated when it changes the markup, the look or the public API.
The Recording Runtime
Section titled “The Recording Runtime”A widget talks only to a WidgetRuntime, so a test gives it one that writes down every call.
import { createFakeRuntime, renderRecommendation } from '@segmentify/widgets';
const runtime = createFakeRuntime({ context: { device: 'mobile' } });await renderRecommendation(config, runtime, document);
runtime.showAll(); // the widget scrolls into viewconsole.log(runtime.calls); // every interaction, basket call and errorIn “data only” delivery runtime.deliveries holds every delivered handle, so a test can act as the site: runtime.deliveries[0].observeView(element).
| Option | Meaning |
|---|---|
context |
Parts of the context: device, pageType, language, currency, region |
capabilities |
addToBasket, favorites |
basketResult, favoriteResult |
What the runtime answers |
extensions |
Registered extensions, by id |
hold |
Keep basket and favourite answers pending until runtime.answer() is called |
Two Environments
Section titled “Two Environments”Every widget test file runs twice. A third project, node, tests the repository’s own tooling in scripts/.
| Project | Environment | Good for | Cannot do |
|---|---|---|---|
dom |
happy-dom | Speed; events and rules | Layout, scrolling, real style rules |
chrome |
The Chrome installed on the machine, through Playwright | Everything | Speed |
A simulated DOM has no layout: every element is zero pixels wide. A test that passes there says nothing about whether four cards really fit in a row. That is why behaviour must also pass in a real browser.
In the chrome project a resize step really resizes the container and the real ResizeObserver answers. In the dom project a stand-in is notified.
Browser-only Tests
Section titled “Browser-only Tests”Files named *.browser.test.ts run in the chrome project only. Today they check:
| Test | What it proves |
|---|---|
| Card geometry at 1200px and 480px | The column count and gap in the config become real widths. |
| Resize | The column count changes when the container changes. |
| Arrow scrolling | An arrow moves the list by exactly one step and back. |
| Style isolation | Page rules for a, span, img, button do not reach inside the widget. |
| Host box and inheritance | The page can size the widget and its font is inherited. This is by design. |
| Hostile values | <img onerror=...> in a title, name, brand or price stays text and never runs. |
| Countdown slot | It stands between the header and the cards, takes its colours from its tokens, and keeps its width from one second to the next. |
| Badges | A long badge is one line and cut, clear of the favourite button, and inside the text column of a compact card. |
Firefox and WebKit
Section titled “Firefox and WebKit”Continuous integration also runs every test in Firefox and in WebKit, the engine of Safari, with Playwright’s own builds of them.
pnpm exec playwright install firefox webkit # oncepnpm test:browsers| Project | Browser | Runs |
|---|---|---|
chrome |
The Chrome installed on the machine | Locally and in continuous integration |
firefox |
Playwright’s Firefox | In continuous integration; locally after the install above |
webkit |
Playwright’s WebKit | In continuous integration; locally after the install above |
When a test fails in one of them, the workflow lists the failed tests in a comment on the pull request.
Coverage is measured in Chrome only.
Component Tests
Section titled “Component Tests”A component test mounts the widget through the public entry of the library and uses it as a visitor does, in a real browser, with real clicks and key presses from the browser, not with events made in the test. It asserts what a visitor perceives (what is shown, what has the focus, what a screen reader is told) and what the runtime was asked to do.
| File | What it covers |
|---|---|
fixtures.browser.test.ts |
Every valid fixture on desktop (1200px) and mobile (480px): the title and a card per product are shown, nothing is reported, every link and button has a name, every image has its text, the keyboard reaches every product, the widget stays inside its container. A new fixture is covered without a new test. |
keyboard.browser.test.ts |
Tab and Shift+Tab order, Enter on a product, Enter and Space on add to basket, the focus after an answer, the favourite and aria-pressed, the slider arrows. |
pointer.browser.test.ts |
Clicks on a product and on the buttons, a second click while the site has not answered, a failed basket and a failed favourite, the arrows forward and back, widget-view once, nothing sent after the widget left the page. |
semantics.browser.test.ts |
Every valid fixture on desktop and mobile: the widget is a region named by its title, the title is a heading of the configured level, the products are one list with an item each, an old price is removed text, an old price, a discount and a rating are named for screen readers, the arrows name their list, and an automated accessibility scan (axe-core: WCAG 2.1 A and AA, best practices) finds nothing. A new fixture is covered without a new test. A new component is not finished without the same test for its fixtures. |
account-card.browser.test.ts |
The same with an account product card: every fixture on desktop and mobile, the layout columns, clicks and keys on the parts the widget handed to the card, the Tab order inside a card, and a card that fails. |
pnpm test:componentThey run in Chrome with pnpm check, and in Firefox and WebKit in continuous integration.
The Regression Suite
Section titled “The Regression Suite”The regression suite compares the widget with golden files committed in tests/regression/golden/. It fails when something changed that nobody meant to change.
| Part | Golden file | Fails when |
|---|---|---|
| Markup | markup/<fixture>.<device>.txt |
An element, an attribute, a text or the order of the widget’s markup changes, for any valid fixture on desktop or mobile. Integration, the mobile renderers and account cards hang on this markup. |
| Default look | look/<fixture>.<device>.json |
A colour, size, weight, radius, padding or decoration of any kind of part changes. Chrome only. Sizes that depend on the machine’s fonts are not recorded. |
| Markup around an account card | account-card/<fixture>.<device>.txt |
The wrapper of an account product card or the names of its parts change. Integration writes account cards and their styles against them. |
| Public API | api.json |
A function or a constant of the package is added, removed or changed: error codes, interaction types, theme tokens and defaults. |
| Fixed bugs | bugs/SFY-… |
A bug that was fixed comes back. One test per bug; see tests/regression/bugs/README.md. |
pnpm test:regression # comparepnpm test:update-golden # a change is meant: write the golden files againWhen a change is meant, the golden files change in the same pull request, and their diff is part of the review: it shows exactly what the change does to the page. Continuous integration never writes a golden file; a missing or different one fails.
The Built Package
Section titled “The Built Package”pnpm test:package builds dist and runs the markup and API comparison again, against the built package and the same golden files, plus one flow from impression to add to basket. It catches what only the build can break: a file left out, an export lost.
What Is Not Tested Yet
Section titled “What Is Not Tested Yet”- Safari itself, on macOS and iOS.
- The look of a part in a state other than at rest (hover, focus ring, disabled, pressed), and of any but the first part of each kind. The look fingerprint records the first one, at rest.
- Golden files of a fixture that was deleted are not noticed; delete them with the fixture.
- Pixel screenshots. Text is drawn with the fonts of the machine, so a screenshot from one machine does not match another. The look fingerprint covers what the library’s own styles set.
- The installed npm package in a consumer’s bundler. The package regression imports
distdirectly. - Component tests with a real screen reader. Names, roles and states are checked; how they are announced is not.
Continuous Integration
Section titled “Continuous Integration”A pull request to development runs these on the organization’s own runner, job-runner-02. Every job runs in the Playwright container image, so the runner needs Docker and nothing else, and no minute of a GitHub-hosted runner is used. The image has no Chrome: the chrome project runs on the Chromium of the image there (SGF_CHROME_CHANNEL=chromium), and on the installed Chrome on a developer’s machine.
One runner takes the jobs one after another, so the three jobs of a pull request queue behind each other and behind the jobs of other repositories. Each job has a time limit.
The release is the exception and runs on a GitHub-hosted runner; see Releasing.
| Step | Fails when |
|---|---|
| Schema check | The committed JSON Schema differs from the contract |
| Type check and lint | A type error or a banned construct |
| Tests with coverage | A unit, scenario, component or regression test fails in happy-dom or Chrome, a golden file differs, or coverage is below the floor |
| Test-first check | A feat or fix has no test, or its tests pass without it |
| Firefox and WebKit | A test fails in one of them |
| Contract check | CONTRACT.md is behind the contract types, the scenarios or the fixtures |
| Build | The package does not compile |
| Package regression | The built package differs from the golden files |
The test-first check is described in Development.
Next Steps
Section titled “Next Steps”- Conformance Scenarios describes the scenario files the runner reads.
- Releasing describes what runs after a merge.