-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathtest-stories.ts
More file actions
84 lines (69 loc) · 1.99 KB
/
test-stories.ts
File metadata and controls
84 lines (69 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import * as path from 'node:path';
import * as fs from 'node:fs/promises';
import { pathToFileURL } from 'node:url';
import type { EvaluationSummary, ExperimentArgs } from '../../types';
import type { JsonTestResults } from 'vitest/reporters';
import { x } from 'tinyexec';
import { dedent } from 'ts-dedent';
export async function testStories({
projectPath,
resultsPath,
}: ExperimentArgs): Promise<Pick<EvaluationSummary, 'test' | 'a11y'>> {
const testResultsPath = path.join(resultsPath, 'tests.json');
const result = await x('pnpm', ['eval:test'], {
nodeOptions: {
cwd: projectPath,
},
});
await fs.writeFile(
path.join(resultsPath, 'tests.md'),
dedent`# Test Results
**Exit Code:** ${result.exitCode}
## stdout
\`\`\`sh
${result.stdout}
\`\`\`
## stderr
\`\`\`
${result.stderr}
\`\`\`
`,
);
const { default: jsonTestResults } = (await import(
pathToFileURL(testResultsPath).href,
{
with: { type: 'json' },
}
)) as { default: JsonTestResults };
// write the file again to pretty-print it
await fs.writeFile(testResultsPath, JSON.stringify(jsonTestResults, null, 2));
// Extract a11y violations per story
const a11yViolations: Record<string, any[]> = {};
for (const jsonTestResult of Object.values(jsonTestResults.testResults)) {
for (const assertionResult of jsonTestResult.assertionResults ?? []) {
const storyId = (assertionResult.meta as any)?.storyId;
if (!storyId) {
continue;
}
for (const report of (assertionResult.meta as any).reports ?? []) {
if (report.type === 'a11y' && report.result?.violations?.length > 0) {
a11yViolations[storyId] = report.result.violations;
}
}
}
}
const a11yViolationsPath = path.join(resultsPath, 'a11y-violations.json');
await fs.writeFile(
a11yViolationsPath,
JSON.stringify(a11yViolations, null, 2),
);
return {
test: {
passed: jsonTestResults.numPassedTests,
failed: jsonTestResults.numFailedTests,
},
a11y: {
violations: Object.keys(a11yViolations).length,
},
};
}