|
| 1 | +import { createWriteStream } from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | +import YAML from 'yaml'; |
| 4 | + |
| 5 | +class TapWriter { |
| 6 | + #stream; |
| 7 | + #testCount = 0; |
| 8 | + |
| 9 | + constructor(stream) { |
| 10 | + this.#stream = stream; |
| 11 | + } |
| 12 | + |
| 13 | + version() { |
| 14 | + this.#stream.write(`TAP version 14\n`); |
| 15 | + } |
| 16 | + |
| 17 | + testPoint(ok, description, directive, data) { |
| 18 | + this.#stream.write(`${ok ? 'ok' : 'not ok'} ${++this.#testCount} - ${description}`); |
| 19 | + if (directive) { |
| 20 | + this.#stream.write(` # ${directive}`); |
| 21 | + } |
| 22 | + this.#stream.write('\n'); |
| 23 | + if (data) { |
| 24 | + this.#stream.write(' ---\n'); |
| 25 | + for (const line of YAML.stringify(data).split('\n')) { |
| 26 | + this.#stream.write(` ${line}\n`); |
| 27 | + } |
| 28 | + this.#stream.write(' ...\n'); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + plan() { |
| 33 | + this.#stream.write(`1..${this.#testCount}\n`); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +function formatErrorMessage(error) { |
| 38 | + // remove escape codes that jest is leaving in the error message |
| 39 | + return error?.replaceAll(/\x1b\[\d+\w/g, ''); |
| 40 | +} |
| 41 | + |
| 42 | +export default class TapReporter { |
| 43 | + constructor(globalConfig, reporterOptions, reporterContext) { |
| 44 | + this._globalConfig = globalConfig; |
| 45 | + this._options = reporterOptions; |
| 46 | + this._context = reporterContext; |
| 47 | + } |
| 48 | + |
| 49 | + onRunComplete(_testContexts, results) { |
| 50 | + let stream = this._options.filePath |
| 51 | + ? createWriteStream(this._options.filePath) |
| 52 | + : process.stdout; |
| 53 | + let writer = new TapWriter(stream); |
| 54 | + writer.version(); |
| 55 | + |
| 56 | + for (const suiteResult of results.testResults) { |
| 57 | + if (suiteResult.testExecError) { |
| 58 | + // suite failed altogether, treat this as a failed single test |
| 59 | + writer.testPoint( |
| 60 | + false, |
| 61 | + suiteResult.displayName ?? this.#relativePath(suiteResult.testFilePath), |
| 62 | + undefined, |
| 63 | + { |
| 64 | + code: suiteResult.testExecError.code, |
| 65 | + message: suiteResult.testExecError.message, |
| 66 | + stack: suiteResult.testExecError.stack, |
| 67 | + type: suiteResult.testExecError.type, |
| 68 | + }, |
| 69 | + ); |
| 70 | + } else { |
| 71 | + // suite passed, now iterate over individual tests |
| 72 | + for (const testResult of suiteResult.testResults) { |
| 73 | + switch (testResult.status) { |
| 74 | + case 'passed': |
| 75 | + writer.testPoint(true, testResult.fullName, undefined, undefined); |
| 76 | + break; |
| 77 | + case 'failed': |
| 78 | + writer.testPoint(false, testResult.fullName, undefined, { |
| 79 | + message: formatErrorMessage(testResult.failureMessages[0]) ?? 'Unknown failure', |
| 80 | + more: testResult.failureMessages.slice(1).map(formatErrorMessage), |
| 81 | + }); |
| 82 | + break; |
| 83 | + case 'skipped': |
| 84 | + case 'pending': |
| 85 | + writer.testPoint(true, testResult.fullName, 'skip', undefined); |
| 86 | + break; |
| 87 | + case 'todo': |
| 88 | + writer.testPoint(false, testResult.fullName, 'todo', undefined); |
| 89 | + break; |
| 90 | + default: |
| 91 | + writer.testPoint(false, testResult.fullName, undefined, { |
| 92 | + message: `Unknown status ${testResult.status}`, |
| 93 | + }); |
| 94 | + break; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + writer.plan(); |
| 101 | + |
| 102 | + if (stream !== process.stdout) { |
| 103 | + stream.end(); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + getLastError() { |
| 108 | + return undefined; |
| 109 | + } |
| 110 | + |
| 111 | + #relativePath(file) { |
| 112 | + return path.relative(this._globalConfig.rootDir, file); |
| 113 | + } |
| 114 | +} |
0 commit comments