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

Tests: Adds reporter that shows JS test errors on GitHub when executing GitHub workflows #31041

Merged
merged 22 commits into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from 10 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
53 changes: 53 additions & 0 deletions test/unit/GithubActionsReporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* 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.
*/

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

class GithubActionReporter {
async onRunComplete( _contexts, _aggregatedResults ) {
if ( ! _aggregatedResults ) {
return;
}
const messages = getMessages( _aggregatedResults.testResults );

for ( const message of messages ) {
// eslint-disable-next-line no-console
console.log( message );
}
}
}

function getMessages( results ) {
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( fn ) {
return ( out, entry ) => out.concat( ...fn( entry ) );
}

module.exports = GithubActionReporter;
1 change: 1 addition & 0 deletions test/unit/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ module.exports = {
'jest-watch-typeahead/filename',
'jest-watch-typeahead/testname',
],
reporters: [ 'default', '<rootDir>/test/unit/GithubActionsReporter.js' ],
};
5 changes: 5 additions & 0 deletions test/unit/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe( 'My test', () => {
test( 'A equals B', () => {
expect( 'a' ).toBe( 'b' );
} );
} );