This repository has been archived by the owner on Dec 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
327 additions
and
48 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/** @typedef {import("./types").IntroFormatter} IntroFormatter */ | ||
/** @typedef {import("./types").TestFormatter} TestFormatter */ | ||
/** @typedef {import("./types").SuiteFormatter} SuiteFormatter */ | ||
/** @typedef {import("./types").Reporter} Reporter */ | ||
|
||
import { formatHrTime } from "../utils/node.js" | ||
|
||
/** | ||
* Format intro to TAP format | ||
* | ||
* @type {IntroFormatter} | ||
* | ||
* @example | ||
* formatIntro({ count: 2 }) | ||
* // => "TAP version 13" | ||
* // => "1..2" | ||
*/ | ||
const formatIntro = ({ count, description }) => | ||
["TAP version 14", `1..${count}`, description ? `# ${description}` : ""].join( | ||
"\n" | ||
) | ||
|
||
/** | ||
* Format test file result to TAP format | ||
* | ||
* @type {TestFormatter} | ||
* | ||
* @example | ||
* formatTestResult({ | ||
* name: "src/__tests__/index.test.js", | ||
* errors: [], | ||
* }) | ||
* // => "ok 1 - src/__tests__/index.test.js 0.8s" | ||
* | ||
* @example | ||
* formatTestResult({ | ||
* name: "src/__tests__/index.test.js", | ||
* errors: [ | ||
* { row: 1, column: 1, message: "Unexpected token" }, | ||
* ], | ||
* }) | ||
* // => "not ok 1 - src/__tests__/index.test.js 1.2s" | ||
* // => " ---" | ||
* // => " message: 'Unexpected token' | ||
* // => " severity: fail" | ||
* // => " data:" | ||
* // => " row: 1" | ||
* // => " column: 1" | ||
* // => " ..." | ||
*/ | ||
const formatTest = ({ index, result: { name, duration, errors } }) => { | ||
const humanReadableDuration = formatHrTime(duration) | ||
|
||
if (errors.length === 0) { | ||
return `ok ${index} - ${name} ${humanReadableDuration}` | ||
} | ||
|
||
const output = [`not ok - ${index} ${name} ${humanReadableDuration}`] | ||
|
||
for (const error of errors) { | ||
output.push( | ||
` ---`, | ||
` message: '${error.message}'`, | ||
` severity: fail`, | ||
` data:`, | ||
` row: ${error.row}`, | ||
` column: ${error.column}`, | ||
` ...` | ||
) | ||
} | ||
|
||
return output.join("\n") | ||
} | ||
|
||
/** | ||
* Format the summary of multiple tests to TAP format | ||
* | ||
* @type {SuiteFormatter} | ||
* | ||
* @example | ||
* formatSuiteResult({ | ||
* passCount: 1, | ||
* failCount: 1, | ||
* duration: [5, 420000000] | ||
* }) | ||
* // => "1..2" | ||
* // => "# tests 2" | ||
* // => "# pass 1" | ||
* // => "# fail 1" | ||
* // => "Duration: 5.42s" | ||
*/ | ||
const formatSuite = ({ passCount, failCount, duration }) => { | ||
const totalCount = passCount + failCount | ||
|
||
return [ | ||
`# tests ${totalCount}`, | ||
`# pass ${passCount}`, | ||
`# fail ${failCount}`, | ||
`# time ${formatHrTime(duration)}`, | ||
].join("\n") | ||
} | ||
|
||
/** @type {Reporter} */ | ||
export default { | ||
formatIntro, | ||
formatTest, | ||
formatSuite, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import type { TestResult } from "../runTest.js" | ||
|
||
export type IntroFormatter = (props: { | ||
count: number | ||
description?: string | ||
}) => string | ||
|
||
export type TestFormatter = (props: { | ||
index?: number | ||
result: TestResult | ||
}) => string | ||
|
||
export type SuiteFormatter = (props: { | ||
passCount: number | ||
failCount: number | ||
duration: [number, number] | ||
}) => string | ||
|
||
export type Reporter = { | ||
formatIntro: IntroFormatter | ||
formatTest: TestFormatter | ||
formatSuite: SuiteFormatter | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { runTest } from "./runTest.js" | ||
|
||
/** | ||
* @callback OnTestFinish | ||
* @param {ReturnType<runTest>} result | ||
* @param {number} index | ||
* @returns {void} | ||
*/ | ||
|
||
/** | ||
* @callback OnSuiteFinish | ||
* @param {ReturnType<runTest>[]} results | ||
* @returns {void} | ||
*/ | ||
|
||
/** | ||
* Run a suite of test files and return a more friendly result format | ||
* | ||
* @param {string[]} absolutePaths | ||
* @param {Object} props | ||
* @param {OnTestFinish} props.onTestFinish | ||
* @param {OnSuiteFinish} props.onSuiteFinish | ||
* @returns {void} | ||
* | ||
* @example | ||
* runSuite(["/home/lorem/src/fn.test-d.ts"], { | ||
* onTestFinish: (result, index) => {}, | ||
* onSuiteDone: (results) => {}, | ||
* }) | ||
*/ | ||
export const runSuite = (absolutePaths, { onTestFinish, onSuiteFinish }) => { | ||
const results = absolutePaths.map((item, index) => { | ||
// TODO: async via worker threads maybe? | ||
const result = runTest(item) | ||
|
||
onTestFinish(result, index) | ||
|
||
return result | ||
}) | ||
|
||
onSuiteFinish(results) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.