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
Changes from 21 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
Original file line number Diff line number Diff line change
@@ -112,7 +112,7 @@ describe( 'Inserting blocks', () => {

// Verify vertical traversal at offset. This has been buggy in the past
// where verticality on a blank newline would skip into previous block.
await page.keyboard.type( 'Foo' );
await page.keyboard.type( 'Bar' );
gziolo marked this conversation as resolved.
Show resolved Hide resolved
await page.keyboard.press( 'ArrowUp' );
await page.keyboard.press( 'ArrowUp' );
await pressKeyTimes( 'Delete', 6 );
4 changes: 4 additions & 0 deletions packages/scripts/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -2,6 +2,10 @@

## Unreleased

### New Features

- Include a Jest Reporter that formats test results for GitHub Actions annotations ([#31041](https://github.com/WordPress/gutenberg/pull/31041)).

### Breaking Changes

- Rename `format-js` script to `format` ([#30240](https://github.com/WordPress/gutenberg/pull/30240)).
4 changes: 4 additions & 0 deletions packages/scripts/config/jest-e2e.config.js
Original file line number Diff line number Diff line change
@@ -20,6 +20,10 @@ const jestE2EConfig = {
setupFilesAfterEnv: [ 'expect-puppeteer' ],
testMatch: [ '**/specs/**/*.[jt]s', '**/?(*.)spec.[jt]s' ],
testPathIgnorePatterns: [ '/node_modules/' ],
reporters: [
'default',
path.join( __dirname, 'jest-github-actions-reporter.js' ),
],
};

if ( ! hasBabelConfig() ) {
64 changes: 64 additions & 0 deletions packages/scripts/config/jest-github-actions-reporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Based on https://github.com/facebook/jest/pull/11320.
*
* We might be able to remove this file once the Jest PR is merged, and a
* version of Jest that includes the GithubActionsReporter is released.
*/

/**
* 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 GithubActionsReporter {
async onRunComplete( _contexts, _aggregatedResults ) {
if ( ! process.env.GITHUB_ACTIONS ) {
return;
}

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 !== 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 = GithubActionsReporter;
4 changes: 4 additions & 0 deletions packages/scripts/config/jest-unit.config.js
Original file line number Diff line number Diff line change
@@ -10,6 +10,10 @@ const { hasBabelConfig } = require( '../utils' );

const jestUnitConfig = {
preset: '@wordpress/jest-preset-default',
reporters: [
'default',
path.join( __dirname, 'jest-github-actions-reporter.js' ),
],
};

if ( ! hasBabelConfig() ) {
4 changes: 4 additions & 0 deletions test/unit/jest.config.js
Original file line number Diff line number Diff line change
@@ -43,4 +43,8 @@ module.exports = {
'jest-watch-typeahead/filename',
'jest-watch-typeahead/testname',
],
reporters: [
'default',
'<rootDir>packages/scripts/config/jest-github-actions-reporter.js',
],
};