@e2e-dev/html-reporter 0.0.0-stage → 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,8 @@
1
+ /**
2
+ * `@e2e-dev/html-reporter` public surface: the `htmlReporter()` factory. The
3
+ * reporter writes one self-contained HTML page per run through the
4
+ * `reporters` seam of `e2e`; the page itself is the app under `app/`, built
5
+ * into a single file at package build time.
6
+ */
7
+ export { htmlReporter } from './reporter.ts';
8
+ export type { HtmlReporterOptions } from './reporter.ts';
package/dist/index.js ADDED
@@ -0,0 +1,7 @@
1
+ /**
2
+ * `@e2e-dev/html-reporter` public surface: the `htmlReporter()` factory. The
3
+ * reporter writes one self-contained HTML page per run through the
4
+ * `reporters` seam of `e2e`; the page itself is the app under `app/`, built
5
+ * into a single file at package build time.
6
+ */
7
+ export { htmlReporter } from './reporter.js';
@@ -0,0 +1,51 @@
1
+ /**
2
+ * What the reporter hands the page, and how it rides inside it. The page is
3
+ * one prebuilt HTML file with a placeholder JSON script; the reporter swaps
4
+ * the run's data in, so the result opens from disk with no server and no
5
+ * fetch. The app (`app/`) imports these types too, so both sides agree.
6
+ */
7
+ import type { Report } from 'e2e';
8
+ /**
9
+ * How the page reaches the run's screenshots, videos, and files:
10
+ * - `link`: by relative path from the page, under `base` (a POSIX path,
11
+ * `results` when the page sits beside `.e2e/results/`).
12
+ * - `embed`: from `files`, each artifact path's bytes as a `data:` URL, so
13
+ * the page is the whole report.
14
+ * - `none`: not at all; the page shows text only.
15
+ */
16
+ export type ReportMedia = {
17
+ readonly mode: 'link';
18
+ readonly base: string;
19
+ } | {
20
+ readonly mode: 'embed';
21
+ readonly files: Readonly<Record<string, string>>;
22
+ } | {
23
+ readonly mode: 'none';
24
+ };
25
+ /** The data one report page renders. */
26
+ export interface ReportPayload {
27
+ /** The run's report-1 document, unchanged. */
28
+ readonly report: Report;
29
+ readonly media: ReportMedia;
30
+ /**
31
+ * The run's results directory from the project root, a POSIX path
32
+ * (`.e2e/results`). The page names files there in the prompt it copies for
33
+ * a coding agent, which reads them from the project.
34
+ */
35
+ readonly resultsDir: string;
36
+ /** Each kept `trace.md` by report result id, as a path from the project root. */
37
+ readonly traces: Readonly<Record<string, string>>;
38
+ /**
39
+ * Lines of the project's test files around each step and error, by file
40
+ * (as the report names it) then line number, for the page's code frames.
41
+ */
42
+ readonly sources: Readonly<Record<string, Readonly<Record<string, string>>>>;
43
+ }
44
+ /** The text the built page holds in place of the payload, until a reporter fills it. */
45
+ export declare const PAYLOAD_PLACEHOLDER = "\"__E2E_REPORT_PAYLOAD__\"";
46
+ /**
47
+ * The page with the payload in place of the placeholder. `<` is escaped
48
+ * inside the JSON so no value (a test title, an error message) can close the
49
+ * script element or open a comment; `JSON.parse` reads `<` back as `<`.
50
+ */
51
+ export declare function embedPayload(template: string, payload: ReportPayload): string;
@@ -0,0 +1,20 @@
1
+ /**
2
+ * What the reporter hands the page, and how it rides inside it. The page is
3
+ * one prebuilt HTML file with a placeholder JSON script; the reporter swaps
4
+ * the run's data in, so the result opens from disk with no server and no
5
+ * fetch. The app (`app/`) imports these types too, so both sides agree.
6
+ */
7
+ /** The text the built page holds in place of the payload, until a reporter fills it. */
8
+ export const PAYLOAD_PLACEHOLDER = '"__E2E_REPORT_PAYLOAD__"';
9
+ /**
10
+ * The page with the payload in place of the placeholder. `<` is escaped
11
+ * inside the JSON so no value (a test title, an error message) can close the
12
+ * script element or open a comment; `JSON.parse` reads `<` back as `<`.
13
+ */
14
+ export function embedPayload(template, payload) {
15
+ const [head, tail, ...rest] = template.split(PAYLOAD_PLACEHOLDER);
16
+ if (head === undefined || tail === undefined || rest.length > 0) {
17
+ throw new Error('the report page template must hold the payload placeholder exactly once');
18
+ }
19
+ return head + JSON.stringify(payload).replaceAll('<', '\\u003c') + tail;
20
+ }
@@ -0,0 +1,38 @@
1
+ import type { FinishedRun, Reporter, ReporterSummary } from 'e2e';
2
+ export interface HtmlReporterOptions {
3
+ /**
4
+ * Where the page goes, from the project root. Defaults to `report.html` in
5
+ * the run's output directory (`.e2e/report.html`), beside the `results/`
6
+ * directory its screenshots and videos link into.
7
+ */
8
+ outputFile?: string;
9
+ /**
10
+ * What the page does with screenshots, videos, and other files:
11
+ * - `'link'` (default): links them from the results directory, so keep the
12
+ * output directory together when you move or upload it.
13
+ * - `'embed'`: copies them into the page, so the one file is the whole
14
+ * report. Device videos make it large: tens of megabytes per minute.
15
+ * - `'none'`: leaves them out; the page shows text only.
16
+ */
17
+ media?: 'link' | 'embed' | 'none';
18
+ }
19
+ /**
20
+ * The HTML reporter. Add it to `reporters` beside the built-in ids:
21
+ *
22
+ * ```ts
23
+ * import { htmlReporter } from '@e2e-dev/html-reporter';
24
+ * export default { targets, reporters: ['list', htmlReporter()] } satisfies E2EConfig;
25
+ * ```
26
+ *
27
+ * Once the run is over it writes one page with the run's report-1 document
28
+ * in it, which opens straight from disk. `media` decides whether
29
+ * screenshots and videos are linked from the results directory (the
30
+ * default), embedded in the page, or left out.
31
+ */
32
+ export declare function htmlReporter(options?: HtmlReporterOptions): Reporter;
33
+ /**
34
+ * Fills the page template with one finished run, writes it, and resolves with
35
+ * the row naming where it went. Once `signal` aborts (the runner gave up on
36
+ * its reporters), it stops reading files and writes nothing.
37
+ */
38
+ export declare function writeReport(run: FinishedRun, options: HtmlReporterOptions, template: string, signal?: AbortSignal): Promise<ReporterSummary>;
@@ -0,0 +1,160 @@
1
+ import { mkdir, readFile, writeFile } from 'node:fs/promises';
2
+ import path from 'node:path';
3
+ import { fileURLToPath } from 'node:url';
4
+ import { embedPayload } from './payload.js';
5
+ /** The built page, filled per run; `vite build` writes it beside this module. */
6
+ const TEMPLATE_PATH = path.join(path.dirname(fileURLToPath(import.meta.url)), 'app', 'index.html');
7
+ /**
8
+ * The HTML reporter. Add it to `reporters` beside the built-in ids:
9
+ *
10
+ * ```ts
11
+ * import { htmlReporter } from '@e2e-dev/html-reporter';
12
+ * export default { targets, reporters: ['list', htmlReporter()] } satisfies E2EConfig;
13
+ * ```
14
+ *
15
+ * Once the run is over it writes one page with the run's report-1 document
16
+ * in it, which opens straight from disk. `media` decides whether
17
+ * screenshots and videos are linked from the results directory (the
18
+ * default), embedded in the page, or left out.
19
+ */
20
+ export function htmlReporter(options = {}) {
21
+ return {
22
+ name: 'html',
23
+ onRunFinished: async (run, signal) => writeReport(run, options, await readFile(TEMPLATE_PATH, 'utf8'), signal),
24
+ };
25
+ }
26
+ /**
27
+ * Fills the page template with one finished run, writes it, and resolves with
28
+ * the row naming where it went. Once `signal` aborts (the runner gave up on
29
+ * its reporters), it stops reading files and writes nothing.
30
+ */
31
+ export async function writeReport(run, options, template, signal) {
32
+ const outputFile = options.outputFile === undefined
33
+ ? path.join(path.dirname(run.artifactsRoot), 'report.html')
34
+ : path.resolve(run.projectRoot, options.outputFile);
35
+ const mode = options.media ?? 'link';
36
+ const media = await pageMedia(mode, run, outputFile, signal);
37
+ const resultsDir = toPosix(path.relative(run.projectRoot, run.artifactsRoot)) || '.';
38
+ const traces = Object.fromEntries([...run.traces].map(([id, trace]) => [id, toPosix(trace)]));
39
+ const sources = await sourceFrames(run.report, run.projectRoot, run.redact);
40
+ const page = embedPayload(template, { report: run.report, media, resultsDir, traces, sources });
41
+ signal?.throwIfAborted();
42
+ await mkdir(path.dirname(outputFile), { recursive: true });
43
+ await writeFile(outputFile, page, 'utf8');
44
+ const shown = path.relative(process.cwd(), outputFile) || outputFile;
45
+ let note = '';
46
+ if (mode === 'embed')
47
+ note = ` (one file, ${formatSize(Buffer.byteLength(page))})`;
48
+ if (mode === 'none')
49
+ note = ' (text only)';
50
+ return [{ label: 'HTML report', text: `${shown}${note}` }];
51
+ }
52
+ /** What the page holds for screenshots and videos in each `media` mode. */
53
+ async function pageMedia(mode, run, outputFile, signal) {
54
+ switch (mode) {
55
+ case 'link':
56
+ return { mode, base: toPosix(path.relative(path.dirname(outputFile), run.artifactsRoot)) };
57
+ case 'embed':
58
+ return { mode, files: await embedArtifacts(run.report, run.artifactsRoot, signal) };
59
+ case 'none':
60
+ return { mode };
61
+ }
62
+ }
63
+ /**
64
+ * Media types an embedded file keeps. An embedded file opens as a blob URL
65
+ * in the page's own origin, so a type a browser would run (HTML, SVG, a
66
+ * download from the app under test) is stored as `application/octet-stream`,
67
+ * which the browser saves instead of rendering.
68
+ */
69
+ const INERT_TYPE = /^(?:image\/(?:png|jpeg|webp|gif)|video\/(?:mp4|webm|quicktime)|text\/plain|application\/json)$/u;
70
+ /**
71
+ * Every artifact file the report names, read into a `data:` URL keyed by its
72
+ * report path. A path that leaves the results directory is never read, and
73
+ * a file that is gone (deleted by `video: 'retain-on-failure'`, say) is left
74
+ * out, so the page shows it as missing. So is a download the runner could not
75
+ * scan for secrets (`redaction: 'incomplete'`): the page is a file to send.
76
+ */
77
+ async function embedArtifacts(report, artifactsRoot, signal) {
78
+ const { run } = report;
79
+ const attempts = [...run.results.flatMap((result) => result.attempts), ...run.serialGroups.flatMap((group) => group.attempts)];
80
+ // One entry per file: a serial group's attempt and its members name the same artifacts.
81
+ const types = new Map(attempts.flatMap((attempt) => attempt.artifacts.flatMap((artifact) => artifact.path === undefined || (artifact.kind === 'download' && artifact.redaction === 'incomplete') ? [] : [[artifact.path, artifact.mediaType]])));
82
+ const files = {};
83
+ for (const [artifactPath, mediaType] of types) {
84
+ const file = path.resolve(artifactsRoot, artifactPath);
85
+ const inside = path.relative(artifactsRoot, file);
86
+ if (inside === '' || inside.startsWith('..') || path.isAbsolute(inside))
87
+ continue;
88
+ signal?.throwIfAborted();
89
+ const bytes = await readFile(file).catch((error) => {
90
+ if (error instanceof Error && 'code' in error && error.code === 'ENOENT')
91
+ return undefined;
92
+ throw error;
93
+ });
94
+ if (bytes === undefined)
95
+ continue;
96
+ const type = INERT_TYPE.test(mediaType) ? mediaType : 'application/octet-stream';
97
+ files[artifactPath] = `data:${type};base64,${bytes.toString('base64')}`;
98
+ }
99
+ return files;
100
+ }
101
+ /** Lines a code frame shows before and after the line it points at. */
102
+ const FRAME_BEFORE = 2;
103
+ const FRAME_AFTER = 2;
104
+ /** A longer line is cut: a frame shows code, not a minified bundle. */
105
+ const MAX_LINE_CHARS = 400;
106
+ /**
107
+ * The lines around every source location in the report (each step's, each
108
+ * error's, each test's), read from the project's files and redacted, since
109
+ * a test file can hold a secret's value. A file outside the project root, or
110
+ * one that is gone, is left out, and so is its frame.
111
+ */
112
+ async function sourceFrames(report, projectRoot, redact) {
113
+ const wanted = new Map();
114
+ collectSources(report.run, (file, line) => {
115
+ const lines = wanted.get(file) ?? new Set();
116
+ for (let at = Math.max(1, line - FRAME_BEFORE); at <= line + FRAME_AFTER; at++)
117
+ lines.add(at);
118
+ wanted.set(file, lines);
119
+ });
120
+ const frames = {};
121
+ for (const [file, lines] of wanted) {
122
+ const absolute = path.resolve(projectRoot, file);
123
+ const inside = path.relative(projectRoot, absolute);
124
+ if (inside === '' || inside.startsWith('..') || path.isAbsolute(inside))
125
+ continue;
126
+ const text = await readFile(absolute, 'utf8').catch(() => undefined);
127
+ if (text === undefined)
128
+ continue;
129
+ const all = text.split(/\r?\n/u);
130
+ frames[file] = Object.fromEntries([...lines].flatMap((line) => {
131
+ const content = all[line - 1];
132
+ return content === undefined ? [] : [[String(line), redact(content).slice(0, MAX_LINE_CHARS)]];
133
+ }));
134
+ }
135
+ return frames;
136
+ }
137
+ /** Calls `found` for every `{ file, line, column }` source object anywhere under `value`. */
138
+ function collectSources(value, found) {
139
+ if (Array.isArray(value)) {
140
+ for (const item of value)
141
+ collectSources(item, found);
142
+ return;
143
+ }
144
+ if (typeof value !== 'object' || value === null)
145
+ return;
146
+ for (const [key, child] of Object.entries(value)) {
147
+ if (key === 'source' && typeof child === 'object' && child !== null && 'file' in child && 'line' in child && typeof child.file === 'string' && typeof child.line === 'number') {
148
+ found(child.file, child.line);
149
+ }
150
+ else {
151
+ collectSources(child, found);
152
+ }
153
+ }
154
+ }
155
+ function toPosix(relative) {
156
+ return relative.split(path.sep).join('/');
157
+ }
158
+ function formatSize(bytes) {
159
+ return bytes < 1024 * 1024 ? `${Math.ceil(bytes / 1024)} KB` : `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
160
+ }
package/package.json CHANGED
@@ -1,6 +1,74 @@
1
1
  {
2
2
  "name": "@e2e-dev/html-reporter",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.0.0",
4
+ "description": "HTML reporter for e2e: one self-contained page per run, steps, screenshots, and video",
5
+ "keywords": [
6
+ "e2e",
7
+ "end-to-end",
8
+ "testing",
9
+ "test-reporter",
10
+ "html-report"
11
+ ],
12
+ "license": "Apache-2.0",
13
+ "contributors": [
14
+ "Oskar Kwaśniewski <oskar@tester.army>",
15
+ "Szymon Rybczak <szymon@tester.army>"
16
+ ],
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/e2e-inc/e2e.git",
20
+ "directory": "packages/html-reporter"
21
+ },
22
+ "bugs": {
23
+ "url": "https://github.com/e2e-inc/e2e/issues"
24
+ },
25
+ "homepage": "https://github.com/e2e-inc/e2e/tree/main/packages/html-reporter#readme",
26
+ "publishConfig": {
27
+ "registry": "https://registry.npmjs.org/",
28
+ "access": "public",
29
+ "tag": "latest"
30
+ },
31
+ "type": "module",
32
+ "exports": {
33
+ ".": {
34
+ "types": "./dist/index.d.ts",
35
+ "default": "./dist/index.js"
36
+ }
37
+ },
38
+ "files": [
39
+ "dist",
40
+ "NOTICE",
41
+ "app/fonts/*-OFL.txt",
42
+ "app/Lucide-LICENSE.txt"
43
+ ],
44
+ "peerDependencies": {
45
+ "e2e": ">=0.23.1 <1"
46
+ },
47
+ "devDependencies": {
48
+ "@tailwindcss/vite": "4.3.5",
49
+ "@types/node": "26.6.6",
50
+ "@types/react": "19.3.0",
51
+ "@types/react-dom": "19.3.0",
52
+ "@vitejs/plugin-react": "6.3.0",
53
+ "e2e": "0.23.0",
54
+ "lucide-react": "1.72.0",
55
+ "react": "19.3.1",
56
+ "react-dom": "19.3.1",
57
+ "tailwindcss": "4.3.5",
58
+ "typescript": "7.0.2",
59
+ "vite": "8.3.8",
60
+ "vite-plugin-singlefile": "2.3.4",
61
+ "vitest": "5.1.0"
62
+ },
63
+ "engines": {
64
+ "node": "^22.22.3 || >=24.8.0"
65
+ },
66
+ "scripts": {
67
+ "build": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true})\" && vite build && tsc --project tsconfig.build.json && node ../../scripts/stamp-dist.ts",
68
+ "dev": "vite",
69
+ "typecheck": "tsc --noEmit && tsc --noEmit --project app/tsconfig.json",
70
+ "test": "vitest run",
71
+ "test:watch": "vitest",
72
+ "test:unit": "vitest run tests/unit"
73
+ }
6
74
  }