forked from desktop/desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjest-actions-reporter.js
67 lines (53 loc) · 1.6 KB
/
jest-actions-reporter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const { resolve, relative } = require('path')
/* eslint-disable @typescript-eslint/explicit-member-accessibility */
class JestActionsReporter {
constructor(globalConfig, options) {
this._globalConfig = globalConfig
this._options = options
}
onRunComplete(contexts, results) {
if (process.env.GITHUB_ACTIONS !== 'true') {
return
}
const { rootDir } = this._globalConfig
const repoRoot = resolve(rootDir, '..')
for (const { testResults, testFilePath } of results.testResults) {
for (const { failureMessages, location } of testResults) {
if (location === null) {
continue
}
const path = relative(repoRoot, testFilePath)
for (const msg of failureMessages) {
const { line, column } =
tryGetFailureLocation(msg, testFilePath) || location
const escapedMessage = `${msg}`.replace(/\r?\n/g, '%0A')
process.stdout.write(
`::error file=${path},line=${line},col=${column}::${escapedMessage}\n`
)
}
}
}
}
}
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
}
module.exports = JestActionsReporter