Skip to content

Commit 242d63c

Browse files
authored
[patch] Fix suite structure when before hook fails in nested suite - fixes issue #95 and issue #149 (#151)
1 parent ce414af commit 242d63c

17 files changed

+1891
-81
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module.exports = {
1212
ignorePatterns: ['*.yaml', '*.yml', '*.csv'],
1313

1414
rules: {
15+
'jest/no-export': 'off',
1516
'@typescript-eslint/no-explicit-any': 'warn',
1617
'prefer-template': 'error',
1718
quotes: ['error', 'single', { avoidEscape: true }],

jest.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ export default {
4242
'!**/tests/test-folder/allure-plugin/**/?(*.)+(spec|test).[tj]s?(x)',
4343
'!**/lib/**/*.*',
4444
],
45+
moduleNameMapper: {
46+
'^@test-utils$': '<rootDir>/tests/cy-helper/utils.ts',
47+
},
4548
transform: {
4649
'^.+\\.tsx?$': ['ts-jest', { tsconfig: 'tests/tsconfig.json' }],
4750

src/plugins/allure-types.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
import type { StatusDetails } from 'allure-js-commons';
21
import type { ContentType } from '../common/types';
32

3+
export type StatusDetails = {
4+
message?: string;
5+
trace?: string;
6+
};
47
export interface AutoScreen {
58
screenshotId?: string;
69
specName?: string;
@@ -61,7 +64,7 @@ type AllureTask = {
6164
screenshotOne: { name: string | undefined; forStep?: boolean };
6265
screenshotAttachment: AutoScreen;
6366
testResult: {
64-
title: string;
67+
title?: string;
6568
id: string;
6669
result: Status;
6770
details?: StatusDetails;

src/setup/allure-mocha-reporter.ts

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ import {
88
Status,
99
Category,
1010
LabelName,
11+
LinkType,
1112
} from '../plugins/allure-types'; // todo
1213
import { registerScreenshotHandler } from './screenshots';
1314
import StatusDetails = Cypress.StatusDetails;
1415
import { logClient } from './helper';
15-
import { tmsIssueUrl } from '../common';
16+
import { packageLog, tmsIssueUrl } from '../common';
1617
import { EventEmitter } from 'events';
1718
import AllureEvents = Cypress.AllureEvents;
1819

@@ -126,10 +127,22 @@ export const allureInterface = (
126127
},
127128

128129
link: (url: string, name?: string, type?: 'issue' | 'tms') => fn({ task: 'link', arg: { url, name, type } }),
129-
tms: (url: string, name?: string) =>
130-
fn({ task: 'link', arg: { url: tmsIssueUrl(env, url, 'tms'), name: name ?? url, type: 'tms' } }),
131-
issue: (url: string, name?: string) =>
132-
fn({ task: 'link', arg: { url: tmsIssueUrl(env, url, 'issue'), name: name ?? url, type: 'issue' } }),
130+
tms: (url: string, name?: string) => {
131+
const type: LinkType = 'tms';
132+
const fullUrl = tmsIssueUrl(env, url, type);
133+
const linkName = name ?? url;
134+
135+
return fn({ task: 'link', arg: { url: fullUrl, name: linkName, type } });
136+
},
137+
138+
issue: (url: string, name?: string) => {
139+
const type: LinkType = 'issue';
140+
const fullUrl = tmsIssueUrl(env, url, type);
141+
const linkName = name ?? url;
142+
143+
return fn({ task: 'link', arg: { url: fullUrl, name: linkName, type } });
144+
},
145+
133146
label: (name: string, value: string) => fn({ task: 'label', arg: { name, value } }),
134147
suite: (name: string) => fn({ task: 'suite', arg: { name } }),
135148
parentSuite: (name: string) => fn({ task: 'parentSuite', arg: { name } }),
@@ -176,7 +189,7 @@ const isHook = (test: Mocha.Test) => {
176189
return (test as any).type === 'hook';
177190
};
178191

179-
const createTests = (runner: Mocha.Runner, test: Mocha.Test) => {
192+
const createTestsForFailedBeforeHook = (runner: Mocha.Runner, test: Mocha.Test) => {
180193
let index = 0;
181194
test.parent?.eachTest(ts => {
182195
ts.err = test.err;
@@ -186,6 +199,15 @@ const createTests = (runner: Mocha.Runner, test: Mocha.Test) => {
186199
if (ts) {
187200
if (index === 1) {
188201
ts.state = 'failed';
202+
203+
if (ts.err) {
204+
// Cypress error cannot be taken here - it will be updated only on 'test:after:run' event
205+
// so to simplify events chain creating own error message
206+
// need to watch cypress error text message when it changes - and update it here
207+
ts.err.message =
208+
`${ts.err.message}\n\n` +
209+
`Because this error occurred during a \`before all\` hook we are skipping the remaining tests in the current suite: \`${ts.parent?.title}\` (added by ${packageLog})`;
210+
}
189211
}
190212
runner.emit(CUSTOM_EVENTS.TEST_BEGIN, ts);
191213
runner.emit(CUSTOM_EVENTS.TEST_FAIL, ts);
@@ -393,7 +415,8 @@ export const registerMochaReporter = (ws: WebSocket) => {
393415
return;
394416
}
395417

396-
createTestsCallb = () => createTests(runner, test);
418+
runner.emit(CUSTOM_EVENTS.TEST_END, test);
419+
createTestsForFailedBeforeHook(runner, test);
397420

398421
return;
399422
}

tests/.eslintrc.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ module.exports = {
66
extends: [...original.extends, 'plugin:jest/recommended'],
77
rules: {
88
...original.rules,
9+
10+
'@typescript-eslint/no-var-requires': 'off',
911
'@typescript-eslint/no-explicit-any': 'off',
1012
'jest/no-standalone-expect': 'off',
1113
'@typescript-eslint/no-non-null-assertion': 'off',

0 commit comments

Comments
 (0)