Development
How to set up the repository, the rules the code follows and how a change is made.
This page explains how to work on the library: set-up, layout, the rules enforced by tooling and the order in which a change is made.
Set-up
Section titled “Set-up”You need Node.js 20 or newer, pnpm and Google Chrome.
pnpm installpnpm checkpnpm check runs the type check, the linter and every test with the coverage floor.
| Command | What it does |
|---|---|
pnpm check |
Type check, lint, all tests, coverage floor |
pnpm tdd:check |
The test-first check on your commits |
pnpm test:dom |
Tests in happy-dom only; fast |
pnpm test:browser |
Tests in real Chrome |
pnpm contract |
Rewrites the generated parts of CONTRACT.md from the code |
pnpm contract:check |
Fails when CONTRACT.md is behind the code |
pnpm test:component |
The component tests in Chrome; see Testing |
pnpm test:regression |
The regression suite against the golden files |
pnpm test:update-golden |
Writes the golden files again, when a change is meant |
pnpm test:package |
Builds, then runs the regression against dist |
pnpm test:browsers |
Tests in Firefox and WebKit; see Testing |
pnpm build |
Compiles to dist/ |
pnpm docs:dev |
Runs this documentation site locally |
Layout
Section titled “Layout”| Path | What it holds |
|---|---|
src/contract/ |
The runtime interface and the config schema |
src/recommendation/ |
The recommendation widget for the web |
src/testing/ |
The recording runtime and the scenario format |
scenarios/ |
Conformance scenarios |
CONTRACT.md |
The written contract in one file; its code parts are generated and checked |
tests/component/ |
Component tests: the whole widget, as a visitor uses it |
tests/regression/ |
The regression suite and its golden files |
tests/package/ |
The regression against the built package |
fixtures/ |
Example configs |
scripts/ |
Repository tooling, such as the test-first check |
docs/ |
This documentation site |
Rules Enforced by the Linter
Section titled “Rules Enforced by the Linter”| Banned | Use instead |
|---|---|
_SgmntfY_, window._SgmntfY_ |
The WidgetRuntime |
jQuery, segJquery |
The DOM API |
| Mustache or any template engine | createElement and textContent |
innerHTML, outerHTML, insertAdjacentHTML |
createElement and textContent |
eval, new Function |
Nothing; there is no dynamic code |
Importing @segmentify/integration |
The WidgetRuntime |
The package has no runtime dependencies. Adding one needs a reason that a few lines of code cannot satisfy.
How a Change Is Made
Section titled “How a Change Is Made”Every change is made test first. This is a rule that continuous integration checks, not a habit.
- Red. Write the test that describes the behaviour and watch it fail.
- Green. Write the smallest change that makes it pass.
- Refactor. Clean up while the tests stay green.
- Check. Run
pnpm check. It runs everything in Chrome too. - Prove it. Run
pnpm tdd:check. - Open a pull request to
development. Its title follows Conventional Commits, because the title decides the next version. See Releasing. - Review it. After the pull request is opened, its author reviews the whole diff and fixes what the review finds. See Reviewing Your Pull Request.
A bug fix starts with a test that reproduces the bug.
Where the First Test Goes
Section titled “Where the First Test Goes”| The change is about | Write first |
|---|---|
| Widget behaviour: what is drawn, what is sent | A JSON scenario. See Conformance Scenarios. |
| The contract or the schema | A fixture in fixtures/valid/ or fixtures/invalid/ |
| Layout, scrolling, real styles | A *.browser.test.ts file |
Repository tooling in scripts/ |
A *.test.mjs file next to the script |
| A bug | A test in tests/regression/bugs/, named after the issue |
The first test is not the last. Before the pull request is opened:
| The change | Also needs |
|---|---|
| Adds or changes something a visitor can reach | A component test in tests/component/ |
| Changes the markup, the default look or the public API | The golden files written again (pnpm test:update-golden) and read in the diff |
| Adds a fixture | Its golden files; the component tests cover it by themselves |
The Test-first Check
Section titled “The Test-first Check”A test written after the code can pass without proving anything. The check removes that doubt.
For a feat or fix, the check takes the behaviour change out of a copy of the checkout, keeps the tests of the change and runs them. At least one test must fail.
| Result | Meaning |
|---|---|
| Some tests fail without the change | The tests prove the change. The check passes and lists them. |
| Every test passes without the change | The tests do not prove the change. The check fails. |
| Behaviour changed and no test changed | The check fails. |
| Kind of file | Paths |
|---|---|
| Behaviour | src/ except tests and src/testing/; schema/; scripts/ |
| Test | *.test.ts, *.test.mjs, src/testing/, scenarios/, fixtures/, tests/ |
Other commit types, such as refactor, docs and chore, are not asked to prove anything. A refactor does not change behaviour, so the suite staying green is its proof.
pnpm tdd:check # against origin/developmentpnpm tdd:check -- --project dom # faster; happy-dom onlyReviewing Your Pull Request
Section titled “Reviewing Your Pull Request”The tests prove what you thought of. The review looks for what you did not think of. It is done by the author, after the pull request is opened and before anyone is asked to merge it.
How to do it
Section titled “How to do it”- Read the whole diff on the pull request page, top to bottom, as if someone else had written it. Not the files in your editor: the diff shows what actually changes, including what you forgot you touched.
- Ask the questions below of every changed file.
- Write each finding down before fixing anything, so that none is lost.
- Fix each finding test first, on the same branch: a test or fixture that shows the problem, seen failing, then the fix. A finding in the documentation alone needs no test.
- Run
pnpm checkandpnpm tdd:checkagain and push. - List the findings in the pull request, under “Review after opening”, with what was done about each. Write “nothing found” when that is the result.
A pull request is not merged before this is done.
What to look for
Section titled “What to look for”| Question | Example of a finding |
|---|---|
| Does every value that reaches the page stay inside its place? | A font family with an unclosed quote swallowed the theme tokens written after it. |
| Does the documentation say what the code does? | A page said a token colours card text only; it also coloured the title. |
| What input did the tests not try? | Empty, very long, wrong type, the edge of a limit, a second call. |
| Does anything behave differently for existing configs? | A stricter pattern refuses a value that was accepted before. Say so in the pull request. |
| Is anything in the diff that the issue did not ask for? | A process change inside a theming pull request. Take it out, or name it in the pull request. |
| Are the schema, the fixtures and the widget still saying the same? | A rule added to the widget but not to the schema. |
| Is a name, comment or message now wrong? | A comment that still describes the previous design. |
What it is not
Section titled “What it is not”- It does not replace the review by another person, where there is one.
- It is not checked by continuous integration. The pull request template has a box for it; ticking the box without reading the diff defeats it.
The Coverage Floor
Section titled “The Coverage Floor”pnpm check fails when coverage drops below the thresholds in vitest.config.ts.
The thresholds are a floor, not a goal. They only move up: when coverage rises, the same pull request raises them.
Language
Section titled “Language”Code, comments, commit messages, scenarios and these pages are written in English.