@kinora/vitest-reporter 0.0.0-stage → 0.1.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026-present Joris Gallot <https://github.com/joris-gallot>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,46 @@
1
- # Temporary Holding Version
1
+ # @kinora/vitest-reporter
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Vitest reporter that uploads your browser tests to a [kinora](https://github.com/kinora-dev/kinora) server: pass rates, flaky tests, history across runs, and the Playwright trace of each test. Storybook stories land on kinora's Components page.
4
+
5
+ It uploads the tests of Vitest projects running in a browser (browser mode, Storybook) at the end of the run, then their traces and screenshot comparisons. Upload never fails your test run.
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm i -D @kinora/vitest-reporter
11
+ # or: pnpm add -D @kinora/vitest-reporter
12
+ ```
13
+
14
+ `vitest` (>=4.1) is a peer dependency. Traces need browser mode with the Playwright provider.
15
+
16
+ ## Usage
17
+
18
+ Add it to `vitest.config.ts`, next to the default reporter:
19
+
20
+ ```ts
21
+ import { defineConfig } from 'vitest/config'
22
+
23
+ export default defineConfig({
24
+ test: {
25
+ reporters: ['default', ['@kinora/vitest-reporter', { project: { slug: 'web-app' } }]],
26
+ },
27
+ })
28
+ ```
29
+
30
+ ```bash
31
+ KINORA_TOKEN=<token> npx vitest run
32
+ ```
33
+
34
+ Create an API token in the kinora dashboard (Settings → Workspace). Self-hosting? Point at your server with `KINORA_URL`.
35
+
36
+ ## Node tests
37
+
38
+ Only browser tests are uploaded by default: Node unit tests are usually thousands of fast tests with no trace, and would eat your plan's monthly result quota. Upload them too with `include: 'all'`.
39
+
40
+ ## Documentation
41
+
42
+ **https://docs.kinora.dev/guides/vitest/**
43
+
44
+ ## License
45
+
46
+ MIT
@@ -0,0 +1,37 @@
1
+ import { AttachmentKind, CiMeta, GitMeta } from "@kinora/core";
2
+ import { Reporter, TestModule, Vitest } from "vitest/node";
3
+ //#region src/convert.d.ts
4
+ type IncludeTests = 'browser' | 'all';
5
+ //#endregion
6
+ //#region src/index.d.ts
7
+ interface KinoraVitestReporterOptions {
8
+ /** kinora server base URL. Defaults to env KINORA_URL, then the hosted cloud. Set for self-host. */
9
+ url?: string;
10
+ /** Project API token. Defaults to env KINORA_TOKEN (keep it out of the config file). */
11
+ token?: string;
12
+ /** Target project. `name` defaults to `slug`. */
13
+ project: {
14
+ slug: string;
15
+ name?: string;
16
+ };
17
+ git?: GitMeta;
18
+ ci?: CiMeta;
19
+ /** Which attachment kinds to upload. Defaults to `['trace', 'snapshot']`. */
20
+ uploadAttachments?: AttachmentKind[];
21
+ /**
22
+ * Which tests to upload. `browser` (default) keeps the tests of projects running in a browser
23
+ * (browser mode, Storybook) and leaves Node unit tests out. `all` uploads everything; mind the
24
+ * plan's monthly result quota with large unit suites.
25
+ */
26
+ include?: IncludeTests;
27
+ }
28
+ declare class KinoraVitestReporter implements Reporter {
29
+ private readonly options;
30
+ private startedAt;
31
+ constructor(options: KinoraVitestReporterOptions);
32
+ onInit(vitest: Vitest): void;
33
+ onTestRunStart(): void;
34
+ onTestRunEnd(modules: ReadonlyArray<TestModule>): Promise<void>;
35
+ }
36
+ //#endregion
37
+ export { KinoraVitestReporterOptions, KinoraVitestReporter as default };
package/dist/index.mjs ADDED
@@ -0,0 +1,206 @@
1
+ import { readFile } from "node:fs/promises";
2
+ import process from "node:process";
3
+ import { DEFAULT_KINORA_URL, DEFAULT_UPLOAD_ATTACHMENTS, IngestError, STORY_ANNOTATION, createIngestClient, detectCiEnv, isUploadableAttachment, makeTestKey, stripAnsi } from "@kinora/core";
4
+ //#region src/convert.ts
5
+ function identity(test) {
6
+ const file = test.module.relativeModuleId;
7
+ const suites = [];
8
+ for (let parent = test.parent; parent.type === "suite"; parent = parent.parent) suites.unshift(parent.name);
9
+ return {
10
+ file,
11
+ titlePath: [
12
+ file,
13
+ ...suites,
14
+ test.name
15
+ ],
16
+ projectName: test.project.name
17
+ };
18
+ }
19
+ function statusOf(test) {
20
+ const { state } = test.result();
21
+ if (state === "skipped") return "skipped";
22
+ if (state === "failed") return "unexpected";
23
+ if (state === "passed") return test.diagnostic()?.flaky ? "flaky" : "expected";
24
+ }
25
+ function storyOf(test, titlePath) {
26
+ const meta = test.meta();
27
+ if (!meta.storyId) return void 0;
28
+ if (!meta.componentName) return meta.storyId.replace("--", "/");
29
+ return `${meta.componentName}/${titlePath[1]}`;
30
+ }
31
+ function slug(name) {
32
+ return name.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "") || "screenshot";
33
+ }
34
+ function attachmentsOf(test) {
35
+ const attachments = [];
36
+ let comparisons = 0;
37
+ for (const annotation of test.annotations()) if (annotation.type === "traces" && annotation.attachment?.path) attachments.push({
38
+ name: "trace",
39
+ contentType: "application/zip",
40
+ path: annotation.attachment.path,
41
+ hasBody: false
42
+ });
43
+ for (const artifact of test.artifacts()) {
44
+ if (artifact.type === "internal:failureScreenshot") {
45
+ for (const a of artifact.attachments ?? []) if (a.path) attachments.push({
46
+ name: "screenshot",
47
+ contentType: a.contentType ?? "image/png",
48
+ path: a.path,
49
+ hasBody: false
50
+ });
51
+ }
52
+ if (artifact.type === "internal:toMatchScreenshot") {
53
+ comparisons++;
54
+ const base = `${slug(test.name)}${comparisons > 1 ? `-${comparisons}` : ""}`;
55
+ for (const a of artifact.attachments ?? []) if (a.path && a.name) attachments.push({
56
+ name: `${base}-${a.name === "reference" ? "expected" : a.name}.png`,
57
+ contentType: a.contentType ?? "image/png",
58
+ path: a.path,
59
+ hasBody: false
60
+ });
61
+ }
62
+ }
63
+ return attachments;
64
+ }
65
+ function isBrowserTest(test) {
66
+ return !!test.project.config.browser?.enabled;
67
+ }
68
+ const STORYBOOK_DEBUG_LINE = /^\s*Click to debug the error directly in Storybook:.*$/m;
69
+ function cleanMessage(message) {
70
+ return stripAnsi(message).replace(STORYBOOK_DEBUG_LINE, "").trim();
71
+ }
72
+ function toNormTest(test) {
73
+ const status = statusOf(test);
74
+ if (!status) return void 0;
75
+ const { file, titlePath, projectName } = identity(test);
76
+ const diagnostic = test.diagnostic();
77
+ const story = storyOf(test, titlePath);
78
+ return {
79
+ testKey: makeTestKey(file, titlePath, projectName),
80
+ title: test.name,
81
+ titlePath,
82
+ file,
83
+ line: test.location?.line ?? 0,
84
+ column: test.location?.column ?? 0,
85
+ projectName,
86
+ status,
87
+ ok: status !== "unexpected",
88
+ duration: Math.round(diagnostic?.duration ?? 0),
89
+ retries: diagnostic?.retryCount ?? 0,
90
+ tags: [...test.tags ?? []],
91
+ annotations: [...test.annotations().filter((a) => a.type !== "traces").map((a) => ({
92
+ type: a.type,
93
+ description: a.message
94
+ })), ...story ? [{
95
+ type: STORY_ANNOTATION,
96
+ description: story
97
+ }] : []],
98
+ errors: (test.result().errors ?? []).map((e) => ({
99
+ message: cleanMessage(e.message),
100
+ stack: e.stack
101
+ })),
102
+ attachments: attachmentsOf(test)
103
+ };
104
+ }
105
+ function toNormTests(modules, include = "browser") {
106
+ const tests = [];
107
+ let skippedNodeTests = 0;
108
+ for (const module of modules) for (const test of module.children.allTests()) {
109
+ if (include === "browser" && !isBrowserTest(test)) {
110
+ skippedNodeTests++;
111
+ continue;
112
+ }
113
+ const norm = toNormTest(test);
114
+ if (norm) tests.push(norm);
115
+ }
116
+ return {
117
+ tests,
118
+ skippedNodeTests
119
+ };
120
+ }
121
+ //#endregion
122
+ //#region src/index.ts
123
+ function countsOf(tests) {
124
+ const counts = {
125
+ total: tests.length,
126
+ expected: 0,
127
+ unexpected: 0,
128
+ flaky: 0,
129
+ skipped: 0
130
+ };
131
+ for (const t of tests) counts[t.status]++;
132
+ return counts;
133
+ }
134
+ var KinoraVitestReporter = class {
135
+ options;
136
+ startedAt = /* @__PURE__ */ new Date();
137
+ constructor(options) {
138
+ this.options = options;
139
+ }
140
+ onInit(vitest) {
141
+ this.startedAt = /* @__PURE__ */ new Date();
142
+ vitest.config.includeTaskLocation = true;
143
+ for (const project of vitest.projects) project.config.includeTaskLocation = true;
144
+ }
145
+ onTestRunStart() {
146
+ this.startedAt = /* @__PURE__ */ new Date();
147
+ }
148
+ async onTestRunEnd(modules) {
149
+ const url = this.options.url ?? process.env.KINORA_URL ?? DEFAULT_KINORA_URL;
150
+ const token = this.options.token ?? process.env.KINORA_TOKEN;
151
+ if (!token) {
152
+ console.warn("[kinora] skipping upload: missing token (set KINORA_TOKEN)");
153
+ return;
154
+ }
155
+ const { tests, skippedNodeTests } = toNormTests(modules, this.options.include);
156
+ if (!tests.length) {
157
+ console.warn(skippedNodeTests ? `[kinora] nothing uploaded: no browser tests in this run (${skippedNodeTests} Node tests left out). Set include: 'all' to upload Node tests too.` : "[kinora] nothing uploaded: no tests ran");
158
+ return;
159
+ }
160
+ const detected = detectCiEnv(process.env);
161
+ const payload = {
162
+ project: {
163
+ slug: this.options.project.slug,
164
+ name: this.options.project.name ?? this.options.project.slug
165
+ },
166
+ run: {
167
+ startedAt: this.startedAt.toISOString(),
168
+ duration: Date.now() - this.startedAt.getTime(),
169
+ counts: countsOf(tests),
170
+ git: this.options.git ?? detected.git,
171
+ ci: this.options.ci ?? detected.ci
172
+ },
173
+ tests
174
+ };
175
+ try {
176
+ const client = createIngestClient({
177
+ baseUrl: url,
178
+ token
179
+ });
180
+ const res = await client.uploadRun(payload);
181
+ const kinds = this.options.uploadAttachments ?? DEFAULT_UPLOAD_ATTACHMENTS;
182
+ let artifacts = 0;
183
+ for (const t of tests) for (const a of t.attachments) {
184
+ if (!a.path || !isUploadableAttachment(a, kinds)) continue;
185
+ try {
186
+ await client.uploadArtifact({
187
+ runId: res.runId,
188
+ testKey: t.testKey,
189
+ name: a.name,
190
+ contentType: a.contentType,
191
+ body: await readFile(a.path)
192
+ });
193
+ artifacts++;
194
+ } catch (err) {
195
+ console.warn(`[kinora] ${a.name} upload failed for ${t.testKey}:`, err instanceof Error ? err.message : err);
196
+ }
197
+ }
198
+ console.log(`[kinora] uploaded ${res.tests} tests + ${artifacts} artifacts (run ${res.runId})`);
199
+ } catch (err) {
200
+ if (err instanceof IngestError && err.status === 402) console.warn(`[kinora] ${err.message}`);
201
+ else console.error("[kinora] upload failed:", err instanceof Error ? err.message : err);
202
+ }
203
+ }
204
+ };
205
+ //#endregion
206
+ export { KinoraVitestReporter as default };
package/package.json CHANGED
@@ -1,6 +1,54 @@
1
1
  {
2
2
  "name": "@kinora/vitest-reporter",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "type": "module",
4
+ "version": "0.1.0",
5
+ "description": "Vitest reporter that uploads browser-mode and Storybook test results to a kinora server",
6
+ "author": "Joris Gallot",
7
+ "license": "MIT",
8
+ "homepage": "https://github.com/kinora-dev/kinora#readme",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/kinora-dev/kinora.git",
12
+ "directory": "packages/vitest-reporter"
13
+ },
14
+ "bugs": "https://github.com/kinora-dev/kinora/issues",
15
+ "keywords": [
16
+ "vitest",
17
+ "storybook",
18
+ "reporter",
19
+ "test-report",
20
+ "ci",
21
+ "kinora"
22
+ ],
23
+ "exports": {
24
+ ".": "./dist/index.mjs"
25
+ },
26
+ "main": "./dist/index.mjs",
27
+ "files": [
28
+ "dist"
29
+ ],
30
+ "engines": {
31
+ "node": ">=20"
32
+ },
33
+ "publishConfig": {
34
+ "access": "public"
35
+ },
36
+ "peerDependencies": {
37
+ "vitest": ">=4.1.0"
38
+ },
39
+ "dependencies": {
40
+ "@kinora/core": "0.7.0"
41
+ },
42
+ "devDependencies": {
43
+ "@types/node": "^26.5.1",
44
+ "@vitest/coverage-v8": "^5.0.3",
45
+ "tsdown": "^0.22.14",
46
+ "typescript": "~6.0.3",
47
+ "vitest": "^5.0.3"
48
+ },
49
+ "scripts": {
50
+ "build": "tsdown",
51
+ "typecheck": "tsc --noEmit",
52
+ "test": "vitest run --coverage"
53
+ }
6
54
  }