github-desktop/script/jest-actions-reporter.js

68 lines
1.6 KiB
JavaScript
Raw Normal View History

2020-07-09 18:15:20 +00:00
const { resolve, relative } = require('path')
2020-07-09 17:28:28 +00:00
/* eslint-disable @typescript-eslint/explicit-member-accessibility */
class JestActionsReporter {
constructor(globalConfig, options) {
this._globalConfig = globalConfig
this._options = options
}
onRunComplete(contexts, results) {
2020-07-09 18:31:57 +00:00
if (process.env.GITHUB_ACTIONS !== 'true') {
2020-07-09 17:28:28 +00:00
return
}
2020-07-09 18:15:20 +00:00
const { rootDir } = this._globalConfig
const repoRoot = resolve(rootDir, '..')
2020-07-09 17:28:28 +00:00
for (const { testResults, testFilePath } of results.testResults) {
for (const { failureMessages, location } of testResults) {
if (location === null) {
continue
}
2020-07-09 18:15:20 +00:00
const path = relative(repoRoot, testFilePath)
2020-07-09 17:28:28 +00:00
for (const msg of failureMessages) {
const { line, column } =
tryGetFailureLocation(msg, testFilePath) || location
2020-07-09 17:28:28 +00:00
const escapedMessage = `${msg}`.replace(/\r?\n/g, '%0A')
process.stdout.write(
2020-07-09 18:15:20 +00:00
`::error file=${path},line=${line},col=${column}::${escapedMessage}\n`
)
2020-07-09 17:28:28 +00:00
}
}
}
}
}
function tryGetFailureLocation(message, testPath) {
for (const line of message.split(/\r?\n/g)) {
if (!/^\s+at\s/.test(line)) {
continue
}
const ix = line.indexOf(testPath)
if (ix < 0) {
continue
}
const locationRe = /:(\d+):(\d+)/
const remainder = line.substr(ix + testPath.length)
const match = locationRe.exec(remainder)
if (match) {
return {
line: parseInt(match[1], 10),
column: parseInt(match[2], 10),
}
}
}
return undefined
}
2020-07-09 17:28:28 +00:00
module.exports = JestActionsReporter