executable-stories-jest 8.1.1 → 8.1.4
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/bin/intent.js +3 -0
- package/dist/reporter.cjs.map +1 -1
- package/dist/reporter.js.map +1 -1
- package/package.json +5 -6
package/bin/intent.js
ADDED
package/dist/reporter.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/reporter.ts"],"sourcesContent":["/**\n * Jest reporter for executable-stories.\n * Generates reports using the executable-stories-formatters package.\n *\n * Uses file-based communication: story.init() writes JSON files to\n * .jest-executable-stories/ which this reporter reads.\n */\n\nimport * as fs from \"node:fs\";\nimport * as path from \"node:path\";\nimport fg from \"fast-glob\";\nimport type { StoryMeta } from \"executable-stories-formatters\";\n\n// Import from formatters package\nimport {\n ReportGenerator,\n canonicalizeRun,\n readGitSha,\n readPackageVersion,\n detectCI,\n type RawRun,\n type RawTestCase,\n type RawAttachment,\n type RawStepEvent,\n type FormatterOptions,\n} from \"executable-stories-formatters\";\n\n// Re-export types from formatters for convenience\nexport type {\n OutputFormat,\n OutputMode,\n ColocatedStyle,\n OutputRule,\n FormatterOptions,\n} from \"executable-stories-formatters\";\n\n// ============================================================================\n// Reporter Options (delegates to FormatterOptions)\n// ============================================================================\n\nexport interface StoryReporterOptions extends FormatterOptions {\n /** If set, write raw run JSON (schemaVersion 1) to this path for use with the executable-stories CLI/binary */\n rawRunPath?: string;\n}\n\n// ============================================================================\n// Internal Types\n// ============================================================================\n\n/** Shape of JSON files written by story.init() */\ninterface StoryFileReport {\n testFilePath: string;\n scenarios: (StoryMeta & {\n _attachments?: Array<{\n name: string;\n mediaType: string;\n path?: string;\n body?: string;\n encoding?: \"BASE64\" | \"IDENTITY\";\n charset?: string;\n fileName?: string;\n stepIndex?: number;\n stepId?: string;\n }>;\n })[];\n}\n\ninterface JestTestResult {\n testFilePath: string;\n testResults: Array<{\n fullName: string;\n status: \"passed\" | \"failed\" | \"pending\" | \"todo\";\n duration?: number;\n failureMessages?: string[];\n }>;\n}\n\ninterface JestAggregatedResult {\n testResults: JestTestResult[];\n}\n\n// ============================================================================\n// Utility Functions\n// ============================================================================\n\n/**\n * Convert path to relative posix format.\n */\nfunction toRelativePosix(absolutePath: string, projectRoot: string): string {\n return path.relative(projectRoot, absolutePath).split(path.sep).join(\"/\");\n}\n\n// ============================================================================\n// Reporter Implementation\n// ============================================================================\n\nexport default class StoryReporter {\n private options: StoryReporterOptions;\n private startTime = 0;\n private packageVersion: string | undefined;\n private gitSha: string | undefined;\n\n constructor(_globalConfig: unknown, reporterOptions: StoryReporterOptions = {}) {\n this.options = reporterOptions;\n }\n\n /** Get the output directory for story JSON files */\n private getOutputDir(): string {\n const baseDir = process.env.JEST_STORY_DOCS_DIR ?? \".jest-executable-stories\";\n return path.resolve(process.cwd(), baseDir);\n }\n\n /** Reset run artifacts (clear previous story files) */\n private resetRunArtifacts(): void {\n try {\n fs.rmSync(this.getOutputDir(), { recursive: true, force: true });\n } catch {\n // Ignore cleanup errors\n }\n }\n\n /** Read story reports written by story.init() during test execution */\n private readStoryReports(): StoryFileReport[] {\n const outputDir = this.getOutputDir();\n if (!fs.existsSync(outputDir)) return [];\n const files = fg.sync(\"**/*.json\", {\n cwd: outputDir,\n onlyFiles: true,\n absolute: true,\n });\n const reports: StoryFileReport[] = [];\n for (const file of files) {\n try {\n const raw = fs.readFileSync(file, \"utf8\");\n const parsed = JSON.parse(raw) as StoryFileReport;\n if (!parsed?.testFilePath || !Array.isArray(parsed.scenarios)) continue;\n reports.push(parsed);\n } catch {\n // Ignore unreadable or malformed files\n }\n }\n return reports;\n }\n\n onRunStart(): void {\n this.resetRunArtifacts();\n this.startTime = Date.now();\n const root = process.cwd();\n const includeMetadata = this.options.markdown?.includeMetadata ?? true;\n if (includeMetadata) {\n this.packageVersion = readPackageVersion(root);\n this.gitSha = readGitSha(root);\n }\n }\n\n async onRunComplete(\n _testContexts: Set<unknown>,\n results: JestAggregatedResult\n ): Promise<void> {\n const root = process.cwd();\n\n // Build map of test results by file for status lookup\n const fileResults = new Map<string, JestTestResult>();\n for (const testFileResult of results.testResults) {\n fileResults.set(testFileResult.testFilePath, testFileResult);\n }\n\n // Read story data from JSON files written by story.init()\n const reports = this.readStoryReports();\n\n // Collect test cases\n const rawTestCases: RawTestCase[] = [];\n\n for (const report of reports) {\n const fileResult = fileResults.get(report.testFilePath);\n const sourceFile = toRelativePosix(report.testFilePath, root);\n\n for (const meta of report.scenarios) {\n if (!meta?.scenario) continue;\n\n // Find matching test result\n const matchingTest = fileResult?.testResults.find((test) => {\n const expectedFullName = meta.suitePath\n ? [...meta.suitePath, meta.scenario].join(\" > \")\n : meta.scenario;\n return test.fullName === expectedFullName;\n });\n\n // Map Jest status to raw status\n const statusMap: Record<string, RawTestCase[\"status\"]> = {\n passed: \"pass\",\n failed: \"fail\",\n pending: \"pending\",\n todo: \"pending\",\n };\n\n const status = matchingTest\n ? statusMap[matchingTest.status] ?? \"unknown\"\n : \"pass\";\n\n // Map attachments\n const rawAttachments: RawAttachment[] = (meta._attachments ?? []).map((a) => ({\n name: a.name,\n mediaType: a.mediaType,\n path: a.path,\n body: a.body,\n encoding: a.encoding,\n charset: a.charset,\n fileName: a.fileName,\n stepIndex: a.stepIndex,\n stepId: a.stepId,\n }));\n\n // Extract step events (timing)\n const stepEvents: RawStepEvent[] = meta.steps\n .filter((s: { durationMs?: number }) => s.durationMs !== undefined)\n .map((s: { durationMs?: number; text: string }, i: number) => ({\n index: i,\n title: s.text,\n durationMs: s.durationMs,\n }));\n\n rawTestCases.push({\n title: meta.scenario,\n titlePath: meta.suitePath\n ? [...meta.suitePath, meta.scenario]\n : [meta.scenario],\n story: meta,\n sourceFile,\n sourceLine: Math.max(1, meta.sourceOrder ?? 1),\n status,\n durationMs: matchingTest?.duration ?? 0,\n error: matchingTest?.failureMessages?.length\n ? { message: matchingTest.failureMessages.join(\"\\n\") }\n : undefined,\n attachments: rawAttachments.length > 0 ? rawAttachments : undefined,\n stepEvents: stepEvents.length > 0 ? stepEvents : undefined,\n retry: 0,\n retries: 0,\n });\n }\n }\n\n if (rawTestCases.length === 0) return;\n\n // Build RawRun\n const rawRun: RawRun = {\n testCases: rawTestCases,\n startedAtMs: this.startTime,\n finishedAtMs: Date.now(),\n projectRoot: root,\n packageVersion: this.packageVersion,\n gitSha: this.gitSha,\n ci: detectCI(),\n };\n\n // Optionally write raw run JSON for CLI/binary consumption\n const rawRunPath = this.options.rawRunPath;\n if (rawRunPath) {\n const absolutePath = path.isAbsolute(rawRunPath)\n ? rawRunPath\n : path.join(root, rawRunPath);\n const dir = path.dirname(absolutePath);\n if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });\n const payload = { schemaVersion: 1, ...rawRun };\n fs.writeFileSync(absolutePath, JSON.stringify(payload, null, 2), \"utf8\");\n }\n\n // Canonicalize\n const canonicalRun = canonicalizeRun(rawRun);\n\n // Generate reports\n const generator = new ReportGenerator(this.options);\n try {\n await generator.generate(canonicalRun);\n } catch (err) {\n console.error(\"Failed to generate reports:\", err);\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAQA,SAAoB;AACpB,WAAsB;AACtB,uBAAe;AAIf,2CAWO;AA+DP,SAAS,gBAAgB,cAAsB,aAA6B;AAC1E,SAAY,cAAS,aAAa,YAAY,EAAE,MAAW,QAAG,EAAE,KAAK,GAAG;AAC1E;AAMA,IAAqB,gBAArB,MAAmC;AAAA,EACzB;AAAA,EACA,YAAY;AAAA,EACZ;AAAA,EACA;AAAA,EAER,YAAY,eAAwB,kBAAwC,CAAC,GAAG;AAC9E,SAAK,UAAU;AAAA,EACjB;AAAA;AAAA,EAGQ,eAAuB;AAC7B,UAAM,UAAU,QAAQ,IAAI,uBAAuB;AACnD,WAAY,aAAQ,QAAQ,IAAI,GAAG,OAAO;AAAA,EAC5C;AAAA;AAAA,EAGQ,oBA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AS,aAAa,gBAAgB,KAAK,IAAI,EAAE,IACnD;AAAA,UACJ,aAAa,eAAe,SAAS,IAAI,iBAAiB;AAAA,UAC1D,YAAY,WAAW,SAAS,IAAI,aAAa;AAAA,UACjD,OAAO;AAAA,UACP,SAAS;AAAA,QACX,CAAC;AAAA,MACH;AAAA,IACF;AAEA,QAAI,aAAa,WAAW,EAAG;AAG/B,UAAM,SAAiB;AAAA,MACrB,WAAW;AAAA,MACX,aAAa,KAAK;AAAA,MAClB,cAAc,KAAK,IAAI;AAAA,MACvB,aAAa;AAAA,MACb,gBAAgB,KAAK;AAAA,MACrB,QAAQ,KAAK;AAAA,MACb,QAAI,+CAAS;AAAA,IACf;AAGA,UAAM,aAAa,KAAK,QAAQ;AAChC,QAAI,YAAY;AACd,YAAM,eAAoB,gBAAW,UAAU,IAC3C,aACK,UAAK,MAAM,UAAU;AAC9B,YAAM,MAAW,aAAQ,YAAY;AACrC,UAAI,CAAI,cAAW,GAAG,EAAG,CAAG,aAAU,KAAK,EAAE,WAAW,KAAK,CAAC;AAC9D,YAAM,UAAU,EAAE,eAAe,GAAG,GAAG,OAAO;AAC9C,MAAG,iBAAc,cAAc,KAAK,UAAU,SAAS,MAAM,CAAC,GAAG,MAAM;AAAA,IACzE;AAGA,UAAM,mBAAe,sDAAgB,MAAM;AAG3C,UAAM,YAAY,IAAI,qDAAgB,KAAK,OAAO;AAClD,QAAI;AACF,YAAM,UAAU,SAAS,YAAY;AAAA,IACvC,SAAS,KAAK;AACZ,cAAQ,MAAM,+BAA+B,GAAG;AAAA,IAClD;AAAA,EACF;AACF;","names":["fg"]}
|
|
1
|
+
{"version":3,"sources":["../src/reporter.ts"],"sourcesContent":["/**\n * Jest reporter for executable-stories.\n * Generates reports using the executable-stories-formatters package.\n *\n * Uses file-based communication: story.init() writes JSON files to\n * .jest-executable-stories/ which this reporter reads.\n */\n\nimport * as fs from \"node:fs\";\nimport * as path from \"node:path\";\nimport fg from \"fast-glob\";\nimport type { StoryMeta } from \"executable-stories-formatters\";\n\n// Import from formatters package\nimport {\n ReportGenerator,\n canonicalizeRun,\n readGitSha,\n readPackageVersion,\n detectCI,\n type RawRun,\n type RawTestCase,\n type RawAttachment,\n type RawStepEvent,\n type FormatterOptions,\n} from \"executable-stories-formatters\";\n\n// Re-export types from formatters for convenience\nexport type {\n OutputFormat,\n OutputMode,\n ColocatedStyle,\n OutputRule,\n FormatterOptions,\n} from \"executable-stories-formatters\";\n\n// ============================================================================\n// Reporter Options (delegates to FormatterOptions)\n// ============================================================================\n\nexport interface StoryReporterOptions extends FormatterOptions {\n /** If set, write raw run JSON (schemaVersion 1) to this path for use with the executable-stories CLI/binary */\n rawRunPath?: string;\n}\n\n// ============================================================================\n// Internal Types\n// ============================================================================\n\n/** Shape of JSON files written by story.init() */\ninterface StoryFileReport {\n testFilePath: string;\n scenarios: (StoryMeta & {\n _attachments?: Array<{\n name: string;\n mediaType: string;\n path?: string;\n body?: string;\n encoding?: \"BASE64\" | \"IDENTITY\";\n charset?: string;\n fileName?: string;\n stepIndex?: number;\n stepId?: string;\n }>;\n })[];\n}\n\ninterface JestTestResult {\n testFilePath: string;\n testResults: Array<{\n fullName: string;\n status: \"passed\" | \"failed\" | \"pending\" | \"todo\";\n duration?: number;\n failureMessages?: string[];\n }>;\n}\n\ninterface JestAggregatedResult {\n testResults: JestTestResult[];\n}\n\n// ============================================================================\n// Utility Functions\n// ============================================================================\n\n/**\n * Convert path to relative posix format.\n */\nfunction toRelativePosix(absolutePath: string, projectRoot: string): string {\n return path.relative(projectRoot, absolutePath).split(path.sep).join(\"/\");\n}\n\n// ============================================================================\n// Reporter Implementation\n// ============================================================================\n\nexport default class StoryReporter {\n private options: StoryReporterOptions;\n private startTime = 0;\n private packageVersion: string | undefined;\n private gitSha: string | undefined;\n\n constructor(_globalConfig: unknown, reporterOptions: StoryReporterOptions = {}) {\n this.options = reporterOptions;\n }\n\n /** Get the output directory for story JSON files */\n private getOutputDir(): string {\n const baseDir = process.env.JEST_STORY_DOCS_DIR ?? \".jest-executable-stories\";\n return path.resolve(process.cwd(), baseDir);\n }\n\n /** Reset run artifacts (clear previous story files) */\n private resetRunArtifacts(): void {\n try {\n fs.rmSync(this.getOutputDir(), { recursive: true, force: true });\n } catch {\n // Ignore cleanup errors\n }\n }\n\n /** Read story reports written by story.init() during test execution */\n private readStoryReports(): StoryFileReport[] {\n const outputDir = this.getOutputDir();\n if (!fs.existsSync(outputDir)) return [];\n const files = fg.sync(\"**/*.json\", {\n cwd: outputDir,\n onlyFiles: true,\n absolute: true,\n });\n const reports: StoryFileReport[] = [];\n for (const file of files) {\n try {\n const raw = fs.readFileSync(file, \"utf8\");\n const parsed = JSON.parse(raw) as StoryFileReport;\n if (!parsed?.testFilePath || !Array.isArray(parsed.scenarios)) continue;\n reports.push(parsed);\n } catch {\n // Ignore unreadable or malformed files\n }\n }\n return reports;\n }\n\n onRunStart(): void {\n this.resetRunArtifacts();\n this.startTime = Date.now();\n const root = process.cwd();\n const includeMetadata = this.options.markdown?.includeMetadata ?? true;\n if (includeMetadata) {\n this.packageVersion = readPackageVersion(root);\n this.gitSha = readGitSha(root);\n }\n }\n\n async onRunComplete(\n _testContexts: Set<unknown>,\n results: JestAggregatedResult\n ): Promise<void> {\n const root = process.cwd();\n\n // Build map of test results by file for status lookup\n const fileResults = new Map<string, JestTestResult>();\n for (const testFileResult of results.testResults) {\n fileResults.set(testFileResult.testFilePath, testFileResult);\n }\n\n // Read story data from JSON files written by story.init()\n const reports = this.readStoryReports();\n\n // Collect test cases\n const rawTestCases: RawTestCase[] = [];\n\n for (const report of reports) {\n const fileResult = fileResults.get(report.testFilePath);\n const sourceFile = toRelativePosix(report.testFilePath, root);\n\n for (const meta of report.scenarios) {\n if (!meta?.scenario) continue;\n\n // Find matching test result\n const matchingTest = fileResult?.testResults.find((test) => {\n const expectedFullName = meta.suitePath\n ? [...meta.suitePath, meta.scenario].join(\" > \")\n : meta.scenario;\n return test.fullName === expectedFullName;\n });\n\n // Map Jest status to raw status\n const statusMap: Record<string, RawTestCase[\"status\"]> = {\n passed: \"pass\",\n failed: \"fail\",\n pending: \"pending\",\n todo: \"pending\",\n };\n\n const status = matchingTest\n ? statusMap[matchingTest.status] ?? \"unknown\"\n : \"pass\";\n\n // Map attachments\n const rawAttachments: RawAttachment[] = (meta._attachments ?? []).map((a: NonNullable<typeof meta._attachments>[number]) => ({\n name: a.name,\n mediaType: a.mediaType,\n path: a.path,\n body: a.body,\n encoding: a.encoding,\n charset: a.charset,\n fileName: a.fileName,\n stepIndex: a.stepIndex,\n stepId: a.stepId,\n }));\n\n // Extract step events (timing)\n const stepEvents: RawStepEvent[] = meta.steps\n .filter((s: { durationMs?: number }) => s.durationMs !== undefined)\n .map((s: { durationMs?: number; text: string }, i: number) => ({\n index: i,\n title: s.text,\n durationMs: s.durationMs,\n }));\n\n rawTestCases.push({\n title: meta.scenario,\n titlePath: meta.suitePath\n ? [...meta.suitePath, meta.scenario]\n : [meta.scenario],\n story: meta,\n sourceFile,\n sourceLine: Math.max(1, meta.sourceOrder ?? 1),\n status,\n durationMs: matchingTest?.duration ?? 0,\n error: matchingTest?.failureMessages?.length\n ? { message: matchingTest.failureMessages.join(\"\\n\") }\n : undefined,\n attachments: rawAttachments.length > 0 ? rawAttachments : undefined,\n stepEvents: stepEvents.length > 0 ? stepEvents : undefined,\n retry: 0,\n retries: 0,\n });\n }\n }\n\n if (rawTestCases.length === 0) return;\n\n // Build RawRun\n const rawRun: RawRun = {\n testCases: rawTestCases,\n startedAtMs: this.startTime,\n finishedAtMs: Date.now(),\n projectRoot: root,\n packageVersion: this.packageVersion,\n gitSha: this.gitSha,\n ci: detectCI(),\n };\n\n // Optionally write raw run JSON for CLI/binary consumption\n const rawRunPath = this.options.rawRunPath;\n if (rawRunPath) {\n const absolutePath = path.isAbsolute(rawRunPath)\n ? rawRunPath\n : path.join(root, rawRunPath);\n const dir = path.dirname(absolutePath);\n if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });\n const payload = { schemaVersion: 1, ...rawRun };\n fs.writeFileSync(absolutePath, JSON.stringify(payload, null, 2), \"utf8\");\n }\n\n // Canonicalize\n const canonicalRun = canonicalizeRun(rawRun);\n\n // Generate reports\n const generator = new ReportGenerator(this.options);\n try {\n await generator.generate(canonicalRun);\n } catch (err) {\n console.error(\"Failed to generate reports:\", err);\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAQA,SAAoB;AACpB,WAAsB;AACtB,uBAAe;AAIf,2CAWO;AA+DP,SAAS,gBAAgB,cAAsB,aAA6B;AAC1E,SAAY,cAAS,aAAa,YAAY,EAAE,MAAW,QAAG,EAAE,KAAK,GAAG;AAC1E;AAMA,IAAqB,gBAArB,MAAmC;AAAA,EACzB;AAAA,EACA,YAAY;AAAA,EACZ;AAAA,EACA;AAAA,EAER,YAAY,eAAwB,kBAAwC,CAAC,GAAG;AAC9E,SAAK,UAAU;AAAA,EACjB;AAAA;AAAA,EAGQ,eAAuB;AAC7B,UAAM,UAAU,QAAQ,IAAI,uBAAuB;AACnD,WAAY,aAAQ,QAAQ,IAAI,GAAG,OAAO;AAAA,EAC5C;AAAA;AAAA,EAGQ,oBAA0B;AAChC,QAAI;AACF,MAAG,UAAO,KAAK,aAAa,GAAG,EAAE,WAAW,MAAM,OAAO,KAAK,CAAC;AAAA,IACjE,QAAQ;AAAA,IAER;AAAA,EACF;AAAA;AAAA,EAGQ,mBAAsC;AAC5C,UAAM,YAAY,KAAK,aAAa;AACpC,QAAI,CAAI,cAAW,SAAS,EAAG,QAAO,CAAC;AACvC,UAAM,QAAQ,iBAAAA,QAAG,KAAK,aAAa;AAAA,MACjC,KAAK;AAAA,MACL,WAAW;AAAA,MACX,UAAU;AAAA,IACZ,CAAC;AACD,UAAM,UAA6B,CAAC;AACpC,eAAW,QAAQ,OAAO;AACxB,UAAI;AACF,cAAM,MAAS,gBAAa,MAAM,MAAM;AACxC,cAAM,SAAS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,QAAI,YAAY;AACd,YAAM,eAAoB,gBAAW,UAAU,IAC3C,aACK,UAAK,MAAM,UAAU;AAC9B,YAAM,MAAW,aAAQ,YAAY;AACrC,UAAI,CAAI,cAAW,GAAG,EAAG,CAAG,aAAU,KAAK,EAAE,WAAW,KAAK,CAAC;AAC9D,YAAM,UAAU,EAAE,eAAe,GAAG,GAAG,OAAO;AAC9C,MAAG,iBAAc,cAAc,KAAK,UAAU,SAAS,MAAM,CAAC,GAAG,MAAM;AAAA,IACzE;AAGA,UAAM,mBAAe,sDAAgB,MAAM;AAG3C,UAAM,YAAY,IAAI,qDAAgB,KAAK,OAAO;AAClD,QAAI;AACF,YAAM,UAAU,SAAS,YAAY;AAAA,IACvC,SAAS,KAAK;AACZ,cAAQ,MAAM,+BAA+B,GAAG;AAAA,IAClD;AAAA,EACF;AACF;","names":["fg"]}
|
package/dist/reporter.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/reporter.ts"],"sourcesContent":["/**\n * Jest reporter for executable-stories.\n * Generates reports using the executable-stories-formatters package.\n *\n * Uses file-based communication: story.init() writes JSON files to\n * .jest-executable-stories/ which this reporter reads.\n */\n\nimport * as fs from \"node:fs\";\nimport * as path from \"node:path\";\nimport fg from \"fast-glob\";\nimport type { StoryMeta } from \"executable-stories-formatters\";\n\n// Import from formatters package\nimport {\n ReportGenerator,\n canonicalizeRun,\n readGitSha,\n readPackageVersion,\n detectCI,\n type RawRun,\n type RawTestCase,\n type RawAttachment,\n type RawStepEvent,\n type FormatterOptions,\n} from \"executable-stories-formatters\";\n\n// Re-export types from formatters for convenience\nexport type {\n OutputFormat,\n OutputMode,\n ColocatedStyle,\n OutputRule,\n FormatterOptions,\n} from \"executable-stories-formatters\";\n\n// ============================================================================\n// Reporter Options (delegates to FormatterOptions)\n// ============================================================================\n\nexport interface StoryReporterOptions extends FormatterOptions {\n /** If set, write raw run JSON (schemaVersion 1) to this path for use with the executable-stories CLI/binary */\n rawRunPath?: string;\n}\n\n// ============================================================================\n// Internal Types\n// ============================================================================\n\n/** Shape of JSON files written by story.init() */\ninterface StoryFileReport {\n testFilePath: string;\n scenarios: (StoryMeta & {\n _attachments?: Array<{\n name: string;\n mediaType: string;\n path?: string;\n body?: string;\n encoding?: \"BASE64\" | \"IDENTITY\";\n charset?: string;\n fileName?: string;\n stepIndex?: number;\n stepId?: string;\n }>;\n })[];\n}\n\ninterface JestTestResult {\n testFilePath: string;\n testResults: Array<{\n fullName: string;\n status: \"passed\" | \"failed\" | \"pending\" | \"todo\";\n duration?: number;\n failureMessages?: string[];\n }>;\n}\n\ninterface JestAggregatedResult {\n testResults: JestTestResult[];\n}\n\n// ============================================================================\n// Utility Functions\n// ============================================================================\n\n/**\n * Convert path to relative posix format.\n */\nfunction toRelativePosix(absolutePath: string, projectRoot: string): string {\n return path.relative(projectRoot, absolutePath).split(path.sep).join(\"/\");\n}\n\n// ============================================================================\n// Reporter Implementation\n// ============================================================================\n\nexport default class StoryReporter {\n private options: StoryReporterOptions;\n private startTime = 0;\n private packageVersion: string | undefined;\n private gitSha: string | undefined;\n\n constructor(_globalConfig: unknown, reporterOptions: StoryReporterOptions = {}) {\n this.options = reporterOptions;\n }\n\n /** Get the output directory for story JSON files */\n private getOutputDir(): string {\n const baseDir = process.env.JEST_STORY_DOCS_DIR ?? \".jest-executable-stories\";\n return path.resolve(process.cwd(), baseDir);\n }\n\n /** Reset run artifacts (clear previous story files) */\n private resetRunArtifacts(): void {\n try {\n fs.rmSync(this.getOutputDir(), { recursive: true, force: true });\n } catch {\n // Ignore cleanup errors\n }\n }\n\n /** Read story reports written by story.init() during test execution */\n private readStoryReports(): StoryFileReport[] {\n const outputDir = this.getOutputDir();\n if (!fs.existsSync(outputDir)) return [];\n const files = fg.sync(\"**/*.json\", {\n cwd: outputDir,\n onlyFiles: true,\n absolute: true,\n });\n const reports: StoryFileReport[] = [];\n for (const file of files) {\n try {\n const raw = fs.readFileSync(file, \"utf8\");\n const parsed = JSON.parse(raw) as StoryFileReport;\n if (!parsed?.testFilePath || !Array.isArray(parsed.scenarios)) continue;\n reports.push(parsed);\n } catch {\n // Ignore unreadable or malformed files\n }\n }\n return reports;\n }\n\n onRunStart(): void {\n this.resetRunArtifacts();\n this.startTime = Date.now();\n const root = process.cwd();\n const includeMetadata = this.options.markdown?.includeMetadata ?? true;\n if (includeMetadata) {\n this.packageVersion = readPackageVersion(root);\n this.gitSha = readGitSha(root);\n }\n }\n\n async onRunComplete(\n _testContexts: Set<unknown>,\n results: JestAggregatedResult\n ): Promise<void> {\n const root = process.cwd();\n\n // Build map of test results by file for status lookup\n const fileResults = new Map<string, JestTestResult>();\n for (const testFileResult of results.testResults) {\n fileResults.set(testFileResult.testFilePath, testFileResult);\n }\n\n // Read story data from JSON files written by story.init()\n const reports = this.readStoryReports();\n\n // Collect test cases\n const rawTestCases: RawTestCase[] = [];\n\n for (const report of reports) {\n const fileResult = fileResults.get(report.testFilePath);\n const sourceFile = toRelativePosix(report.testFilePath, root);\n\n for (const meta of report.scenarios) {\n if (!meta?.scenario) continue;\n\n // Find matching test result\n const matchingTest = fileResult?.testResults.find((test) => {\n const expectedFullName = meta.suitePath\n ? [...meta.suitePath, meta.scenario].join(\" > \")\n : meta.scenario;\n return test.fullName === expectedFullName;\n });\n\n // Map Jest status to raw status\n const statusMap: Record<string, RawTestCase[\"status\"]> = {\n passed: \"pass\",\n failed: \"fail\",\n pending: \"pending\",\n todo: \"pending\",\n };\n\n const status = matchingTest\n ? statusMap[matchingTest.status] ?? \"unknown\"\n : \"pass\";\n\n // Map attachments\n const rawAttachments: RawAttachment[] = (meta._attachments ?? []).map((a) => ({\n name: a.name,\n mediaType: a.mediaType,\n path: a.path,\n body: a.body,\n encoding: a.encoding,\n charset: a.charset,\n fileName: a.fileName,\n stepIndex: a.stepIndex,\n stepId: a.stepId,\n }));\n\n // Extract step events (timing)\n const stepEvents: RawStepEvent[] = meta.steps\n .filter((s: { durationMs?: number }) => s.durationMs !== undefined)\n .map((s: { durationMs?: number; text: string }, i: number) => ({\n index: i,\n title: s.text,\n durationMs: s.durationMs,\n }));\n\n rawTestCases.push({\n title: meta.scenario,\n titlePath: meta.suitePath\n ? [...meta.suitePath, meta.scenario]\n : [meta.scenario],\n story: meta,\n sourceFile,\n sourceLine: Math.max(1, meta.sourceOrder ?? 1),\n status,\n durationMs: matchingTest?.duration ?? 0,\n error: matchingTest?.failureMessages?.length\n ? { message: matchingTest.failureMessages.join(\"\\n\") }\n : undefined,\n attachments: rawAttachments.length > 0 ? rawAttachments : undefined,\n stepEvents: stepEvents.length > 0 ? stepEvents : undefined,\n retry: 0,\n retries: 0,\n });\n }\n }\n\n if (rawTestCases.length === 0) return;\n\n // Build RawRun\n const rawRun: RawRun = {\n testCases: rawTestCases,\n startedAtMs: this.startTime,\n finishedAtMs: Date.now(),\n projectRoot: root,\n packageVersion: this.packageVersion,\n gitSha: this.gitSha,\n ci: detectCI(),\n };\n\n // Optionally write raw run JSON for CLI/binary consumption\n const rawRunPath = this.options.rawRunPath;\n if (rawRunPath) {\n const absolutePath = path.isAbsolute(rawRunPath)\n ? rawRunPath\n : path.join(root, rawRunPath);\n const dir = path.dirname(absolutePath);\n if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });\n const payload = { schemaVersion: 1, ...rawRun };\n fs.writeFileSync(absolutePath, JSON.stringify(payload, null, 2), \"utf8\");\n }\n\n // Canonicalize\n const canonicalRun = canonicalizeRun(rawRun);\n\n // Generate reports\n const generator = new ReportGenerator(this.options);\n try {\n await generator.generate(canonicalRun);\n } catch (err) {\n console.error(\"Failed to generate reports:\", err);\n }\n }\n}\n"],"mappings":";AAQA,YAAY,QAAQ;AACpB,YAAY,UAAU;AACtB,OAAO,QAAQ;AAIf;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAMK;AA+DP,SAAS,gBAAgB,cAAsB,aAA6B;AAC1E,SAAY,cAAS,aAAa,YAAY,EAAE,MAAW,Q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,SAAS,KAAK;AACZ,cAAQ,MAAM,+BAA+B,GAAG;AAAA,IAClD;AAAA,EACF;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/reporter.ts"],"sourcesContent":["/**\n * Jest reporter for executable-stories.\n * Generates reports using the executable-stories-formatters package.\n *\n * Uses file-based communication: story.init() writes JSON files to\n * .jest-executable-stories/ which this reporter reads.\n */\n\nimport * as fs from \"node:fs\";\nimport * as path from \"node:path\";\nimport fg from \"fast-glob\";\nimport type { StoryMeta } from \"executable-stories-formatters\";\n\n// Import from formatters package\nimport {\n ReportGenerator,\n canonicalizeRun,\n readGitSha,\n readPackageVersion,\n detectCI,\n type RawRun,\n type RawTestCase,\n type RawAttachment,\n type RawStepEvent,\n type FormatterOptions,\n} from \"executable-stories-formatters\";\n\n// Re-export types from formatters for convenience\nexport type {\n OutputFormat,\n OutputMode,\n ColocatedStyle,\n OutputRule,\n FormatterOptions,\n} from \"executable-stories-formatters\";\n\n// ============================================================================\n// Reporter Options (delegates to FormatterOptions)\n// ============================================================================\n\nexport interface StoryReporterOptions extends FormatterOptions {\n /** If set, write raw run JSON (schemaVersion 1) to this path for use with the executable-stories CLI/binary */\n rawRunPath?: string;\n}\n\n// ============================================================================\n// Internal Types\n// ============================================================================\n\n/** Shape of JSON files written by story.init() */\ninterface StoryFileReport {\n testFilePath: string;\n scenarios: (StoryMeta & {\n _attachments?: Array<{\n name: string;\n mediaType: string;\n path?: string;\n body?: string;\n encoding?: \"BASE64\" | \"IDENTITY\";\n charset?: string;\n fileName?: string;\n stepIndex?: number;\n stepId?: string;\n }>;\n })[];\n}\n\ninterface JestTestResult {\n testFilePath: string;\n testResults: Array<{\n fullName: string;\n status: \"passed\" | \"failed\" | \"pending\" | \"todo\";\n duration?: number;\n failureMessages?: string[];\n }>;\n}\n\ninterface JestAggregatedResult {\n testResults: JestTestResult[];\n}\n\n// ============================================================================\n// Utility Functions\n// ============================================================================\n\n/**\n * Convert path to relative posix format.\n */\nfunction toRelativePosix(absolutePath: string, projectRoot: string): string {\n return path.relative(projectRoot, absolutePath).split(path.sep).join(\"/\");\n}\n\n// ============================================================================\n// Reporter Implementation\n// ============================================================================\n\nexport default class StoryReporter {\n private options: StoryReporterOptions;\n private startTime = 0;\n private packageVersion: string | undefined;\n private gitSha: string | undefined;\n\n constructor(_globalConfig: unknown, reporterOptions: StoryReporterOptions = {}) {\n this.options = reporterOptions;\n }\n\n /** Get the output directory for story JSON files */\n private getOutputDir(): string {\n const baseDir = process.env.JEST_STORY_DOCS_DIR ?? \".jest-executable-stories\";\n return path.resolve(process.cwd(), baseDir);\n }\n\n /** Reset run artifacts (clear previous story files) */\n private resetRunArtifacts(): void {\n try {\n fs.rmSync(this.getOutputDir(), { recursive: true, force: true });\n } catch {\n // Ignore cleanup errors\n }\n }\n\n /** Read story reports written by story.init() during test execution */\n private readStoryReports(): StoryFileReport[] {\n const outputDir = this.getOutputDir();\n if (!fs.existsSync(outputDir)) return [];\n const files = fg.sync(\"**/*.json\", {\n cwd: outputDir,\n onlyFiles: true,\n absolute: true,\n });\n const reports: StoryFileReport[] = [];\n for (const file of files) {\n try {\n const raw = fs.readFileSync(file, \"utf8\");\n const parsed = JSON.parse(raw) as StoryFileReport;\n if (!parsed?.testFilePath || !Array.isArray(parsed.scenarios)) continue;\n reports.push(parsed);\n } catch {\n // Ignore unreadable or malformed files\n }\n }\n return reports;\n }\n\n onRunStart(): void {\n this.resetRunArtifacts();\n this.startTime = Date.now();\n const root = process.cwd();\n const includeMetadata = this.options.markdown?.includeMetadata ?? true;\n if (includeMetadata) {\n this.packageVersion = readPackageVersion(root);\n this.gitSha = readGitSha(root);\n }\n }\n\n async onRunComplete(\n _testContexts: Set<unknown>,\n results: JestAggregatedResult\n ): Promise<void> {\n const root = process.cwd();\n\n // Build map of test results by file for status lookup\n const fileResults = new Map<string, JestTestResult>();\n for (const testFileResult of results.testResults) {\n fileResults.set(testFileResult.testFilePath, testFileResult);\n }\n\n // Read story data from JSON files written by story.init()\n const reports = this.readStoryReports();\n\n // Collect test cases\n const rawTestCases: RawTestCase[] = [];\n\n for (const report of reports) {\n const fileResult = fileResults.get(report.testFilePath);\n const sourceFile = toRelativePosix(report.testFilePath, root);\n\n for (const meta of report.scenarios) {\n if (!meta?.scenario) continue;\n\n // Find matching test result\n const matchingTest = fileResult?.testResults.find((test) => {\n const expectedFullName = meta.suitePath\n ? [...meta.suitePath, meta.scenario].join(\" > \")\n : meta.scenario;\n return test.fullName === expectedFullName;\n });\n\n // Map Jest status to raw status\n const statusMap: Record<string, RawTestCase[\"status\"]> = {\n passed: \"pass\",\n failed: \"fail\",\n pending: \"pending\",\n todo: \"pending\",\n };\n\n const status = matchingTest\n ? statusMap[matchingTest.status] ?? \"unknown\"\n : \"pass\";\n\n // Map attachments\n const rawAttachments: RawAttachment[] = (meta._attachments ?? []).map((a: NonNullable<typeof meta._attachments>[number]) => ({\n name: a.name,\n mediaType: a.mediaType,\n path: a.path,\n body: a.body,\n encoding: a.encoding,\n charset: a.charset,\n fileName: a.fileName,\n stepIndex: a.stepIndex,\n stepId: a.stepId,\n }));\n\n // Extract step events (timing)\n const stepEvents: RawStepEvent[] = meta.steps\n .filter((s: { durationMs?: number }) => s.durationMs !== undefined)\n .map((s: { durationMs?: number; text: string }, i: number) => ({\n index: i,\n title: s.text,\n durationMs: s.durationMs,\n }));\n\n rawTestCases.push({\n title: meta.scenario,\n titlePath: meta.suitePath\n ? [...meta.suitePath, meta.scenario]\n : [meta.scenario],\n story: meta,\n sourceFile,\n sourceLine: Math.max(1, meta.sourceOrder ?? 1),\n status,\n durationMs: matchingTest?.duration ?? 0,\n error: matchingTest?.failureMessages?.length\n ? { message: matchingTest.failureMessages.join(\"\\n\") }\n : undefined,\n attachments: rawAttachments.length > 0 ? rawAttachments : undefined,\n stepEvents: stepEvents.length > 0 ? stepEvents : undefined,\n retry: 0,\n retries: 0,\n });\n }\n }\n\n if (rawTestCases.length === 0) return;\n\n // Build RawRun\n const rawRun: RawRun = {\n testCases: rawTestCases,\n startedAtMs: this.startTime,\n finishedAtMs: Date.now(),\n projectRoot: root,\n packageVersion: this.packageVersion,\n gitSha: this.gitSha,\n ci: detectCI(),\n };\n\n // Optionally write raw run JSON for CLI/binary consumption\n const rawRunPath = this.options.rawRunPath;\n if (rawRunPath) {\n const absolutePath = path.isAbsolute(rawRunPath)\n ? rawRunPath\n : path.join(root, rawRunPath);\n const dir = path.dirname(absolutePath);\n if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });\n const payload = { schemaVersion: 1, ...rawRun };\n fs.writeFileSync(absolutePath, JSON.stringify(payload, null, 2), \"utf8\");\n }\n\n // Canonicalize\n const canonicalRun = canonicalizeRun(rawRun);\n\n // Generate reports\n const generator = new ReportGenerator(this.options);\n try {\n await generator.generate(canonicalRun);\n } catch (err) {\n console.error(\"Failed to generate reports:\", err);\n }\n }\n}\n"],"mappings":";AAQA,YAAY,QAAQ;AACpB,YAAY,UAAU;AACtB,OAAO,QAAQ;AAIf;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAMK;AA+DP,SAAS,gBAAgB,cAAsB,aAA6B;AAC1E,SAAY,cAAS,aAAa,YAAY,EAAE,MAAW,QAAG,EAAE,KAAK,GAAG;AAC1E;AAMA,IAAqB,gBAArB,MAAmC;AAAA,EACzB;AAAA,EACA,YAAY;AAAA,EACZ;AAAA,EACA;AAAA,EAER,YAAY,eAAwB,kBAAwC,CAAC,GAAG;AAC9E,SAAK,UAAU;AAAA,EACjB;AAAA;AAAA,EAGQ,eAAuB;AAC7B,UAAM,UAAU,QAAQ,IAAI,uBAAuB;AACnD,WAAY,aAAQ,QAAQ,IAAI,GAAG,OAAO;AAAA,EAC5C;AAAA;AAAA,EAGQ,oBAA0B;AAChC,QAAI;AACF,MAAG,UAAO,KAAK,aAAa,GAAG,EAAE,WAAW,MAAM,OAAO,KAAK,CAAC;AAAA,IACjE,QAAQ;AAAA,IAER;AAAA,EACF;AAAA;AA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O;AAAA,UACP,SAAS;AAAA,QACX,CAAC;AAAA,MACH;AAAA,IACF;AAEA,QAAI,aAAa,WAAW,EAAG;AAG/B,UAAM,SAAiB;AAAA,MACrB,WAAW;AAAA,MACX,aAAa,KAAK;AAAA,MAClB,cAAc,KAAK,IAAI;AAAA,MACvB,aAAa;AAAA,MACb,gBAAgB,KAAK;AAAA,MACrB,QAAQ,KAAK;AAAA,MACb,IAAI,SAAS;AAAA,IACf;AAGA,UAAM,aAAa,KAAK,QAAQ;AAChC,QAAI,YAAY;AACd,YAAM,eAAoB,gBAAW,UAAU,IAC3C,aACK,UAAK,MAAM,UAAU;AAC9B,YAAM,MAAW,aAAQ,YAAY;AACrC,UAAI,CAAI,cAAW,GAAG,EAAG,CAAG,aAAU,KAAK,EAAE,WAAW,KAAK,CAAC;AAC9D,YAAM,UAAU,EAAE,eAAe,GAAG,GAAG,OAAO;AAC9C,MAAG,iBAAc,cAAc,KAAK,UAAU,SAAS,MAAM,CAAC,GAAG,MAAM;AAAA,IACzE;AAGA,UAAM,eAAe,gBAAgB,MAAM;AAG3C,UAAM,YAAY,IAAI,gBAAgB,KAAK,OAAO;AAClD,QAAI;AACF,YAAM,UAAU,SAAS,YAAY;AAAA,IACvC,SAAS,KAAK;AACZ,cAAQ,MAAM,+BAA+B,GAAG;AAAA,IAClD;AAAA,EACF;AACF;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "executable-stories-jest",
|
|
3
|
-
"version": "8.1.
|
|
3
|
+
"version": "8.1.4",
|
|
4
4
|
"description": "BDD-style executable stories for Jest with documentation generation",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -30,11 +30,11 @@
|
|
|
30
30
|
"bin"
|
|
31
31
|
],
|
|
32
32
|
"peerDependencies": {
|
|
33
|
-
"jest": ">=30.3.0"
|
|
34
|
-
"executable-stories-formatters": "^0.7.1"
|
|
33
|
+
"jest": ">=30.3.0"
|
|
35
34
|
},
|
|
36
35
|
"dependencies": {
|
|
37
|
-
"fast-glob": "^3.3.3"
|
|
36
|
+
"fast-glob": "^3.3.3",
|
|
37
|
+
"executable-stories-formatters": "0.7.3"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"@jest/globals": "^30.3.0",
|
|
@@ -44,8 +44,7 @@
|
|
|
44
44
|
"jest": "^30.2.0",
|
|
45
45
|
"ts-jest": "^29.4.6",
|
|
46
46
|
"tsup": "^8.5.1",
|
|
47
|
-
"typescript": "~5.9.3"
|
|
48
|
-
"executable-stories-formatters": "0.7.1"
|
|
47
|
+
"typescript": "~5.9.3"
|
|
49
48
|
},
|
|
50
49
|
"repository": {
|
|
51
50
|
"type": "git",
|