Skip to content

Commit

Permalink
[patch] Fix suite structure when before hook fails in nested suite - …
Browse files Browse the repository at this point in the history
…fixes issue #95 and issue #149 (#151)
  • Loading branch information
mmisty authored Jun 28, 2024
1 parent ce414af commit 242d63c
Show file tree
Hide file tree
Showing 17 changed files with 1,891 additions and 81 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = {
ignorePatterns: ['*.yaml', '*.yml', '*.csv'],

rules: {
'jest/no-export': 'off',
'@typescript-eslint/no-explicit-any': 'warn',
'prefer-template': 'error',
quotes: ['error', 'single', { avoidEscape: true }],
Expand Down
3 changes: 3 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export default {
'!**/tests/test-folder/allure-plugin/**/?(*.)+(spec|test).[tj]s?(x)',
'!**/lib/**/*.*',
],
moduleNameMapper: {
'^@test-utils$': '<rootDir>/tests/cy-helper/utils.ts',
},
transform: {
'^.+\\.tsx?$': ['ts-jest', { tsconfig: 'tests/tsconfig.json' }],

Expand Down
7 changes: 5 additions & 2 deletions src/plugins/allure-types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type { StatusDetails } from 'allure-js-commons';
import type { ContentType } from '../common/types';

export type StatusDetails = {
message?: string;
trace?: string;
};
export interface AutoScreen {
screenshotId?: string;
specName?: string;
Expand Down Expand Up @@ -61,7 +64,7 @@ type AllureTask = {
screenshotOne: { name: string | undefined; forStep?: boolean };
screenshotAttachment: AutoScreen;
testResult: {
title: string;
title?: string;
id: string;
result: Status;
details?: StatusDetails;
Expand Down
37 changes: 30 additions & 7 deletions src/setup/allure-mocha-reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import {
Status,
Category,
LabelName,
LinkType,
} from '../plugins/allure-types'; // todo
import { registerScreenshotHandler } from './screenshots';
import StatusDetails = Cypress.StatusDetails;
import { logClient } from './helper';
import { tmsIssueUrl } from '../common';
import { packageLog, tmsIssueUrl } from '../common';
import { EventEmitter } from 'events';
import AllureEvents = Cypress.AllureEvents;

Expand Down Expand Up @@ -126,10 +127,22 @@ export const allureInterface = (
},

link: (url: string, name?: string, type?: 'issue' | 'tms') => fn({ task: 'link', arg: { url, name, type } }),
tms: (url: string, name?: string) =>
fn({ task: 'link', arg: { url: tmsIssueUrl(env, url, 'tms'), name: name ?? url, type: 'tms' } }),
issue: (url: string, name?: string) =>
fn({ task: 'link', arg: { url: tmsIssueUrl(env, url, 'issue'), name: name ?? url, type: 'issue' } }),
tms: (url: string, name?: string) => {
const type: LinkType = 'tms';
const fullUrl = tmsIssueUrl(env, url, type);
const linkName = name ?? url;

return fn({ task: 'link', arg: { url: fullUrl, name: linkName, type } });
},

issue: (url: string, name?: string) => {
const type: LinkType = 'issue';
const fullUrl = tmsIssueUrl(env, url, type);
const linkName = name ?? url;

return fn({ task: 'link', arg: { url: fullUrl, name: linkName, type } });
},

label: (name: string, value: string) => fn({ task: 'label', arg: { name, value } }),
suite: (name: string) => fn({ task: 'suite', arg: { name } }),
parentSuite: (name: string) => fn({ task: 'parentSuite', arg: { name } }),
Expand Down Expand Up @@ -176,7 +189,7 @@ const isHook = (test: Mocha.Test) => {
return (test as any).type === 'hook';
};

const createTests = (runner: Mocha.Runner, test: Mocha.Test) => {
const createTestsForFailedBeforeHook = (runner: Mocha.Runner, test: Mocha.Test) => {
let index = 0;
test.parent?.eachTest(ts => {
ts.err = test.err;
Expand All @@ -186,6 +199,15 @@ const createTests = (runner: Mocha.Runner, test: Mocha.Test) => {
if (ts) {
if (index === 1) {
ts.state = 'failed';

if (ts.err) {
// Cypress error cannot be taken here - it will be updated only on 'test:after:run' event
// so to simplify events chain creating own error message
// need to watch cypress error text message when it changes - and update it here
ts.err.message =
`${ts.err.message}\n\n` +
`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})`;
}
}
runner.emit(CUSTOM_EVENTS.TEST_BEGIN, ts);
runner.emit(CUSTOM_EVENTS.TEST_FAIL, ts);
Expand Down Expand Up @@ -393,7 +415,8 @@ export const registerMochaReporter = (ws: WebSocket) => {
return;
}

createTestsCallb = () => createTests(runner, test);
runner.emit(CUSTOM_EVENTS.TEST_END, test);
createTestsForFailedBeforeHook(runner, test);

return;
}
Expand Down
2 changes: 2 additions & 0 deletions tests/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module.exports = {
extends: [...original.extends, 'plugin:jest/recommended'],
rules: {
...original.rules,

'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'jest/no-standalone-expect': 'off',
'@typescript-eslint/no-non-null-assertion': 'off',
Expand Down
Loading

0 comments on commit 242d63c

Please sign in to comment.