Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

- `[jest-runtime]` Fix issue where user cannot utilize dynamic import despite specifying `--experimental-vm-modules` Node option ([#15842](https://github.com/jestjs/jest/pull/15842))
- `[jest-test-sequencer]` Fix issue where failed tests due to compilation errors not getting re-executed even with `--onlyFailures` CLI option ([#15851](https://github.com/jestjs/jest/pull/15851))
- `[jest-reporters]` Fix issue where console output not displayed for GHA reporter even with `silent: false` option ([#15864](https://github.com/jestjs/jest/pull/15864))

### Chore & Maintenance

Expand Down
2 changes: 1 addition & 1 deletion packages/jest-console/src/getConsoleOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default function getConsoleOutput(
globalConfig: Config.GlobalConfig,
): string {
const TITLE_INDENT =
globalConfig.verbose === true ? ' '.repeat(2) : ' '.repeat(4);
globalConfig?.verbose === true ? ' '.repeat(2) : ' '.repeat(4);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have to add verbose nullish check for double assurance

const CONSOLE_INDENT = TITLE_INDENT + ' '.repeat(2);

const logEntries = buffer.reduce((output, {type, message, origin}) => {
Expand Down
26 changes: 23 additions & 3 deletions packages/jest-reporters/src/GitHubActionsReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import {stripVTControlCharacters as stripAnsi} from 'util';
import chalk from 'chalk';
import {type ConsoleBuffer, getConsoleOutput} from '@jest/console';
import type {
AggregatedResult,
AssertionResult,
Expand Down Expand Up @@ -66,12 +67,14 @@ type ResultTree = {
export default class GitHubActionsReporter extends BaseReporter {
static readonly filename = __filename;
private readonly options: {silent: boolean};
protected globalConfig: Config.GlobalConfig;

constructor(
_globalConfig: Config.GlobalConfig,
reporterOptions: {silent?: boolean} = {},
) {
super();
this.globalConfig = _globalConfig;
this.options = {
silent:
typeof reporterOptions.silent === 'boolean'
Expand All @@ -90,7 +93,7 @@ export default class GitHubActionsReporter extends BaseReporter {
this.printFullResult(test.context, testResult);
}
if (this.isLastTestSuite(aggregatedResults)) {
this.printFailedTestLogs(test, aggregatedResults);
this.printFailedTestLogs(test, testResult.console, aggregatedResults);
}
}

Expand Down Expand Up @@ -179,7 +182,7 @@ export default class GitHubActionsReporter extends BaseReporter {
testDir,
results.perfStats,
);
this.printResultTree(resultTree);
this.printResultTree(resultTree, context.config, results.console);
}

private arrayEqual(a1: Array<any>, a2: Array<any>): boolean {
Expand Down Expand Up @@ -311,7 +314,11 @@ export default class GitHubActionsReporter extends BaseReporter {
return node;
}

private printResultTree(resultTree: ResultTree): void {
private printResultTree(
resultTree: ResultTree,
config: Config.ProjectConfig,
consoleLog?: ConsoleBuffer,
): void {
let perfMs;
if (resultTree.performanceInfo.slow) {
perfMs = ` (${chalk.red.inverse(
Expand All @@ -324,6 +331,9 @@ export default class GitHubActionsReporter extends BaseReporter {
this.startGroup(
`${chalk.bold.green.inverse('PASS')} ${resultTree.name}${perfMs}`,
);
if (consoleLog && !this.options.silent) {
this.log(getConsoleOutput(consoleLog, config, this.globalConfig));
}
for (const child of resultTree.children) {
this.recursivePrintResultTree(child, true, 1);
}
Expand Down Expand Up @@ -401,6 +411,7 @@ export default class GitHubActionsReporter extends BaseReporter {

private printFailedTestLogs(
context: Test,
consoleLog: ConsoleBuffer | undefined,
testResults: AggregatedResult,
): boolean {
const rootDir = context.context.config.rootDir;
Expand All @@ -416,6 +427,15 @@ export default class GitHubActionsReporter extends BaseReporter {
written = true;
}
this.startGroup(`Errors thrown in ${testDir}`);
if (consoleLog && !this.options.silent) {
this.log(
getConsoleOutput(
consoleLog,
context.context.config,
this.globalConfig,
),
);
}
this.log(result.failureMessage);
this.endGroup();
}
Expand Down
103 changes: 103 additions & 0 deletions packages/jest-reporters/src/__tests__/GitHubActionsReporter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -687,5 +687,108 @@ describe('logs', () => {

expect(mockedStderrWrite.mock.calls).toMatchSnapshot();
});

test('onTestResult last with console output for failed test', () => {
const mockTest = {
context: {
config: {
rootDir: '/testDir',
},
},
};
const mockTestResult = {
console: [
{
message: 'bar',
origin:
' at Object.log (/tmp/jest-test/a.test.js:2:13)\n at Promise.finally.completed (/github.com/jestjs/jest/packages/jest-circus/build/jestAdapterInit.js:1557:28)',
type: 'log',
},
],
failureMessage: 'Failure message',
perfStats: {
runtime: 20,
slow: false,
},
testFilePath: '/testDir/test1.js',
testResults: [
{
ancestorTitles: [],
duration: 10,
status: 'passed',
title: 'test1',
},
],
};
const mockResults = {
numFailedTestSuites: 1,
numPassedTestSuites: 2,
numTotalTestSuites: 3,
testResults: [mockTestResult],
};
const gha = new GitHubActionsReporter({} as Config.GlobalConfig, {
silent: false,
});
gha.generateAnnotations = jest.fn();

gha.onTestResult(
mockTest as Test,
mockTestResult as unknown as TestResult,
mockResults as unknown as AggregatedResult,
);

expect(mockedStderrWrite.mock.calls).toMatchSnapshot();
});

test('onTestResult last with console output for success test', () => {
const mockTest = {
context: {
config: {
rootDir: '/testDir',
},
},
};
const mockTestResult = {
console: [
{
message: 'bar',
origin:
' at Object.log (/tmp/jest-test/a.test.js:2:13)\n at Promise.finally.completed (/github.com/jestjs/jest/packages/jest-circus/build/jestAdapterInit.js:1557:28)',
type: 'log',
},
],
perfStats: {
runtime: 20,
slow: false,
},
testFilePath: '/testDir/test1.js',
testResults: [
{
ancestorTitles: [],
duration: 10,
status: 'passed',
title: 'test1',
},
],
};
const mockResults = {
numFailedTestSuites: 0,
numPassedTestSuites: 1,
numTotalTestSuites: 1,
testResults: [mockTestResult],
};
const gha = new GitHubActionsReporter({} as Config.GlobalConfig, {
silent: false,
});
gha.generateAnnotations = jest.fn();

gha.onTestResult(
mockTest as Test,
mockTestResult as unknown as TestResult,
mockResults as unknown as AggregatedResult,
);

expect(mockedStderrWrite.mock.calls).toMatchSnapshot();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,80 @@ Array [
]
`;

exports[`logs Reporter interface onTestResult last with console output for failed test 1`] = `
Array [
Array [
"::group::<bold><green><inverse>PASS</inverse></color></intensity> test1.js (20 ms)
",
],
Array [
" <dim>console.log</intensity>
bar
<dim></intensity>
<dim> <dim>at Object.log (</intensity><dim>../tmp/jest-test/a.test.js<dim>:2:13)</intensity><dim></intensity>

",
],
Array [
" <green>✓</color> test1 (10 ms)
",
],
Array [
"::endgroup::
",
],
Array [
"
",
],
Array [
"::group::Errors thrown in test1.js
",
],
Array [
" <dim>console.log</intensity>
bar
<dim></intensity>
<dim> <dim>at Object.log (</intensity><dim>../tmp/jest-test/a.test.js<dim>:2:13)</intensity><dim></intensity>

",
],
Array [
"Failure message
",
],
Array [
"::endgroup::
",
],
]
`;

exports[`logs Reporter interface onTestResult last with console output for success test 1`] = `
Array [
Array [
"::group::<bold><green><inverse>PASS</inverse></color></intensity> test1.js (20 ms)
",
],
Array [
" <dim>console.log</intensity>
bar
<dim></intensity>
<dim> <dim>at Object.log (</intensity><dim>../tmp/jest-test/a.test.js<dim>:2:13)</intensity><dim></intensity>

",
],
Array [
" <green>✓</color> test1 (10 ms)
",
],
Array [
"::endgroup::
",
],
]
`;

exports[`logs Reporter interface onTestResult not last 1`] = `
Array [
Array [
Expand Down
Loading