Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

test(a11y): run the audit CLI fixtures on an OS-assigned port (#7765)

Christopher Chedeaue4e0c6015e6b
diff --git a/.github/scripts/accessibility-audit.integration.test.mjs b/.github/scripts/accessibility-audit.integration.test.mjs
index 5ad43bdc3c..bcaf979b66 100644
--- a/.github/scripts/accessibility-audit.integration.test.mjs
+++ b/.github/scripts/accessibility-audit.integration.test.mjs
@@ -16,7 +16,6 @@ import {describe, expect, it} from 'vitest';
const SCRIPTS_DIR = path.dirname(fileURLToPath(import.meta.url));
const REPO_ROOT = path.resolve(SCRIPTS_DIR, '../..');
const SCRIPT = path.join(SCRIPTS_DIR, 'accessibility-audit.js');
-let nextPort = 40000 + (process.pid % 10000);
const BROWSER_MOCK = String.raw`
const Module = require('node:module');
@@ -68,7 +67,15 @@ function evaluateInFixture(fn, arg) {
}
const page = {
- goto: async url => { requestedUrl = url; },
+ // A real request, so a run only passes when the audit opens the port its
+ // server actually holds.
+ goto: async url => {
+ requestedUrl = url;
+ const response = await fetch(url);
+ if (!response.ok) {
+ throw new Error('fixture server answered ' + response.status + ' for ' + url);
+ }
+ },
addStyleTag: async () => {},
waitForSelector: async (selector, options) => {
if (process.env.A11Y_TEST_SCENARIO === 'selector-missing') {
@@ -123,7 +130,6 @@ function runFixture(
fs.writeFileSync(path.join(storybook, 'index.json'), indexContent);
fs.writeFileSync(path.join(storybook, 'iframe.html'), '<!doctype html>');
fs.writeFileSync(mock, BROWSER_MOCK);
- const port = nextPort++;
try {
const result = spawnSync(
process.execPath,
@@ -135,8 +141,10 @@ function runFixture(
output,
'--components',
components,
+ // Any fixed port may already be held by another process's socket, so
+ // let the OS assign a free one.
'--port',
- String(port),
+ '0',
...(readySelector == null
? []
: ['--ready-selector', readySelector]),
@@ -203,6 +211,16 @@ const PATTERN_INDEX = JSON.stringify({
});
describe.sequential('accessibility-audit CLI readiness', () => {
+ it('serves the stories on a port the OS assigns', () => {
+ const result = runFixture('delayed', VALID_INDEX);
+ expect(result.status).toBe(0);
+ const served = /Storybook server running on http:\/\/localhost:(\d+)/.exec(
+ result.stdout,
+ );
+ expect(Number(served?.[1])).toBeGreaterThan(0);
+ expect(result.report.auditedStoryKeys).toHaveLength(1);
+ });
+
it('waits through a delayed valid Storybook root and writes audited evidence', () => {
const result = runFixture('delayed', VALID_INDEX);
expect(result.status).toBe(0);
diff --git a/.github/scripts/accessibility-audit.js b/.github/scripts/accessibility-audit.js
index 8f1ebb181b..495ad0586c 100644
--- a/.github/scripts/accessibility-audit.js
+++ b/.github/scripts/accessibility-audit.js
@@ -5,7 +5,7 @@
/**
* @description Runs accessibility audits on component stories using axe-core
* @input --storybook-dir <path> --output <file> [--components <comma-separated>]
- * [--stories <comma-separated-story-ids>] [--port <number>]
+ * [--stories <comma-separated-story-ids>] [--port <number, 0 = any free>]
* [--ready-selector <css-selector>] [--concurrency <pages, default 1>]
* [--shard <i>/<n> --shard-output <file>] (scan every n-th story, write it)
* [--merge-shards <dir>] (read a complete shard set instead of scanning)
@@ -61,7 +61,7 @@ const hasFlag = (name) => args.includes(`--${name}`);
const storybookDir = getArg('storybook-dir') || 'apps/storybook/dist';
const outputFile = getArg('output') || 'a11y-report.json';
const port = Number(getArg('port') || 6007);
-if (!Number.isInteger(port) || port < 1 || port > 65535) {
+if (!Number.isInteger(port) || port < 0 || port > 65535) {
throw new Error(`Invalid --port value: ${getArg('port')}`);
}
const componentsArg = getArg('components');
@@ -193,7 +193,9 @@ function createServer(dir, port) {
});
server.listen(port, () => {
- console.log(`Storybook server running on http://localhost:${port}`);
+ console.log(
+ `Storybook server running on http://localhost:${server.address().port}`,
+ );
resolve(server);
});
});
@@ -369,6 +371,8 @@ async function runAccessibilityAudit() {
// A merge reads results; it starts no server and no browser.
const scanning = mergeShardsDir === null;
const server = scanning ? await createServer(storybookPath, port) : null;
+ // The port the server actually holds: `--port 0` lets the OS pick a free one.
+ const origin = server ? `http://localhost:${server.address().port}` : null;
const browser = scanning ? await chromium.launch() : null;
const componentResults = {};
const auditedStoryIds = new Set();
@@ -386,7 +390,7 @@ async function runAccessibilityAudit() {
const scanStory = async (component, story) => {
const page = await context.newPage();
try {
- const url = `http://localhost:${port}/iframe.html?id=${story.id}&viewMode=story`;
+ const url = `${origin}/iframe.html?id=${story.id}&viewMode=story`;
// Higher timeout to accommodate axe-core's heavier DOM analysis
await page.goto(url, {
waitUntil: 'domcontentloaded',
@@ -416,7 +420,7 @@ async function runAccessibilityAudit() {
(root.textContent ?? '').trim().length > 0),
};
},
- {expectedOrigin: `http://localhost:${port}`, storyId: story.id},
+ {expectedOrigin: origin, storyId: story.id},
),
{timeoutMs: 5000, pollMs: 50},
);