@kaizenreport/kensho-cypress 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.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +52 -0
  3. package/index.js +152 -0
  4. package/package.json +42 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Brandon Ordoñez
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 ADDED
@@ -0,0 +1,52 @@
1
+ # @kaizenreport/kensho-cypress
2
+
3
+ A Cypress reporter that emits the canonical [Kensho v1](../schema) JSON format. Cypress uses Mocha under the hood, so this reporter plugs in via the standard Mocha reporter API.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ pnpm add -D @kaizenreport/kensho-cypress
9
+ # or
10
+ npm i -D @kaizenreport/kensho-cypress
11
+ ```
12
+
13
+ ## Configure
14
+
15
+ ```js
16
+ // cypress.config.js
17
+ const { defineConfig } = require('cypress');
18
+
19
+ module.exports = defineConfig({
20
+ reporter: '@kaizenreport/kensho-cypress',
21
+ reporterOptions: {
22
+ output: 'kensho-results',
23
+ project: { name: 'Acme Web', slug: 'acme-web' },
24
+ severityFromTag: true,
25
+ },
26
+ });
27
+ ```
28
+
29
+ ## Run
30
+
31
+ ```bash
32
+ npx cypress run
33
+ # => kensho-results/ populated with cases/*.json and run.json
34
+
35
+ npx kensho generate # → kensho-report/
36
+ npx kensho open
37
+ ```
38
+
39
+ ## What it produces
40
+
41
+ - `kensho-results/run.json` — run manifest (project, env, totals, timing)
42
+ - `kensho-results/cases/<stableId>.json` — one per test case
43
+ - `kensho-results/attachments/` — empty by default (Cypress screenshots/videos
44
+ are handled per-spec; wire a `cy.task()` hook if you want to copy them in).
45
+
46
+ Each test gets a **stable id** hashed from its fullName + file path, so the
47
+ same test correlates across runs for history and flakiness tracking.
48
+
49
+ ## Environment auto-detected
50
+
51
+ GitHub Actions, CircleCI, GitLab CI, Jenkins, Buildkite — CI provider, branch,
52
+ commit, run URL, OS, architecture, Node version.
package/index.js ADDED
@@ -0,0 +1,152 @@
1
+ // @kaizenreport/kensho-cypress — Cypress reporter (built on Mocha's reporter API).
2
+ // Collects pass/fail/pending events from the Mocha runner, then writes
3
+ // kensho-results/run.json + cases/<id>.json on the runner 'end' event.
4
+
5
+ import { mkdirSync, writeFileSync } from 'node:fs';
6
+ import { resolve, relative } from 'node:path';
7
+ import { emptyRun, computeTotals, stableCaseId, validateRun, envInfo } from '@kaizenreport/kensho-schema';
8
+
9
+ // envInfo() is imported from @kaizenreport/kensho-schema below.
10
+
11
+ function suiteChainOf(test) {
12
+ const chain = [];
13
+ for (let s = test.parent; s; s = s.parent) {
14
+ if (s.title) chain.unshift(s.title);
15
+ }
16
+ return chain;
17
+ }
18
+
19
+ function mapMochaStatus(state) {
20
+ if (state === 'passed') return 'pass';
21
+ if (state === 'failed') return 'fail';
22
+ if (state === 'pending') return 'skip';
23
+ return 'broken';
24
+ }
25
+
26
+ function extractInlineTags(title) {
27
+ const tags = [];
28
+ const re = /@([\w-]+)/g;
29
+ let m;
30
+ while ((m = re.exec(title || ''))) tags.push(m[1]);
31
+ return tags;
32
+ }
33
+
34
+ function severityFromTags(tags) {
35
+ for (const t of tags || []) {
36
+ const m = /^@?(blocker|critical|normal|minor|trivial)$/i.exec(t);
37
+ if (m) return m[1].toLowerCase();
38
+ }
39
+ return undefined;
40
+ }
41
+
42
+ export default class KenshoCypressReporter {
43
+ /**
44
+ * Mocha-style reporter constructor.
45
+ * @param {*} runner - Mocha Runner
46
+ * @param {{ reporterOptions?: any }} opts
47
+ */
48
+ constructor(runner, opts = {}) {
49
+ const options = (opts && opts.reporterOptions) || {};
50
+ this.outputDir = resolve(process.cwd(), options.output || 'kensho-results');
51
+ this.casesDir = resolve(this.outputDir, 'cases');
52
+ this.attachmentsDir = resolve(this.outputDir, 'attachments');
53
+ this.project = options.project || {};
54
+ this.severityFromTag = options.severityFromTag !== false;
55
+ this.runId = options.runId || ('run_' + new Date().toISOString().replace(/[^0-9]/g, '').slice(0, 14));
56
+ this.startedAt = new Date().toISOString();
57
+ this.casesById = new Map();
58
+
59
+ mkdirSync(this.outputDir, { recursive: true });
60
+ mkdirSync(this.casesDir, { recursive: true });
61
+ mkdirSync(this.attachmentsDir, { recursive: true });
62
+
63
+ if (!runner) return; // allow instantiation for tests without a runner
64
+
65
+ runner.on('pass', (test) => this._record(test, 'pass'));
66
+ runner.on('fail', (test, err) => this._record(test, 'fail', err));
67
+ runner.on('pending', (test) => this._record(test, 'skip'));
68
+ runner.on('end', () => this._finalize(runner));
69
+ }
70
+
71
+ _record(test, statusLabel, err) {
72
+ try {
73
+ const caseObj = this._toKenshoCase(test, statusLabel, err);
74
+ writeFileSync(
75
+ resolve(this.casesDir, caseObj.id + '.json'),
76
+ JSON.stringify(caseObj, null, 2),
77
+ );
78
+ this.casesById.set(caseObj.id, caseObj);
79
+ } catch (e) {
80
+ console.error('[kensho] failed to write case:', e && e.message);
81
+ }
82
+ }
83
+
84
+ _toKenshoCase(test, statusLabel, err) {
85
+ const suite = suiteChainOf(test);
86
+ const fullName = suite.concat(test.title || '').join(' › ');
87
+ const filePath = test.file ? relative(process.cwd(), test.file) : undefined;
88
+ let id = stableCaseId(fullName, filePath);
89
+ if (this.casesById.has(id)) {
90
+ let i = 2;
91
+ while (this.casesById.has(id + '_' + i)) i++;
92
+ id = id + '_' + i;
93
+ }
94
+ const tags = extractInlineTags(test.title);
95
+ const duration = Math.max(0, Math.round(test.duration || 0));
96
+ const startedAt = new Date().toISOString();
97
+
98
+ const errors = err ? [{
99
+ message: String(err.message || err),
100
+ stack: err.stack,
101
+ type: err.name,
102
+ }] : undefined;
103
+
104
+ return {
105
+ id,
106
+ name: test.title || 'unnamed',
107
+ fullName,
108
+ filePath,
109
+ suite,
110
+ tags,
111
+ severity: this.severityFromTag ? severityFromTags(tags) : undefined,
112
+ status: mapMochaStatus(statusLabel === 'pass' ? 'passed' : statusLabel === 'fail' ? 'failed' : 'pending'),
113
+ startedAt,
114
+ finishedAt: new Date(Date.parse(startedAt) + duration).toISOString(),
115
+ duration,
116
+ retries: (test.currentRetry && test.currentRetry()) || 0,
117
+ platform: process.platform,
118
+ steps: [],
119
+ errors,
120
+ attachments: [],
121
+ logs: [],
122
+ };
123
+ }
124
+
125
+ _finalize() {
126
+ const cases = [...this.casesById.values()];
127
+ const finishedAt = new Date().toISOString();
128
+ const run = emptyRun({
129
+ id: this.runId,
130
+ project: {
131
+ name: this.project.name || 'Unknown project',
132
+ slug: this.project.slug || 'unknown',
133
+ url: this.project.url,
134
+ },
135
+ framework: { name: 'cypress', version: process.env.CYPRESS_VERSION || 'unknown' },
136
+ env: envInfo(),
137
+ startedAt: this.startedAt,
138
+ });
139
+ run.finishedAt = finishedAt;
140
+ run.durationMs = Math.max(0, Date.parse(finishedAt) - Date.parse(this.startedAt));
141
+ run.testCases = cases;
142
+ run.totals = computeTotals(cases);
143
+
144
+ writeFileSync(resolve(this.outputDir, 'run.json'), JSON.stringify(run, null, 2));
145
+ const { ok, errors } = validateRun(run);
146
+ if (!ok) {
147
+ console.warn('[kensho] run.json failed validation:');
148
+ for (const e of errors.slice(0, 8)) console.warn(' -', e);
149
+ }
150
+ console.log(`[kensho] wrote ${cases.length} cases + run.json to ${this.outputDir}`);
151
+ }
152
+ }
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@kaizenreport/kensho-cypress",
3
+ "version": "0.1.0",
4
+ "description": "Kensho reporter for Cypress (Mocha runner) — writes kensho-results/ so the Kensho CLI can generate a rich HTML report.",
5
+ "type": "module",
6
+ "main": "index.js",
7
+ "files": [
8
+ "index.js",
9
+ "README.md"
10
+ ],
11
+ "peerDependencies": {
12
+ "cypress": ">=12.0.0",
13
+ "mocha": ">=10.0.0"
14
+ },
15
+ "peerDependenciesMeta": {
16
+ "cypress": {
17
+ "optional": true
18
+ },
19
+ "mocha": {
20
+ "optional": true
21
+ }
22
+ },
23
+ "dependencies": {
24
+ "@kaizenreport/kensho-schema": "0.1.0"
25
+ },
26
+ "engines": {
27
+ "node": ">=22"
28
+ },
29
+ "license": "Apache-2.0",
30
+ "publishConfig": {
31
+ "access": "public"
32
+ },
33
+ "repository": {
34
+ "type": "git",
35
+ "url": "git+https://github.com/brandon1794/kensho.git",
36
+ "directory": "packages/cypress"
37
+ },
38
+ "homepage": "https://github.com/brandon1794/kensho/tree/main/packages/cypress#readme",
39
+ "bugs": {
40
+ "url": "https://github.com/brandon1794/kensho/issues"
41
+ }
42
+ }