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

Fix skipped and todo tests in Github Actions Reporter #14309

Merged
merged 2 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- `[jest-circus]` Fix snapshot matchers in concurrent tests when nr of tests exceeds `maxConcurrency` ([#14335](https://github.com/jestjs/jest/pull/14335))
- `[jest-snapshot]` Move `@types/prettier` from `dependencies` to `devDependencies` ([#14328](https://github.com/jestjs/jest/pull/14328))
- `[jest-reporters]` Add "skipped" and "todo" symbols to Github Actions Reporter ([#14309](https://github.com/jestjs/jest/pull/14309))

### Chore & Maintenance

Expand Down
11 changes: 0 additions & 11 deletions e2e/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,17 +310,6 @@ export const extractSummaries = (
.map(({start, end}) => extractSortedSummary(stdout.slice(start, end)));
};

export const normalizeIcons = (str: string) => {
if (!str) {
return str;
}

// Make sure to keep in sync with `jest-util/src/specialChars`
return str
.replace(new RegExp('\u00D7', 'gu'), '\u2715')
.replace(new RegExp('\u221A', 'gu'), '\u2713');
};

// Certain environments (like CITGM and GH Actions) do not come with mercurial installed
let hgIsInstalled: boolean | null = null;

Expand Down
2 changes: 1 addition & 1 deletion e2e/runJest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import execa = require('execa');
import * as fs from 'graceful-fs';
import stripAnsi = require('strip-ansi');
import type {FormattedTestResults} from '@jest/test-result';
import {normalizeIcons} from '@jest/test-utils';
import type {Config} from '@jest/types';
import {ErrorWithStack} from 'jest-util';
import {normalizeIcons} from './Utils';

const JEST_PATH = path.resolve(__dirname, '../packages/jest-cli/bin/jest.js');

Expand Down
38 changes: 25 additions & 13 deletions packages/jest-reporters/src/GitHubActionsReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import stripAnsi = require('strip-ansi');
import type {
AggregatedResult,
AssertionResult,
Status,
Test,
TestContext,
TestResult,
Expand All @@ -21,6 +22,7 @@ import {
getTopFrame,
separateMessageFromStack,
} from 'jest-message-util';
import {specialChars} from 'jest-util';
import BaseReporter from './BaseReporter';

type AnnotationOptions = {
Expand All @@ -32,6 +34,7 @@ type AnnotationOptions = {
};

const titleSeparator = ' \u203A ';
const ICONS = specialChars.ICONS;

type PerformanceInfo = {
end: number;
Expand All @@ -42,7 +45,7 @@ type PerformanceInfo = {

type ResultTreeLeaf = {
name: string;
passed: boolean;
status: Status;
duration: number;
children: Array<never>;
};
Expand Down Expand Up @@ -215,17 +218,15 @@ export default class GitHubActionsReporter extends BaseReporter {
const branches: Array<Array<string>> = [];
suiteResult.forEach(element => {
if (element.ancestorTitles.length === 0) {
let passed = true;
if (element.status !== 'passed') {
if (element.status === 'failed') {
root.passed = false;
passed = false;
}
const duration = element.duration || 1;
root.children.push({
children: [],
duration,
name: element.title,
passed,
status: element.status,
});
} else {
let alreadyInserted = false;
Expand Down Expand Up @@ -263,21 +264,19 @@ export default class GitHubActionsReporter extends BaseReporter {
};
const branches: Array<Array<string>> = [];
suiteResult.forEach(element => {
let passed = true;
let duration = element.duration;
if (!duration || isNaN(duration)) {
duration = 1;
}
if (this.arrayEqual(element.ancestorTitles, ancestors)) {
if (element.status !== 'passed') {
if (element.status === 'failed') {
node.passed = false;
passed = false;
}
node.children.push({
children: [],
duration,
name: element.title,
passed,
status: element.status,
});
} else if (
this.arrayChild(
Expand Down Expand Up @@ -354,17 +353,30 @@ export default class GitHubActionsReporter extends BaseReporter {
}
const spaces = ' '.repeat(numberSpaces);
let resultSymbol;
if (resultTree.passed) {
resultSymbol = chalk.green('\u2713');
} else {
resultSymbol = chalk.red('\u00D7');
switch (resultTree.status) {
case 'passed':
resultSymbol = chalk.green(ICONS.success);
break;
case 'failed':
resultSymbol = chalk.red(ICONS.failed);
break;
case 'todo':
resultSymbol = chalk.magenta(ICONS.todo);
break;
case 'pending':
case 'skipped':
resultSymbol = chalk.yellow(ICONS.pending);
break;
}
this.log(
`${spaces + resultSymbol} ${resultTree.name} (${
resultTree.duration
} ms)`,
);
} else {
if (!('passed' in resultTree)) {
throw new Error('Expected a node. Got a leaf');
}
if (resultTree.passed) {
if (alreadyGrouped) {
this.log(' '.repeat(depth) + resultTree.name);
Expand Down
155 changes: 146 additions & 9 deletions packages/jest-reporters/src/__tests__/GitHubActionsReporter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,25 @@
import type {
AggregatedResult,
AssertionResult,
Status,
Test,
TestCaseResult,
TestResult,
} from '@jest/test-result';
import {normalizeIcons} from '@jest/test-utils';
import type {Config} from '@jest/types';
import GitHubActionsReporter from '../GitHubActionsReporter';
import BaseGitHubActionsReporter from '../GitHubActionsReporter';

afterEach(() => {
jest.clearAllMocks();
});

class GitHubActionsReporter extends BaseGitHubActionsReporter {
override log(message: string): void {
super.log(normalizeIcons(message));
}
}

const mockedStderrWrite = jest
.spyOn(process.stderr, 'write')
.mockImplementation(() => true);
Expand Down Expand Up @@ -174,7 +182,7 @@ describe('logs', () => {
children: [],
duration: 10,
name: 'test',
passed: false,
status: 'failed',
},
],
name: '/',
Expand Down Expand Up @@ -217,7 +225,7 @@ describe('logs', () => {
children: [],
duration: 10,
name: 'test',
passed: true,
status: 'passed',
},
],
name: '/',
Expand Down Expand Up @@ -262,7 +270,7 @@ describe('logs', () => {
children: [],
duration: 10,
name: 'test',
passed: false,
status: 'failed',
},
],
name: 'Test describe',
Expand Down Expand Up @@ -311,7 +319,68 @@ describe('logs', () => {
children: [],
duration: 10,
name: 'test',
passed: true,
status: 'passed',
},
],
name: 'Test describe',
passed: true,
},
],
name: '/',
passed: true,
performanceInfo: {
end: 30,
runtime: 20,
slow: false,
start: 10,
},
};
const gha = new GitHubActionsReporter({} as Config.GlobalConfig, {
silent: false,
});

const generated = gha['getResultTree'](testResults, '/', suitePerf);

expect(mockedStderrWrite).not.toHaveBeenCalled();
expect(generated).toEqual(expectedResults);
});

test('skipped single test and todo single test inside describe', () => {
const testResults = [
{
ancestorTitles: ['Test describe'],
duration: 10,
status: 'skipped',
title: 'test',
},
{
ancestorTitles: ['Test describe'],
duration: 14,
status: 'todo',
title: 'test2',
},
] as unknown as Array<AssertionResult>;
const suitePerf = {
end: 30,
runtime: 20,
slow: false,
start: 10,
};
const expectedResults = {
children: [
{
children: [
{
children: [],
duration: 10,
name: 'test',
status: 'skipped',
},
{
children: [],
duration: 14,
name: 'test2',
status: 'todo',
},
],
name: 'Test describe',
Expand Down Expand Up @@ -346,7 +415,7 @@ describe('logs', () => {
children: [],
duration: 10,
name: 'test',
passed: false,
status: 'failed' as Status,
},
],
name: '/',
Expand Down Expand Up @@ -374,7 +443,7 @@ describe('logs', () => {
children: [],
duration: 10,
name: 'test',
passed: true,
status: 'passed' as Status,
},
],
name: '/',
Expand Down Expand Up @@ -404,7 +473,7 @@ describe('logs', () => {
children: [],
duration: 10,
name: 'test',
passed: false,
status: 'failed' as Status,
},
],
name: 'Test describe',
Expand Down Expand Up @@ -438,7 +507,75 @@ describe('logs', () => {
children: [],
duration: 10,
name: 'test',
passed: true,
status: 'passed' as Status,
},
],
name: 'Test describe',
passed: true,
},
],
name: '/',
passed: true,
performanceInfo: {
end: 30,
runtime: 20,
slow: false,
start: 10,
},
};
const gha = new GitHubActionsReporter({} as Config.GlobalConfig, {
silent: false,
});

gha['printResultTree'](generatedTree);

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

test('todo single test inside describe', () => {
const generatedTree = {
children: [
{
children: [
{
children: [],
duration: 10,
name: 'test',
status: 'todo' as Status,
},
],
name: 'Test describe',
passed: true,
},
],
name: '/',
passed: true,
performanceInfo: {
end: 30,
runtime: 20,
slow: false,
start: 10,
},
};
const gha = new GitHubActionsReporter({} as Config.GlobalConfig, {
silent: false,
});

gha['printResultTree'](generatedTree);

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

test('skipped single test inside describe', () => {
const generatedTree = {
children: [
{
children: [
{
children: [],
duration: 10,
name: 'test',
status: 'skipped' as Status,
},
],
name: 'Test describe',
Expand Down