From 4b441a106dedbdebc2eb89899d7f832e70e7d075 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 20 Apr 2021 23:21:07 +0200 Subject: [PATCH 01/22] Add GitHub Actions Reporter --- .../src/GithubActionsReporter.ts | 54 ++++++++ .../__tests__/GithubActionsReporter.test.js | 121 ++++++++++++++++++ .../GithubActionsReporter.test.js.snap | 6 + packages/jest-reporters/src/index.ts | 1 + 4 files changed, 182 insertions(+) create mode 100644 packages/jest-reporters/src/GithubActionsReporter.ts create mode 100644 packages/jest-reporters/src/__tests__/GithubActionsReporter.test.js create mode 100644 packages/jest-reporters/src/__tests__/__snapshots__/GithubActionsReporter.test.js.snap diff --git a/packages/jest-reporters/src/GithubActionsReporter.ts b/packages/jest-reporters/src/GithubActionsReporter.ts new file mode 100644 index 000000000000..6a93bb5725bc --- /dev/null +++ b/packages/jest-reporters/src/GithubActionsReporter.ts @@ -0,0 +1,54 @@ +/** + * 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, + aggregatedResults?: AggregatedResult, + ): void { + const messages = getMessages(aggregatedResults?.testResults); + + for (const message of messages) { + this.log(message); + } + } +} + +function getMessages(results: Array | 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(map: (x: In) => Array) { + return (out: Array, entry: In) => out.concat(...map(entry)); +} diff --git a/packages/jest-reporters/src/__tests__/GithubActionsReporter.test.js b/packages/jest-reporters/src/__tests__/GithubActionsReporter.test.js new file mode 100644 index 000000000000..1e4271ae5038 --- /dev/null +++ b/packages/jest-reporters/src/__tests__/GithubActionsReporter.test.js @@ -0,0 +1,121 @@ +/** + * 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. + */ +'use strict'; + +let GithubActionsReporter; + +const write = process.stderr.write; +const globalConfig = { + rootDir: 'root', + watch: false, +}; + +let results = []; + +function requireReporter() { + jest.isolateModules(() => { + GithubActionsReporter = require('../GithubActionsReporter').default; + }); +} + +beforeEach(() => { + process.stderr.write = result => results.push(result); +}); + +afterEach(() => { + results = []; + process.stderr.write = write; +}); + +test('snapshots all have results (no update)', () => { + const aggregatedResults = { + numFailedTestSuites: 1, + numFailedTests: 1, + numPassedTestSuites: 0, + numTotalTestSuites: 1, + numTotalTests: 1, + snapshot: { + added: 0, + didUpdate: false, + failure: false, + filesAdded: 0, + filesRemoved: 0, + filesRemovedList: [], + filesUnmatched: 0, + filesUpdated: 0, + matched: 0, + total: 0, + unchecked: 0, + uncheckedKeysByFile: [], + unmatched: 0, + updated: 0, + }, + startTime: 0, + success: false, + testResults: [ + { + numFailingTests: 1, + numPassingTests: 0, + numPendingTests: 0, + numTodoTests: 0, + openHandles: [], + perfStats: { + end: 1234, + runtime: 1234, + slow: false, + start: 0, + }, + skipped: false, + snapshot: { + added: 0, + fileDeleted: false, + matched: 0, + unchecked: 0, + uncheckedKeys: [], + unmatched: 0, + updated: 0, + }, + testFilePath: '/home/runner/work/jest/jest/some.test.js', + testResults: [ + { + ancestorTitles: [Array], + duration: 7, + failureDetails: [Array], + failureMessages: [ + ` + expect(received).toBe(expected) // Object.is equality + + Expected: "b" + Received: "a" + + 121 | + 122 | expect( 'a' ).toBe( 'a' ); + > 123 | expect( 'a' ).toBe( 'b' ); + | ^ + 124 | } ); + 125 | } ); + 126 | + + at Object. (/home/runner/work/jest/jest/some.test.js:123:17) + `, + ], + fullName: 'asserts that a === b', + location: null, + numPassingAsserts: 0, + status: 'failed', + title: 'asserts that a === b', + }, + ], + }, + ], + }; + + requireReporter(); + const testReporter = new GithubActionsReporter(globalConfig); + testReporter.onRunComplete(new Set(), aggregatedResults); + expect(results.join('').replace(/\\/g, '/')).toMatchSnapshot(); +}); diff --git a/packages/jest-reporters/src/__tests__/__snapshots__/GithubActionsReporter.test.js.snap b/packages/jest-reporters/src/__tests__/__snapshots__/GithubActionsReporter.test.js.snap new file mode 100644 index 000000000000..2299c4f69968 --- /dev/null +++ b/packages/jest-reporters/src/__tests__/__snapshots__/GithubActionsReporter.test.js.snap @@ -0,0 +1,6 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`snapshots all have results (no update) 1`] = ` +"::error file=/home/runner/work/jest/jest/some.test.js,line=123,col=17::%0A expect(received).toBe(expected) // Object.is equality%0A%0A Expected: \\"b\\"%0A Received: \\"a\\"%0A%0A 121 | %0A 122 | expect( 'a' ).toBe( 'a' );%0A > 123 | expect( 'a' ).toBe( 'b' );%0A | ^%0A 124 | } );%0A 125 | } );%0A 126 | %0A%0A at Object. (/home/runner/work/jest/jest/some.test.js:123:17)%0A +" +`; diff --git a/packages/jest-reporters/src/index.ts b/packages/jest-reporters/src/index.ts index 9185df007f1b..40f0ba0b92c9 100644 --- a/packages/jest-reporters/src/index.ts +++ b/packages/jest-reporters/src/index.ts @@ -26,6 +26,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, From 6b299e22454e2a4b60578954ec0f4dbf78eda53a Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 22 Apr 2021 17:27:46 +0200 Subject: [PATCH 02/22] Use plural to match filename --- packages/jest-reporters/src/GithubActionsReporter.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-reporters/src/GithubActionsReporter.ts b/packages/jest-reporters/src/GithubActionsReporter.ts index 6a93bb5725bc..30e00b62b260 100644 --- a/packages/jest-reporters/src/GithubActionsReporter.ts +++ b/packages/jest-reporters/src/GithubActionsReporter.ts @@ -13,7 +13,7 @@ const newLine = /\n/g; const encodedNewLine = '%0A'; const lineAndColumnInStackTrace = /^.*:([0-9]+):([0-9]+).*$/; -export default class GithubActionReporter extends BaseReporter { +export default class GithubActionsReporter extends BaseReporter { onRunComplete( _contexts?: Set, aggregatedResults?: AggregatedResult, From b6cb64bb53acbe1ea3951e884575c7cf8281050e Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Fri, 23 Apr 2021 14:06:56 +0200 Subject: [PATCH 03/22] More expressive test failure message --- .../__tests__/GithubActionsReporter.test.js | 25 ++++++++----------- .../GithubActionsReporter.test.js.snap | 2 +- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/packages/jest-reporters/src/__tests__/GithubActionsReporter.test.js b/packages/jest-reporters/src/__tests__/GithubActionsReporter.test.js index 1e4271ae5038..febb6ffc391e 100644 --- a/packages/jest-reporters/src/__tests__/GithubActionsReporter.test.js +++ b/packages/jest-reporters/src/__tests__/GithubActionsReporter.test.js @@ -87,20 +87,17 @@ test('snapshots all have results (no update)', () => { failureDetails: [Array], failureMessages: [ ` - expect(received).toBe(expected) // Object.is equality - - Expected: "b" - Received: "a" - - 121 | - 122 | expect( 'a' ).toBe( 'a' ); - > 123 | expect( 'a' ).toBe( 'b' ); - | ^ - 124 | } ); - 125 | } ); - 126 | - - at Object. (/home/runner/work/jest/jest/some.test.js:123:17) + Error: \u001b[2mexpect(\u001b[22m\u001b[31mreceived\u001b[39m\u001b[2m).\u001b[22mtoBe\u001b[2m(\u001b[22m\u001b[32mexpected\u001b[39m\u001b[2m) // Object.is equality\u001b[22m\n + \n + Expected: \u001b[32m\"b\"\u001b[39m\n + Received: \u001b[31m\"a\"\u001b[39m\n + at Object. (/home/runner/work/jest/jest/some.test.js:4:17)\n + at Object.asyncJestTest (/home/runner/work/jest/jest/node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:106:37)\n + at /home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:45:12\n + at new Promise ()\n + at mapper (/home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:28:19)\n + at /home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:75:41\n + at processTicksAndRejections (internal/process/task_queues.js:93:5) `, ], fullName: 'asserts that a === b', diff --git a/packages/jest-reporters/src/__tests__/__snapshots__/GithubActionsReporter.test.js.snap b/packages/jest-reporters/src/__tests__/__snapshots__/GithubActionsReporter.test.js.snap index 2299c4f69968..392af4ccdb7f 100644 --- a/packages/jest-reporters/src/__tests__/__snapshots__/GithubActionsReporter.test.js.snap +++ b/packages/jest-reporters/src/__tests__/__snapshots__/GithubActionsReporter.test.js.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`snapshots all have results (no update) 1`] = ` -"::error file=/home/runner/work/jest/jest/some.test.js,line=123,col=17::%0A expect(received).toBe(expected) // Object.is equality%0A%0A Expected: \\"b\\"%0A Received: \\"a\\"%0A%0A 121 | %0A 122 | expect( 'a' ).toBe( 'a' );%0A > 123 | expect( 'a' ).toBe( 'b' );%0A | ^%0A 124 | } );%0A 125 | } );%0A 126 | %0A%0A at Object. (/home/runner/work/jest/jest/some.test.js:123:17)%0A +"::error file=/home/runner/work/jest/jest/some.test.js,line=93,col=5::%0A Error: expect(received).toBe(expected) // Object.is equality%0A%0A %0A%0A Expected: \\"b\\"%0A%0A Received: \\"a\\"%0A%0A at Object. (/home/runner/work/jest/jest/some.test.js:4:17)%0A%0A at Object.asyncJestTest (/home/runner/work/jest/jest/node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:106:37)%0A%0A at /home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:45:12%0A%0A at new Promise ()%0A%0A at mapper (/home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:28:19)%0A%0A at /home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:75:41%0A%0A at processTicksAndRejections (internal/process/task_queues.js:93:5)%0A " `; From 48e2778cd9d2ed8d047fa144768e0bbb21990afd Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Fri, 23 Apr 2021 14:11:03 +0200 Subject: [PATCH 04/22] Fix line and col by making the RegEx less greedy --- packages/jest-reporters/src/GithubActionsReporter.ts | 2 +- .../__tests__/__snapshots__/GithubActionsReporter.test.js.snap | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/jest-reporters/src/GithubActionsReporter.ts b/packages/jest-reporters/src/GithubActionsReporter.ts index 30e00b62b260..41f54a12e5bf 100644 --- a/packages/jest-reporters/src/GithubActionsReporter.ts +++ b/packages/jest-reporters/src/GithubActionsReporter.ts @@ -11,7 +11,7 @@ import type {Context} from './types'; const newLine = /\n/g; const encodedNewLine = '%0A'; -const lineAndColumnInStackTrace = /^.*:([0-9]+):([0-9]+).*$/; +const lineAndColumnInStackTrace = /^.*?:([0-9]+):([0-9]+).*$/; export default class GithubActionsReporter extends BaseReporter { onRunComplete( diff --git a/packages/jest-reporters/src/__tests__/__snapshots__/GithubActionsReporter.test.js.snap b/packages/jest-reporters/src/__tests__/__snapshots__/GithubActionsReporter.test.js.snap index 392af4ccdb7f..2f05977f839c 100644 --- a/packages/jest-reporters/src/__tests__/__snapshots__/GithubActionsReporter.test.js.snap +++ b/packages/jest-reporters/src/__tests__/__snapshots__/GithubActionsReporter.test.js.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`snapshots all have results (no update) 1`] = ` -"::error file=/home/runner/work/jest/jest/some.test.js,line=93,col=5::%0A Error: expect(received).toBe(expected) // Object.is equality%0A%0A %0A%0A Expected: \\"b\\"%0A%0A Received: \\"a\\"%0A%0A at Object. (/home/runner/work/jest/jest/some.test.js:4:17)%0A%0A at Object.asyncJestTest (/home/runner/work/jest/jest/node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:106:37)%0A%0A at /home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:45:12%0A%0A at new Promise ()%0A%0A at mapper (/home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:28:19)%0A%0A at /home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:75:41%0A%0A at processTicksAndRejections (internal/process/task_queues.js:93:5)%0A +"::error file=/home/runner/work/jest/jest/some.test.js,line=4,col=17::%0A Error: expect(received).toBe(expected) // Object.is equality%0A%0A %0A%0A Expected: \\"b\\"%0A%0A Received: \\"a\\"%0A%0A at Object. (/home/runner/work/jest/jest/some.test.js:4:17)%0A%0A at Object.asyncJestTest (/home/runner/work/jest/jest/node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:106:37)%0A%0A at /home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:45:12%0A%0A at new Promise ()%0A%0A at mapper (/home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:28:19)%0A%0A at /home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:75:41%0A%0A at processTicksAndRejections (internal/process/task_queues.js:93:5)%0A " `; From 195062db3e2bc5c59819ffa7740548acedc15814 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Fri, 23 Apr 2021 14:24:21 +0200 Subject: [PATCH 05/22] Disable outside of GitHub Actions --- packages/jest-reporters/src/GithubActionsReporter.ts | 4 ++++ .../src/__tests__/GithubActionsReporter.test.js | 3 +++ 2 files changed, 7 insertions(+) diff --git a/packages/jest-reporters/src/GithubActionsReporter.ts b/packages/jest-reporters/src/GithubActionsReporter.ts index 41f54a12e5bf..8e103d2fd646 100644 --- a/packages/jest-reporters/src/GithubActionsReporter.ts +++ b/packages/jest-reporters/src/GithubActionsReporter.ts @@ -18,6 +18,10 @@ export default class GithubActionsReporter extends BaseReporter { _contexts?: Set, aggregatedResults?: AggregatedResult, ): void { + if (!process.env.GITHUB_ACTIONS) { + return; + } + const messages = getMessages(aggregatedResults?.testResults); for (const message of messages) { diff --git a/packages/jest-reporters/src/__tests__/GithubActionsReporter.test.js b/packages/jest-reporters/src/__tests__/GithubActionsReporter.test.js index febb6ffc391e..64ae9a4ebe90 100644 --- a/packages/jest-reporters/src/__tests__/GithubActionsReporter.test.js +++ b/packages/jest-reporters/src/__tests__/GithubActionsReporter.test.js @@ -8,6 +8,7 @@ let GithubActionsReporter; +const env = {...process.env}; const write = process.stderr.write; const globalConfig = { rootDir: 'root', @@ -23,11 +24,13 @@ function requireReporter() { } beforeEach(() => { + process.env.GITHUB_ACTIONS = true; process.stderr.write = result => results.push(result); }); afterEach(() => { results = []; + process.env = env; process.stderr.write = write; }); From 808bd5cf837d9d0dfe523d8db849fcd6107cee43 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Fri, 23 Apr 2021 14:25:34 +0200 Subject: [PATCH 06/22] Fix test description --- .../jest-reporters/src/__tests__/GithubActionsReporter.test.js | 2 +- .../__tests__/__snapshots__/GithubActionsReporter.test.js.snap | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/jest-reporters/src/__tests__/GithubActionsReporter.test.js b/packages/jest-reporters/src/__tests__/GithubActionsReporter.test.js index 64ae9a4ebe90..6e3bd62668f3 100644 --- a/packages/jest-reporters/src/__tests__/GithubActionsReporter.test.js +++ b/packages/jest-reporters/src/__tests__/GithubActionsReporter.test.js @@ -34,7 +34,7 @@ afterEach(() => { process.stderr.write = write; }); -test('snapshots all have results (no update)', () => { +test('reporter extracts the correct filename, line, and column', () => { const aggregatedResults = { numFailedTestSuites: 1, numFailedTests: 1, diff --git a/packages/jest-reporters/src/__tests__/__snapshots__/GithubActionsReporter.test.js.snap b/packages/jest-reporters/src/__tests__/__snapshots__/GithubActionsReporter.test.js.snap index 2f05977f839c..49a03c3774b7 100644 --- a/packages/jest-reporters/src/__tests__/__snapshots__/GithubActionsReporter.test.js.snap +++ b/packages/jest-reporters/src/__tests__/__snapshots__/GithubActionsReporter.test.js.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`snapshots all have results (no update) 1`] = ` +exports[`reporter extracts the correct filename, line, and column 1`] = ` "::error file=/home/runner/work/jest/jest/some.test.js,line=4,col=17::%0A Error: expect(received).toBe(expected) // Object.is equality%0A%0A %0A%0A Expected: \\"b\\"%0A%0A Received: \\"a\\"%0A%0A at Object. (/home/runner/work/jest/jest/some.test.js:4:17)%0A%0A at Object.asyncJestTest (/home/runner/work/jest/jest/node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:106:37)%0A%0A at /home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:45:12%0A%0A at new Promise ()%0A%0A at mapper (/home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:28:19)%0A%0A at /home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:75:41%0A%0A at processTicksAndRejections (internal/process/task_queues.js:93:5)%0A " `; From 5c4884f8df64dbeb321ef94f85d1812f29e8b890 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Fri, 23 Apr 2021 14:30:26 +0200 Subject: [PATCH 07/22] Add test case for non-GHA context --- .../__tests__/GithubActionsReporter.test.js | 165 +++++++++--------- .../GithubActionsReporter.test.js.snap | 4 +- 2 files changed, 89 insertions(+), 80 deletions(-) diff --git a/packages/jest-reporters/src/__tests__/GithubActionsReporter.test.js b/packages/jest-reporters/src/__tests__/GithubActionsReporter.test.js index 6e3bd62668f3..f71444cc8057 100644 --- a/packages/jest-reporters/src/__tests__/GithubActionsReporter.test.js +++ b/packages/jest-reporters/src/__tests__/GithubActionsReporter.test.js @@ -24,7 +24,6 @@ function requireReporter() { } beforeEach(() => { - process.env.GITHUB_ACTIONS = true; process.stderr.write = result => results.push(result); }); @@ -34,85 +33,93 @@ afterEach(() => { process.stderr.write = write; }); -test('reporter extracts the correct filename, line, and column', () => { - const aggregatedResults = { - numFailedTestSuites: 1, - numFailedTests: 1, - numPassedTestSuites: 0, - numTotalTestSuites: 1, - numTotalTests: 1, - snapshot: { - added: 0, - didUpdate: false, - failure: false, - filesAdded: 0, - filesRemoved: 0, - filesRemovedList: [], - filesUnmatched: 0, - filesUpdated: 0, - matched: 0, - total: 0, - unchecked: 0, - uncheckedKeysByFile: [], - unmatched: 0, - updated: 0, - }, - startTime: 0, - success: false, - testResults: [ - { - numFailingTests: 1, - numPassingTests: 0, - numPendingTests: 0, - numTodoTests: 0, - openHandles: [], - perfStats: { - end: 1234, - runtime: 1234, - slow: false, - start: 0, - }, - skipped: false, - snapshot: { - added: 0, - fileDeleted: false, - matched: 0, - unchecked: 0, - uncheckedKeys: [], - unmatched: 0, - updated: 0, - }, - testFilePath: '/home/runner/work/jest/jest/some.test.js', - testResults: [ - { - ancestorTitles: [Array], - duration: 7, - failureDetails: [Array], - failureMessages: [ - ` - Error: \u001b[2mexpect(\u001b[22m\u001b[31mreceived\u001b[39m\u001b[2m).\u001b[22mtoBe\u001b[2m(\u001b[22m\u001b[32mexpected\u001b[39m\u001b[2m) // Object.is equality\u001b[22m\n - \n - Expected: \u001b[32m\"b\"\u001b[39m\n - Received: \u001b[31m\"a\"\u001b[39m\n - at Object. (/home/runner/work/jest/jest/some.test.js:4:17)\n - at Object.asyncJestTest (/home/runner/work/jest/jest/node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:106:37)\n - at /home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:45:12\n - at new Promise ()\n - at mapper (/home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:28:19)\n - at /home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:75:41\n - at processTicksAndRejections (internal/process/task_queues.js:93:5) - `, - ], - fullName: 'asserts that a === b', - location: null, - numPassingAsserts: 0, - status: 'failed', - title: 'asserts that a === b', - }, - ], +const aggregatedResults = { + numFailedTestSuites: 1, + numFailedTests: 1, + numPassedTestSuites: 0, + numTotalTestSuites: 1, + numTotalTests: 1, + snapshot: { + added: 0, + didUpdate: false, + failure: false, + filesAdded: 0, + filesRemoved: 0, + filesRemovedList: [], + filesUnmatched: 0, + filesUpdated: 0, + matched: 0, + total: 0, + unchecked: 0, + uncheckedKeysByFile: [], + unmatched: 0, + updated: 0, + }, + startTime: 0, + success: false, + testResults: [ + { + numFailingTests: 1, + numPassingTests: 0, + numPendingTests: 0, + numTodoTests: 0, + openHandles: [], + perfStats: { + end: 1234, + runtime: 1234, + slow: false, + start: 0, }, - ], - }; + skipped: false, + snapshot: { + added: 0, + fileDeleted: false, + matched: 0, + unchecked: 0, + uncheckedKeys: [], + unmatched: 0, + updated: 0, + }, + testFilePath: '/home/runner/work/jest/jest/some.test.js', + testResults: [ + { + ancestorTitles: [Array], + duration: 7, + failureDetails: [Array], + failureMessages: [ + ` + Error: \u001b[2mexpect(\u001b[22m\u001b[31mreceived\u001b[39m\u001b[2m).\u001b[22mtoBe\u001b[2m(\u001b[22m\u001b[32mexpected\u001b[39m\u001b[2m) // Object.is equality\u001b[22m\n + \n + Expected: \u001b[32m\"b\"\u001b[39m\n + Received: \u001b[31m\"a\"\u001b[39m\n + at Object. (/home/runner/work/jest/jest/some.test.js:4:17)\n + at Object.asyncJestTest (/home/runner/work/jest/jest/node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:106:37)\n + at /home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:45:12\n + at new Promise ()\n + at mapper (/home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:28:19)\n + at /home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:75:41\n + at processTicksAndRejections (internal/process/task_queues.js:93:5) + `, + ], + fullName: 'asserts that a === b', + location: null, + numPassingAsserts: 0, + status: 'failed', + title: 'asserts that a === b', + }, + ], + }, + ], +}; + +test("reporter returns empty string if GITHUB_ACTIONS isn't set", () => { + requireReporter(); + const testReporter = new GithubActionsReporter(globalConfig); + testReporter.onRunComplete(new Set(), aggregatedResults); + expect(results.join('').replace(/\\/g, '/')).toMatchSnapshot(); +}); +test('reporter extracts the correct filename, line, and column', () => { + process.env.GITHUB_ACTIONS = true; requireReporter(); const testReporter = new GithubActionsReporter(globalConfig); diff --git a/packages/jest-reporters/src/__tests__/__snapshots__/GithubActionsReporter.test.js.snap b/packages/jest-reporters/src/__tests__/__snapshots__/GithubActionsReporter.test.js.snap index 49a03c3774b7..cc7a475ffd7f 100644 --- a/packages/jest-reporters/src/__tests__/__snapshots__/GithubActionsReporter.test.js.snap +++ b/packages/jest-reporters/src/__tests__/__snapshots__/GithubActionsReporter.test.js.snap @@ -1,6 +1,8 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`reporter returns empty string if GITHUB_ACTIONS isn't set 1`] = `""`; + exports[`reporter extracts the correct filename, line, and column 1`] = ` -"::error file=/home/runner/work/jest/jest/some.test.js,line=4,col=17::%0A Error: expect(received).toBe(expected) // Object.is equality%0A%0A %0A%0A Expected: \\"b\\"%0A%0A Received: \\"a\\"%0A%0A at Object. (/home/runner/work/jest/jest/some.test.js:4:17)%0A%0A at Object.asyncJestTest (/home/runner/work/jest/jest/node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:106:37)%0A%0A at /home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:45:12%0A%0A at new Promise ()%0A%0A at mapper (/home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:28:19)%0A%0A at /home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:75:41%0A%0A at processTicksAndRejections (internal/process/task_queues.js:93:5)%0A +"::error file=/home/runner/work/jest/jest/some.test.js,line=4,col=17::%0A Error: expect(received).toBe(expected) // Object.is equality%0A%0A %0A%0A Expected: \\"b\\"%0A%0A Received: \\"a\\"%0A%0A at Object. (/home/runner/work/jest/jest/some.test.js:4:17)%0A%0A at Object.asyncJestTest (/home/runner/work/jest/jest/node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:106:37)%0A%0A at /home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:45:12%0A%0A at new Promise ()%0A%0A at mapper (/home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:28:19)%0A%0A at /home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:75:41%0A%0A at processTicksAndRejections (internal/process/task_queues.js:93:5)%0A " `; From 97a7b1ce88cfe79f4456b8f4ccfaa652c8ce990c Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Fri, 23 Apr 2021 14:32:05 +0200 Subject: [PATCH 08/22] Whitespace --- .../jest-reporters/src/__tests__/GithubActionsReporter.test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/jest-reporters/src/__tests__/GithubActionsReporter.test.js b/packages/jest-reporters/src/__tests__/GithubActionsReporter.test.js index f71444cc8057..2103be40c42c 100644 --- a/packages/jest-reporters/src/__tests__/GithubActionsReporter.test.js +++ b/packages/jest-reporters/src/__tests__/GithubActionsReporter.test.js @@ -118,6 +118,7 @@ test("reporter returns empty string if GITHUB_ACTIONS isn't set", () => { testReporter.onRunComplete(new Set(), aggregatedResults); expect(results.join('').replace(/\\/g, '/')).toMatchSnapshot(); }); + test('reporter extracts the correct filename, line, and column', () => { process.env.GITHUB_ACTIONS = true; From fc6dd305acf4402ddb2abd21de907deb1ff5a828 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Fri, 25 Feb 2022 12:13:40 +0000 Subject: [PATCH 09/22] Use ci-info to check for GHA --- packages/jest-reporters/package.json | 1 + packages/jest-reporters/src/GithubActionsReporter.ts | 3 ++- .../src/__tests__/GithubActionsReporter.test.js | 4 +--- yarn.lock | 1 + 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/jest-reporters/package.json b/packages/jest-reporters/package.json index 0b07c2734776..b50f92f1fd4b 100644 --- a/packages/jest-reporters/package.json +++ b/packages/jest-reporters/package.json @@ -19,6 +19,7 @@ "@jest/types": "^28.0.0-alpha.5", "@types/node": "*", "chalk": "^4.0.0", + "ci-info": "^3.2.0", "collect-v8-coverage": "^1.0.0", "exit": "^0.1.2", "glob": "^7.1.2", diff --git a/packages/jest-reporters/src/GithubActionsReporter.ts b/packages/jest-reporters/src/GithubActionsReporter.ts index 8e103d2fd646..9a60b884ebad 100644 --- a/packages/jest-reporters/src/GithubActionsReporter.ts +++ b/packages/jest-reporters/src/GithubActionsReporter.ts @@ -5,6 +5,7 @@ * LICENSE file in the root directory of this source tree. */ +import {GITHUB_ACTIONS} from 'ci-info'; import type {AggregatedResult, TestResult} from '@jest/test-result'; import BaseReporter from './BaseReporter'; import type {Context} from './types'; @@ -18,7 +19,7 @@ export default class GithubActionsReporter extends BaseReporter { _contexts?: Set, aggregatedResults?: AggregatedResult, ): void { - if (!process.env.GITHUB_ACTIONS) { + if (!GITHUB_ACTIONS) { return; } diff --git a/packages/jest-reporters/src/__tests__/GithubActionsReporter.test.js b/packages/jest-reporters/src/__tests__/GithubActionsReporter.test.js index 2103be40c42c..253742b7155f 100644 --- a/packages/jest-reporters/src/__tests__/GithubActionsReporter.test.js +++ b/packages/jest-reporters/src/__tests__/GithubActionsReporter.test.js @@ -8,7 +8,6 @@ let GithubActionsReporter; -const env = {...process.env}; const write = process.stderr.write; const globalConfig = { rootDir: 'root', @@ -29,7 +28,6 @@ beforeEach(() => { afterEach(() => { results = []; - process.env = env; process.stderr.write = write; }); @@ -120,7 +118,7 @@ test("reporter returns empty string if GITHUB_ACTIONS isn't set", () => { }); test('reporter extracts the correct filename, line, and column', () => { - process.env.GITHUB_ACTIONS = true; + jest.doMock('ci-info', () => ({GITHUB_ACTIONS: true})); requireReporter(); const testReporter = new GithubActionsReporter(globalConfig); diff --git a/yarn.lock b/yarn.lock index b4f06b4b9a56..fdfa5e991092 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2743,6 +2743,7 @@ __metadata: "@types/node": "*" "@types/node-notifier": ^8.0.0 chalk: ^4.0.0 + ci-info: ^3.2.0 collect-v8-coverage: ^1.0.0 exit: ^0.1.2 glob: ^7.1.2 From 6289d6af2dc8ca789045a36ceedd829915002fac Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Sat, 26 Feb 2022 12:46:44 +0000 Subject: [PATCH 10/22] Escape all 3 chars that require it --- .../jest-reporters/src/GithubActionsReporter.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/jest-reporters/src/GithubActionsReporter.ts b/packages/jest-reporters/src/GithubActionsReporter.ts index 9a60b884ebad..36489759a16e 100644 --- a/packages/jest-reporters/src/GithubActionsReporter.ts +++ b/packages/jest-reporters/src/GithubActionsReporter.ts @@ -10,10 +10,17 @@ 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]+).*$/; +function replaceEntities(s: string): string { + const substitutions: Array<[RegExp, string]> = [ + [/%/, '%25'], + [/\r/g, '%0D'], + [/\n/g, '%0A'], + ]; + return substitutions.reduce((acc, sub) => acc.replace(...sub), s); +} + export default class GithubActionsReporter extends BaseReporter { onRunComplete( _contexts?: Set, @@ -42,7 +49,7 @@ function getMessages(results: Array | undefined) { flatMap(r => r.failureMessages), [], ) - .map(m => m.replace(newLine, encodedNewLine)) + .map(m => replaceEntities(m)) .map(m => lineAndColumnInStackTrace.exec(m)) .filter((m): m is RegExpExecArray => m !== null) .map( From 5ab275e4c2e19e444d9c298445948c345cd796d9 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Sat, 26 Feb 2022 12:48:19 +0000 Subject: [PATCH 11/22] Update snapshot --- .../__snapshots__/GithubActionsReporter.test.js.snap | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/jest-reporters/src/__tests__/__snapshots__/GithubActionsReporter.test.js.snap b/packages/jest-reporters/src/__tests__/__snapshots__/GithubActionsReporter.test.js.snap index cc7a475ffd7f..75bbbe693664 100644 --- a/packages/jest-reporters/src/__tests__/__snapshots__/GithubActionsReporter.test.js.snap +++ b/packages/jest-reporters/src/__tests__/__snapshots__/GithubActionsReporter.test.js.snap @@ -1,8 +1,8 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`reporter returns empty string if GITHUB_ACTIONS isn't set 1`] = `""`; - exports[`reporter extracts the correct filename, line, and column 1`] = ` -"::error file=/home/runner/work/jest/jest/some.test.js,line=4,col=17::%0A Error: expect(received).toBe(expected) // Object.is equality%0A%0A %0A%0A Expected: \\"b\\"%0A%0A Received: \\"a\\"%0A%0A at Object. (/home/runner/work/jest/jest/some.test.js:4:17)%0A%0A at Object.asyncJestTest (/home/runner/work/jest/jest/node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:106:37)%0A%0A at /home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:45:12%0A%0A at new Promise ()%0A%0A at mapper (/home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:28:19)%0A%0A at /home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:75:41%0A%0A at processTicksAndRejections (internal/process/task_queues.js:93:5)%0A +"::error file=/home/runner/work/jest/jest/some.test.js,line=4,col=17::%0A Error: expect(received).toBe(expected) // Object.is equality%0A%0A %0A%0A Expected: "b"%0A%0A Received: "a"%0A%0A at Object. (/home/runner/work/jest/jest/some.test.js:4:17)%0A%0A at Object.asyncJestTest (/home/runner/work/jest/jest/node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:106:37)%0A%0A at /home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:45:12%0A%0A at new Promise ()%0A%0A at mapper (/home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:28:19)%0A%0A at /home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:75:41%0A%0A at processTicksAndRejections (internal/process/task_queues.js:93:5)%0A " `; + +exports[`reporter returns empty string if GITHUB_ACTIONS isn't set 1`] = `""`; From ec6eb0d7d3fef8c37519c71c9bde7f5bf79aa945 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Sat, 26 Feb 2022 12:53:23 +0000 Subject: [PATCH 12/22] Add Changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7af1fa88d4be..a65a97589c73 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ - `[jest-mock]` [**BREAKING**] Rename exported utility types `ConstructorLike`, `MethodLike`, `ConstructorLikeKeys`, `MethodLikeKeys`, `PropertyLikeKeys`; remove exports of utility types `ArgumentsOf`, `ArgsType`, `ConstructorArgumentsOf` - TS builtin utility types `ConstructorParameters` and `Parameters` should be used instead ([#12435](https://github.com/facebook/jest/pull/12435)) - `[jest-mock]` Improve `isMockFunction` to infer types of passed function ([#12442](https://github.com/facebook/jest/pull/12442)) - `[jest-mock]` Add support for auto-mocking async generator functions ([#11080](https://github.com/facebook/jest/pull/11080)) +- `[jest-reporters]` Add GitHub Actions reporter ([#11320](https://github.com/facebook/jest/pull/11320)) - `[jest-resolve]` [**BREAKING**] Add support for `package.json` `exports` ([#11961](https://github.com/facebook/jest/pull/11961), [#12373](https://github.com/facebook/jest/pull/12373)) - `[jest-resolve, jest-runtime]` Add support for `data:` URI import and mock ([#12392](https://github.com/facebook/jest/pull/12392)) - `[jest-resolve, jest-runtime]` Add support for async resolver ([#11540](https://github.com/facebook/jest/pull/11540)) From a35a0d98f7ab101b92f48e067bfb5bb9ef866441 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 7 Mar 2022 14:34:35 +0100 Subject: [PATCH 13/22] Add missing modifier Co-authored-by: Brian Surowiec --- packages/jest-reporters/src/GithubActionsReporter.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-reporters/src/GithubActionsReporter.ts b/packages/jest-reporters/src/GithubActionsReporter.ts index 36489759a16e..94a18789c388 100644 --- a/packages/jest-reporters/src/GithubActionsReporter.ts +++ b/packages/jest-reporters/src/GithubActionsReporter.ts @@ -14,7 +14,7 @@ const lineAndColumnInStackTrace = /^.*?:([0-9]+):([0-9]+).*$/; function replaceEntities(s: string): string { const substitutions: Array<[RegExp, string]> = [ - [/%/, '%25'], + [/%/g, '%25'], [/\r/g, '%0D'], [/\n/g, '%0A'], ]; From e72e4475a188dc650fe96ba08b43a8f4e8392f11 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 7 Mar 2022 14:43:49 +0100 Subject: [PATCH 14/22] No need for snapshot Co-authored-by: Simen Bekkhus --- .../jest-reporters/src/__tests__/GithubActionsReporter.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-reporters/src/__tests__/GithubActionsReporter.test.js b/packages/jest-reporters/src/__tests__/GithubActionsReporter.test.js index 253742b7155f..ab101572ff11 100644 --- a/packages/jest-reporters/src/__tests__/GithubActionsReporter.test.js +++ b/packages/jest-reporters/src/__tests__/GithubActionsReporter.test.js @@ -114,7 +114,7 @@ test("reporter returns empty string if GITHUB_ACTIONS isn't set", () => { requireReporter(); const testReporter = new GithubActionsReporter(globalConfig); testReporter.onRunComplete(new Set(), aggregatedResults); - expect(results.join('').replace(/\\/g, '/')).toMatchSnapshot(); + expect(results.join('')).toBe(''); }); test('reporter extracts the correct filename, line, and column', () => { From 20a65629a815fc8ac156724af27371e4783e77d7 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 7 Mar 2022 14:45:47 +0100 Subject: [PATCH 15/22] Remove now-obsolete snapshot --- .../__tests__/__snapshots__/GithubActionsReporter.test.js.snap | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/jest-reporters/src/__tests__/__snapshots__/GithubActionsReporter.test.js.snap b/packages/jest-reporters/src/__tests__/__snapshots__/GithubActionsReporter.test.js.snap index 75bbbe693664..d70454f79f8a 100644 --- a/packages/jest-reporters/src/__tests__/__snapshots__/GithubActionsReporter.test.js.snap +++ b/packages/jest-reporters/src/__tests__/__snapshots__/GithubActionsReporter.test.js.snap @@ -4,5 +4,3 @@ exports[`reporter extracts the correct filename, line, and column 1`] = ` "::error file=/home/runner/work/jest/jest/some.test.js,line=4,col=17::%0A Error: expect(received).toBe(expected) // Object.is equality%0A%0A %0A%0A Expected: "b"%0A%0A Received: "a"%0A%0A at Object. (/home/runner/work/jest/jest/some.test.js:4:17)%0A%0A at Object.asyncJestTest (/home/runner/work/jest/jest/node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:106:37)%0A%0A at /home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:45:12%0A%0A at new Promise ()%0A%0A at mapper (/home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:28:19)%0A%0A at /home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:75:41%0A%0A at processTicksAndRejections (internal/process/task_queues.js:93:5)%0A " `; - -exports[`reporter returns empty string if GITHUB_ACTIONS isn't set 1`] = `""`; From d8c045481e61d5f7510ff8787b01d31426a1ee58 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 7 Mar 2022 14:46:58 +0100 Subject: [PATCH 16/22] Capitalize H in GithubActionsReporter Co-authored-by: Simen Bekkhus --- packages/jest-reporters/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-reporters/src/index.ts b/packages/jest-reporters/src/index.ts index 40f0ba0b92c9..3f8089c3b689 100644 --- a/packages/jest-reporters/src/index.ts +++ b/packages/jest-reporters/src/index.ts @@ -26,7 +26,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 {default as GitHubActionsReporter} from './GitHubActionsReporter'; export type { Context, Reporter, From c7c4ac08bd7b061506f7f0d6a0e1d7146c7a6e26 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 7 Mar 2022 14:49:47 +0100 Subject: [PATCH 17/22] More capital H's in GitHub --- ...{GithubActionsReporter.ts => GitHubActionsReporter.ts} | 2 +- ...ionsReporter.test.js => GitHubActionsReporter.test.js} | 8 ++++---- ...er.test.js.snap => GitHubActionsReporter.test.js.snap} | 0 3 files changed, 5 insertions(+), 5 deletions(-) rename packages/jest-reporters/src/{GithubActionsReporter.ts => GitHubActionsReporter.ts} (96%) rename packages/jest-reporters/src/__tests__/{GithubActionsReporter.test.js => GitHubActionsReporter.test.js} (94%) rename packages/jest-reporters/src/__tests__/__snapshots__/{GithubActionsReporter.test.js.snap => GitHubActionsReporter.test.js.snap} (100%) diff --git a/packages/jest-reporters/src/GithubActionsReporter.ts b/packages/jest-reporters/src/GitHubActionsReporter.ts similarity index 96% rename from packages/jest-reporters/src/GithubActionsReporter.ts rename to packages/jest-reporters/src/GitHubActionsReporter.ts index 94a18789c388..b3c51347dbf5 100644 --- a/packages/jest-reporters/src/GithubActionsReporter.ts +++ b/packages/jest-reporters/src/GitHubActionsReporter.ts @@ -21,7 +21,7 @@ function replaceEntities(s: string): string { return substitutions.reduce((acc, sub) => acc.replace(...sub), s); } -export default class GithubActionsReporter extends BaseReporter { +export default class GitHubActionsReporter extends BaseReporter { onRunComplete( _contexts?: Set, aggregatedResults?: AggregatedResult, diff --git a/packages/jest-reporters/src/__tests__/GithubActionsReporter.test.js b/packages/jest-reporters/src/__tests__/GitHubActionsReporter.test.js similarity index 94% rename from packages/jest-reporters/src/__tests__/GithubActionsReporter.test.js rename to packages/jest-reporters/src/__tests__/GitHubActionsReporter.test.js index ab101572ff11..ab3a288903ed 100644 --- a/packages/jest-reporters/src/__tests__/GithubActionsReporter.test.js +++ b/packages/jest-reporters/src/__tests__/GitHubActionsReporter.test.js @@ -6,7 +6,7 @@ */ 'use strict'; -let GithubActionsReporter; +let GitHubActionsReporter; const write = process.stderr.write; const globalConfig = { @@ -18,7 +18,7 @@ let results = []; function requireReporter() { jest.isolateModules(() => { - GithubActionsReporter = require('../GithubActionsReporter').default; + GitHubActionsReporter = require('../GitHubActionsReporter').default; }); } @@ -112,7 +112,7 @@ const aggregatedResults = { test("reporter returns empty string if GITHUB_ACTIONS isn't set", () => { requireReporter(); - const testReporter = new GithubActionsReporter(globalConfig); + const testReporter = new GitHubActionsReporter(globalConfig); testReporter.onRunComplete(new Set(), aggregatedResults); expect(results.join('')).toBe(''); }); @@ -121,7 +121,7 @@ test('reporter extracts the correct filename, line, and column', () => { jest.doMock('ci-info', () => ({GITHUB_ACTIONS: true})); requireReporter(); - const testReporter = new GithubActionsReporter(globalConfig); + const testReporter = new GitHubActionsReporter(globalConfig); testReporter.onRunComplete(new Set(), aggregatedResults); expect(results.join('').replace(/\\/g, '/')).toMatchSnapshot(); }); diff --git a/packages/jest-reporters/src/__tests__/__snapshots__/GithubActionsReporter.test.js.snap b/packages/jest-reporters/src/__tests__/__snapshots__/GitHubActionsReporter.test.js.snap similarity index 100% rename from packages/jest-reporters/src/__tests__/__snapshots__/GithubActionsReporter.test.js.snap rename to packages/jest-reporters/src/__tests__/__snapshots__/GitHubActionsReporter.test.js.snap From bb4bbe64fc988f8914b991684d81e31cc022079c Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 7 Mar 2022 15:28:12 +0100 Subject: [PATCH 18/22] Use Node's flatMap --- .../src/GitHubActionsReporter.ts | 32 +++++++------------ 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/packages/jest-reporters/src/GitHubActionsReporter.ts b/packages/jest-reporters/src/GitHubActionsReporter.ts index b3c51347dbf5..ea5246e306de 100644 --- a/packages/jest-reporters/src/GitHubActionsReporter.ts +++ b/packages/jest-reporters/src/GitHubActionsReporter.ts @@ -41,26 +41,16 @@ export default class GitHubActionsReporter extends BaseReporter { function getMessages(results: Array | undefined) { if (!results) return []; - return results.reduce( - flatMap(({testFilePath, testResults}) => - testResults - .filter(r => r.status === 'failed') - .reduce( - flatMap(r => r.failureMessages), - [], - ) - .map(m => replaceEntities(m)) - .map(m => lineAndColumnInStackTrace.exec(m)) - .filter((m): m is RegExpExecArray => m !== null) - .map( - ([message, line, col]) => - `::error file=${testFilePath},line=${line},col=${col}::${message}`, - ), - ), - [], + return results.flatMap(({testFilePath, testResults}) => + testResults + .filter(r => r.status === 'failed') + .flatMap(r => r.failureMessages) + .map(m => replaceEntities(m)) + .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(map: (x: In) => Array) { - return (out: Array, entry: In) => out.concat(...map(entry)); -} From e4d5c018da256fe9bdb4e16c1a9fdd369cee681c Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 28 Mar 2022 16:41:11 +0200 Subject: [PATCH 19/22] Drop ci-info check for GHA --- packages/jest-reporters/package.json | 1 - packages/jest-reporters/src/GitHubActionsReporter.ts | 5 ----- yarn.lock | 1 - 3 files changed, 7 deletions(-) diff --git a/packages/jest-reporters/package.json b/packages/jest-reporters/package.json index b50f92f1fd4b..0b07c2734776 100644 --- a/packages/jest-reporters/package.json +++ b/packages/jest-reporters/package.json @@ -19,7 +19,6 @@ "@jest/types": "^28.0.0-alpha.5", "@types/node": "*", "chalk": "^4.0.0", - "ci-info": "^3.2.0", "collect-v8-coverage": "^1.0.0", "exit": "^0.1.2", "glob": "^7.1.2", diff --git a/packages/jest-reporters/src/GitHubActionsReporter.ts b/packages/jest-reporters/src/GitHubActionsReporter.ts index ea5246e306de..2eeb2b366aaf 100644 --- a/packages/jest-reporters/src/GitHubActionsReporter.ts +++ b/packages/jest-reporters/src/GitHubActionsReporter.ts @@ -5,7 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -import {GITHUB_ACTIONS} from 'ci-info'; import type {AggregatedResult, TestResult} from '@jest/test-result'; import BaseReporter from './BaseReporter'; import type {Context} from './types'; @@ -26,10 +25,6 @@ export default class GitHubActionsReporter extends BaseReporter { _contexts?: Set, aggregatedResults?: AggregatedResult, ): void { - if (!GITHUB_ACTIONS) { - return; - } - const messages = getMessages(aggregatedResults?.testResults); for (const message of messages) { diff --git a/yarn.lock b/yarn.lock index fdfa5e991092..b4f06b4b9a56 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2743,7 +2743,6 @@ __metadata: "@types/node": "*" "@types/node-notifier": ^8.0.0 chalk: ^4.0.0 - ci-info: ^3.2.0 collect-v8-coverage: ^1.0.0 exit: ^0.1.2 glob: ^7.1.2 From 1ae84fbcbfc8b863f1aaff55e73792c9915564cb Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 28 Mar 2022 17:04:06 +0200 Subject: [PATCH 20/22] Strip ANSI codes --- packages/jest-reporters/src/GitHubActionsReporter.ts | 2 ++ .../__tests__/__snapshots__/GitHubActionsReporter.test.js.snap | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/jest-reporters/src/GitHubActionsReporter.ts b/packages/jest-reporters/src/GitHubActionsReporter.ts index 2eeb2b366aaf..ea3c1d6ec31a 100644 --- a/packages/jest-reporters/src/GitHubActionsReporter.ts +++ b/packages/jest-reporters/src/GitHubActionsReporter.ts @@ -5,6 +5,7 @@ * LICENSE file in the root directory of this source tree. */ +import stripAnsi = require('strip-ansi'); import type {AggregatedResult, TestResult} from '@jest/test-result'; import BaseReporter from './BaseReporter'; import type {Context} from './types'; @@ -40,6 +41,7 @@ function getMessages(results: Array | undefined) { 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) diff --git a/packages/jest-reporters/src/__tests__/__snapshots__/GitHubActionsReporter.test.js.snap b/packages/jest-reporters/src/__tests__/__snapshots__/GitHubActionsReporter.test.js.snap index d70454f79f8a..c523c4dd3448 100644 --- a/packages/jest-reporters/src/__tests__/__snapshots__/GitHubActionsReporter.test.js.snap +++ b/packages/jest-reporters/src/__tests__/__snapshots__/GitHubActionsReporter.test.js.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`reporter extracts the correct filename, line, and column 1`] = ` -"::error file=/home/runner/work/jest/jest/some.test.js,line=4,col=17::%0A Error: expect(received).toBe(expected) // Object.is equality%0A%0A %0A%0A Expected: "b"%0A%0A Received: "a"%0A%0A at Object. (/home/runner/work/jest/jest/some.test.js:4:17)%0A%0A at Object.asyncJestTest (/home/runner/work/jest/jest/node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:106:37)%0A%0A at /home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:45:12%0A%0A at new Promise ()%0A%0A at mapper (/home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:28:19)%0A%0A at /home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:75:41%0A%0A at processTicksAndRejections (internal/process/task_queues.js:93:5)%0A +"::error file=/home/runner/work/jest/jest/some.test.js,line=4,col=17::%0A Error: expect(received).toBe(expected) // Object.is equality%0A%0A %0A%0A Expected: "b"%0A%0A Received: "a"%0A%0A at Object. (/home/runner/work/jest/jest/some.test.js:4:17)%0A%0A at Object.asyncJestTest (/home/runner/work/jest/jest/node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:106:37)%0A%0A at /home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:45:12%0A%0A at new Promise ()%0A%0A at mapper (/home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:28:19)%0A%0A at /home/runner/work/jest/jest/node_modules/jest-jasmine2/build/queueRunner.js:75:41%0A%0A at processTicksAndRejections (internal/process/task_queues.js:93:5)%0A " `; From 8b5414f10d386bda67f17ad4f6f3be9641164dc2 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 28 Mar 2022 17:39:13 +0200 Subject: [PATCH 21/22] Update test to reflect removal of GHA check --- .../src/__tests__/GitHubActionsReporter.test.js | 9 --------- 1 file changed, 9 deletions(-) diff --git a/packages/jest-reporters/src/__tests__/GitHubActionsReporter.test.js b/packages/jest-reporters/src/__tests__/GitHubActionsReporter.test.js index ab3a288903ed..b7939b579458 100644 --- a/packages/jest-reporters/src/__tests__/GitHubActionsReporter.test.js +++ b/packages/jest-reporters/src/__tests__/GitHubActionsReporter.test.js @@ -110,16 +110,7 @@ const aggregatedResults = { ], }; -test("reporter returns empty string if GITHUB_ACTIONS isn't set", () => { - requireReporter(); - const testReporter = new GitHubActionsReporter(globalConfig); - testReporter.onRunComplete(new Set(), aggregatedResults); - expect(results.join('')).toBe(''); -}); - test('reporter extracts the correct filename, line, and column', () => { - jest.doMock('ci-info', () => ({GITHUB_ACTIONS: true})); - requireReporter(); const testReporter = new GitHubActionsReporter(globalConfig); testReporter.onRunComplete(new Set(), aggregatedResults); From 0461d492a18beb5fdd704b272de653e82e47633c Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 30 Mar 2022 09:16:21 +0200 Subject: [PATCH 22/22] Update packages/jest-reporters/src/GitHubActionsReporter.ts --- packages/jest-reporters/src/GitHubActionsReporter.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/jest-reporters/src/GitHubActionsReporter.ts b/packages/jest-reporters/src/GitHubActionsReporter.ts index ea3c1d6ec31a..dd63977df91d 100644 --- a/packages/jest-reporters/src/GitHubActionsReporter.ts +++ b/packages/jest-reporters/src/GitHubActionsReporter.ts @@ -13,6 +13,7 @@ import type {Context} from './types'; const lineAndColumnInStackTrace = /^.*?:([0-9]+):([0-9]+).*$/; 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'],