-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(@jest/reporters): improve annotation formatting of GitHubActionsReporter
#12826
Changes from all commits
d71c5c1
814db33
7c116a7
ae036a7
e6c5d54
300e7eb
e594bcb
fadd5f2
1022130
172a41e
f1a1e97
e95b5e5
ebd0bf8
7171272
cc02d0b
87fb028
74c3f74
8f60aa1
8e6e9ef
fe273cd
40b847d
a5ac9e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,54 +6,79 @@ | |
*/ | ||
|
||
import stripAnsi = require('strip-ansi'); | ||
import type { | ||
AggregatedResult, | ||
TestContext, | ||
TestResult, | ||
} from '@jest/test-result'; | ||
import type {Test, TestResult} from '@jest/test-result'; | ||
import type {Config} from '@jest/types'; | ||
import { | ||
formatPath, | ||
getStackTraceLines, | ||
getTopFrame, | ||
separateMessageFromStack, | ||
} from 'jest-message-util'; | ||
import BaseReporter from './BaseReporter'; | ||
|
||
const lineAndColumnInStackTrace = /^.*?:([0-9]+):([0-9]+).*$/; | ||
type AnnotationOptions = { | ||
file?: string; | ||
line?: number | string; | ||
message: string; | ||
title: string; | ||
type: 'error' | 'warning'; | ||
}; | ||
|
||
function replaceEntities(s: string): string { | ||
// https://github.com/actions/toolkit/blob/b4639928698a6bfe1c4bdae4b2bfdad1cb75016d/packages/core/src/command.ts#L80-L85 | ||
const substitutions: Array<[RegExp, string]> = [ | ||
[/%/g, '%25'], | ||
[/\r/g, '%0D'], | ||
[/\n/g, '%0A'], | ||
]; | ||
return substitutions.reduce((acc, sub) => acc.replace(...sub), s); | ||
} | ||
const titleSeparator = ' \u203A '; | ||
|
||
export default class GitHubActionsReporter extends BaseReporter { | ||
static readonly filename = __filename; | ||
|
||
override onRunComplete( | ||
_testContexts?: Set<TestContext>, | ||
aggregatedResults?: AggregatedResult, | ||
): void { | ||
const messages = getMessages(aggregatedResults?.testResults); | ||
onTestFileResult({context}: Test, {testResults}: TestResult): void { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
can you file an issue for it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have to figure out what is the root of that. Locally all was fine, but not on CI. Strange thing. Let’s see. |
||
testResults.forEach(result => { | ||
const title = [...result.ancestorTitles, result.title].join( | ||
titleSeparator, | ||
); | ||
|
||
result.retryReasons?.forEach((retryReason, index) => { | ||
this.#createAnnotation({ | ||
...this.#getMessageDetails(retryReason, context.config), | ||
title: `RETRY ${index + 1}: ${title}`, | ||
type: 'warning', | ||
}); | ||
}); | ||
|
||
for (const message of messages) { | ||
this.log(message); | ||
} | ||
result.failureMessages.forEach(failureMessage => { | ||
this.#createAnnotation({ | ||
...this.#getMessageDetails(failureMessage, context.config), | ||
title, | ||
type: 'error', | ||
}); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
function getMessages(results: Array<TestResult> | undefined) { | ||
if (!results) return []; | ||
|
||
return results.flatMap(({testFilePath, testResults}) => | ||
testResults | ||
.filter(r => r.status === 'failed') | ||
.flatMap(r => r.failureMessages) | ||
.map(m => stripAnsi(m)) | ||
.map(m => replaceEntities(m)) | ||
.map(m => lineAndColumnInStackTrace.exec(m)) | ||
.filter((m): m is RegExpExecArray => m !== null) | ||
.map( | ||
([message, line, col]) => | ||
`\n::error file=${testFilePath},line=${line},col=${col}::${message}`, | ||
), | ||
); | ||
#getMessageDetails(failureMessage: string, config: Config.ProjectConfig) { | ||
const {message, stack} = separateMessageFromStack(failureMessage); | ||
|
||
const stackLines = getStackTraceLines(stack); | ||
const topFrame = getTopFrame(stackLines); | ||
|
||
const normalizedStackLines = stackLines.map(line => | ||
formatPath(line, config), | ||
); | ||
const messageText = [message, ...normalizedStackLines].join('\n'); | ||
|
||
return { | ||
file: topFrame?.file, | ||
line: topFrame?.line, | ||
message: messageText, | ||
}; | ||
} | ||
|
||
#createAnnotation({file, line, message, title, type}: AnnotationOptions) { | ||
message = stripAnsi( | ||
// copied from: https://github.com/actions/toolkit/blob/main/packages/core/src/command.ts | ||
message.replace(/%/g, '%25').replace(/\r/g, '%0D').replace(/\n/g, '%0A'), | ||
); | ||
|
||
this.log( | ||
`\n::${type} file=${file},line=${line},title=${title}::${message}`, | ||
); | ||
} | ||
} |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps fine to have
null
as default, it just enables more styling.