Skip to content
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

feat: Added GithubActionsReporter #10741

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions packages/jest-reporters/src/GithubActionsReporter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import type {
AggregatedResult, TestResult
} from '@jest/test-result';
import BaseReporter from './BaseReporter';
import type { Context } from './types';

const newLine = /\n/g;
const encodedNewLine = "%0A";
const lineAndColumnInStackTrace = /^.*:([0-9]+):([0-9]+).*$/;

export default class GithubActionReporter extends BaseReporter {
onRunComplete(
_contexts?: Set<Context>,
aggregatedResults?: AggregatedResult) {
var messages = getMessages(aggregatedResults?.testResults);

for (const message of messages) {
console.log(message);
}
}
}

function getMessages(results: TestResult[] | undefined){
if(!results) return [];

return results
.reduce(flatMap(({ testFilePath, testResults }) => testResults
.filter(r => r.status === "failed")
.reduce(flatMap(r => r.failureMessages), [])
.map(m => m.replace(newLine, encodedNewLine))
.map(m => lineAndColumnInStackTrace.exec(m))
.filter((m): m is RegExpExecArray => m !== null)
.map(([message, line, col]) =>
`::error file=${testFilePath},line=${line},col=${col}::${message}`)),
[]);
}

function flatMap<In, Out>(map: (x: In) => Out[]) {
return (out: Out[], entry: In) => out.concat(...map(entry))
}
1 change: 1 addition & 0 deletions packages/jest-reporters/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export {default as DefaultReporter} from './DefaultReporter';
export {default as NotifyReporter} from './NotifyReporter';
export {default as SummaryReporter} from './SummaryReporter';
export {default as VerboseReporter} from './VerboseReporter';
export {default as GithubActionsReporter} from './GithubActionsReporter';
export type {
Context,
Reporter,
Expand Down