Write model-based tests with @xstate/test 2.0 beta, replace end-to-end tests with model-based tests without losing coverage, and visualize the model in Stately. Use for model-based testing, @xstate/test, generating tests from a state machine, testing every path, replacing Playwright, Cypress, Vitest, or Jest end-to-end tests with a model, coverage parity, or migrating from @xstate/test 0.x or 1.0 beta.
100%
Does it follow best practices?
Run evals on this skill
Adds up to 20 points to the overall score
Medium
Suggest reviewing before use
import assert from 'node:assert/strict';
import { spawnSync } from 'node:child_process';
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { test } from 'node:test';
const script = fileURLToPath(new URL('./parity-check.mjs', import.meta.url));
const id = 'flow.spec.ts > adds an item';
const entry = {
id, projects: ['chromium', 'firefox'], status: 'mapped', events: [{ type: 'ADD' }],
assertions: [{ legacy: 'expect(count).toBe(1)', afterStep: 1, oracle: 'projectModel.count' }]
};
const inventory = {
suites: [{ title: 'flow.spec.ts', file: 'flow.spec.ts', specs: [{
title: 'adds an item', tests: entry.projects.map((projectName) => ({ projectName }))
}] }]
};
const records = entry.projects.flatMap((project) => [
{ id, project, kind: 'replay', outcome: 'passed', events: entry.events },
{ id, project, kind: 'control', outcome: 'diverged', events: entry.events, assertion: 0, afterStep: 1, oracle: 'projectModel.count' }
]);
function check({ baseline = inventory, evidence = records, init = false, ledgerEntry = entry } = {}) {
const dir = mkdtempSync(join(tmpdir(), 'mbt-parity-'));
try {
for (const [name, data] of Object.entries({
baseline, evidence, ledger: { version: 2, tests: [ledgerEntry] }
})) {
writeFileSync(join(dir, name), JSON.stringify(data));
}
return spawnSync(process.execPath, [
script, '--inventory', 'baseline',
...(init ? ['--init'] : ['--ledger', 'ledger', '--evidence', 'evidence'])
], { encoding: 'utf8', cwd: dir });
} finally {
rmSync(dir, { force: true, recursive: true });
}
}
test('records every project in the original inventory', () => {
const result = check({ init: true });
assert.equal(result.status, 0, result.stderr);
assert.deepEqual(JSON.parse(result.stdout).tests[0].projects, entry.projects);
});
test('rejects a filtered baseline and missing project evidence', () => {
const baseline = structuredClone(inventory);
baseline.suites[0].specs[0].tests.pop();
const filtered = check({ baseline });
assert.equal(filtered.status, 1, filtered.stdout);
assert.match(filtered.stdout, /baseline.*firefox/);
const missing = check({ evidence: records.filter(({ project }) => project !== 'firefox') });
assert.equal(missing.status, 1, missing.stdout);
assert.match(missing.stdout, /firefox.*missing.*replay/);
});
test('requires a passing replay of the ledger events and divergence at the recorded step and oracle', () => {
const evidence = structuredClone(records);
evidence[0].events[0].type = 'TYPO';
evidence[1].afterStep = 0;
evidence[3].oracle = 'projectModel.other';
const result = check({ evidence });
assert.equal(result.status, 1, result.stdout);
assert.match(result.stdout, /replay.*events/);
assert.match(result.stdout, /control.*step.*oracle/);
assert.equal(check().status, 0);
});
test('counts only attachments from passing Playwright cases', () => {
const report = {
suites: [{ specs: [{ tests: entry.projects.map((projectName) => ({
projectName, expectedStatus: 'passed', status: 'expected',
results: [{ status: 'passed', attachments: records.filter(r => r.project === projectName).map(r => ({
name: 'mbt-parity', body: Buffer.from(JSON.stringify(r)).toString('base64')
})) }]
})) }] }]
};
assert.equal(check({ evidence: report }).status, 0);
report.suites[0].specs[0].tests[1].results[0].status = 'failed';
const failed = check({ evidence: report });
assert.equal(failed.status, 1, failed.stdout);
assert.match(failed.stdout, /firefox.*missing.*replay/);
});
test('requires concrete Vitest cases and accepts their execution report', () => {
const file = 'publish.test.ts';
const unresolved = check({
init: true,
baseline: [{ file, name: 'Publish > $name > rejects duplicates', location: { line: 1 } }]
});
assert.equal(unresolved.status, 2);
assert.match(unresolved.stderr, /unexpanded runner title/);
assert.equal(check({
init: true,
baseline: [{ file, name: 'Publish with %s', location: { line: 1 } }]
}).status, 2);
const expanded = check({
init: true,
baseline: { testResults: [{ name: file, assertionResults: ['npm 10', 'pnpm 12'].map((name) => ({
ancestorTitles: ['Publish', name], title: 'rejects duplicates', projectName: 'e2e'
})) }] }
});
assert.equal(expanded.status, 0, expanded.stderr);
assert.deepEqual(JSON.parse(expanded.stdout).tests.map(({ id, projects }) => [id, projects]), [
['publish.test.ts > Publish > npm 10 > rejects duplicates', ['e2e']],
['publish.test.ts > Publish > pnpm 12 > rejects duplicates', ['e2e']]
]);
});
test('normalizes an unnamed Playwright project in the inventory and evidence', () => {
const baseline = structuredClone(inventory);
baseline.suites[0].specs[0].tests = [{ projectName: '' }];
const initialized = check({ baseline, init: true });
assert.deepEqual(JSON.parse(initialized.stdout).tests[0].projects, [null]);
const evidence = records.filter((row) => row.project === 'chromium').map((row) => ({ ...row, project: '' }));
const result = check({ baseline, evidence, ledgerEntry: { ...entry, projects: [null] } });
assert.equal(result.status, 0, result.stdout);
});
test('rejects control evidence recorded with a different event sequence', () => {
const evidence = records.map((row) => row.kind === 'control' ? { ...row, events: [{ type: 'OLD' }] } : row);
const result = check({ evidence });
assert.equal(result.status, 1, result.stdout);
assert.match(result.stdout, /control.*ledger events/);
});