Skip to content

Commit

Permalink
[patch] fix of #59 issue and update cypress (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmisty committed Nov 1, 2023
1 parent fb828a1 commit f67b151
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
## Change Log
### 0.12.3
- [tech] update cypress 13.3.4
- fix of #59 issue
### 0.12.2
- small local dependancy issue
### 0.12.1
Expand Down
24 changes: 24 additions & 0 deletions integration/e2e/hooks/before-each-fail-in-test-then-in-hook.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
describe('before each fail with retry - first in test then in hook @beforeEachRetry', { retries: 2 }, () => {
beforeEach('Named hook', function () {
cy.log('before each');

if (Cypress.currentRetry > 1 && this.currentTest?.title?.indexOf('test 05') !== -1) {
// throw new Error('Fail in hook');
cy.wrap(null).then(() => {
throw new Error('Fail in hook');
});
}
});

for (let i = 1; i <= 10; i++) {
it(`test ${`0${i}`.slice(-2)}`, function () {
cy.log(`test ${i}`);

if (this.test?.title.indexOf('test 05') !== -1) {
cy.wrap(null).then(() => {
throw new Error('Fail in test');
});
}
});
}
});
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
"@typescript-eslint/eslint-plugin": "^5.44.0",
"@typescript-eslint/parser": "^5.44.0",
"allure-commandline": "^2.24.0",
"cypress": "^13.3.3",
"cypress": "^13.3.4",
"cypress-redirect-browser-log": "^1.1.2",
"eslint": "^8.46.0",
"eslint-config-prettier": "^8.5.0",
Expand Down
18 changes: 16 additions & 2 deletions src/setup/allure-mocha-reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,28 @@ const createTests = (runner: Mocha.Runner, test: Mocha.Test) => {
});
};

/**
* Will create synthetic tests that were not run for allure report with unknown status
* @param runner
* @param test - hook that failed
*/
const createTestsBeforeEach = (runner: Mocha.Runner, test: Mocha.Test) => {
let index = 0;
let failedIndex = 0;
let found = false;

test.parent?.eachTest(ts => {
ts.err = test.err;

// test.title is hook title like ""before each" hook: Named hook for "test 05""
const startFrom = test.title.indexOf(ts.title);
index++;

if (index !== 1 && ts) {
if (!found && startFrom !== -1) {
found = true;
failedIndex = index;
}

if (found && index > failedIndex) {
runner.emit(CUSTOM_EVENTS.TEST_BEGIN, ts);
runner.emit(CUSTOM_EVENTS.TEST_FAIL, ts);
runner.emit(CUSTOM_EVENTS.TEST_END, ts);
Expand Down
169 changes: 169 additions & 0 deletions tests/test-folder/mocha-events/hooks/synthetic-tests-creation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
import { createResTest2, fixResult } from '../../../cy-helper/utils';
import { AllureTest, parseAllure } from 'allure-js-parser';

describe('should have all hooks and steps inside', () => {
const res = createResTest2(
[
`
describe('before each fail with retry - first in test then in hook @beforeEachRetry', { retries: 2 }, () => {
beforeEach('Named hook', function () {
cy.log('before each');
if (Cypress.currentRetry > 1 && this.currentTest?.title?.indexOf('test 05') !== -1) {
cy.wrap(null).then(() => {
throw new Error('Fail in hook');
});
}
});
for (let i = 1; i <= 10; i++) {
it('test ' + ('0'+i).slice(-2), function () {
cy.log('test ' + i);
if (this.test?.title.indexOf('test 05') !== -1) {
cy.wrap(null).then(() => {
throw new Error('Fail in test');
});
}
});
}
});
`,
],
{ allureAddVideoOnPass: 'true' },
);

describe('check results', () => {
let resFixed: AllureTest[];
let results: AllureTest[];

beforeAll(() => {
results = parseAllure(res.watch);
resFixed = fixResult(results);
});

it('check tests count', async () => {
expect(resFixed.length).toEqual(12);
});

it('check tests names', async () => {
expect(resFixed.map(t => t.fullName).sort()).toEqual([
'before each fail with retry - first in test then in hook @beforeEachRetry test 01',
'before each fail with retry - first in test then in hook @beforeEachRetry test 02',
'before each fail with retry - first in test then in hook @beforeEachRetry test 03',
'before each fail with retry - first in test then in hook @beforeEachRetry test 04',
'before each fail with retry - first in test then in hook @beforeEachRetry test 05',
'before each fail with retry - first in test then in hook @beforeEachRetry test 05',
'before each fail with retry - first in test then in hook @beforeEachRetry test 05',
'before each fail with retry - first in test then in hook @beforeEachRetry test 06',
'before each fail with retry - first in test then in hook @beforeEachRetry test 07',
'before each fail with retry - first in test then in hook @beforeEachRetry test 08',
'before each fail with retry - first in test then in hook @beforeEachRetry test 09',
'before each fail with retry - first in test then in hook @beforeEachRetry test 10',
]);
});

it('check statuses and details', () => {
expect(
results
.map(t => ({
fullName: t.fullName,
name: t.name,
status: t.status,
message: t.statusDetails.message,
start: t.start,
}))
.sort((a, b) => (a.start && b.start && a.start < b.start ? -1 : 1))
.map(x => ({ ...x, start: undefined })),
).toEqual([
{
fullName:
'before each fail with retry - first in test then in hook @beforeEachRetry test 01',
name: 'test 01',
status: 'passed',
},
{
fullName:
'before each fail with retry - first in test then in hook @beforeEachRetry test 02',
name: 'test 02',
status: 'passed',
},
{
fullName:
'before each fail with retry - first in test then in hook @beforeEachRetry test 03',
name: 'test 03',
status: 'passed',
},
{
fullName:
'before each fail with retry - first in test then in hook @beforeEachRetry test 04',
name: 'test 04',
status: 'passed',
},
{
fullName:
'before each fail with retry - first in test then in hook @beforeEachRetry test 05',
message: 'Fail in test',
name: 'test 05',
status: 'failed',
},
{
fullName:
'before each fail with retry - first in test then in hook @beforeEachRetry test 05',
message: 'Fail in test',
name: 'test 05',
status: 'failed',
},
{
fullName:
'before each fail with retry - first in test then in hook @beforeEachRetry test 05',
message:
'Fail in hook\n\nBecause this error occurred during a `before each` hook we are skipping the remaining tests in the current suite: `before each fail with retry...`',
name: 'test 05',
status: 'failed',
},
{
fullName:
'before each fail with retry - first in test then in hook @beforeEachRetry test 06',
message:
'Fail in hook\n\nBecause this error occurred during a `before each` hook we are skipping the remaining tests in the current suite: `before each fail with retry...`',
name: 'test 06',
status: 'unknown',
},
{
fullName:
'before each fail with retry - first in test then in hook @beforeEachRetry test 07',
message:
'Fail in hook\n\nBecause this error occurred during a `before each` hook we are skipping the remaining tests in the current suite: `before each fail with retry...`',
name: 'test 07',
status: 'unknown',
},
{
fullName:
'before each fail with retry - first in test then in hook @beforeEachRetry test 08',
message:
'Fail in hook\n\nBecause this error occurred during a `before each` hook we are skipping the remaining tests in the current suite: `before each fail with retry...`',
name: 'test 08',
status: 'unknown',
},
{
fullName:
'before each fail with retry - first in test then in hook @beforeEachRetry test 09',
message:
'Fail in hook\n\nBecause this error occurred during a `before each` hook we are skipping the remaining tests in the current suite: `before each fail with retry...`',
name: 'test 09',
status: 'unknown',
},
{
fullName:
'before each fail with retry - first in test then in hook @beforeEachRetry test 10',
message:
'Fail in hook\n\nBecause this error occurred during a `before each` hook we are skipping the remaining tests in the current suite: `before each fail with retry...`',
name: 'test 10',
status: 'unknown',
},
]);
});
});
});

0 comments on commit f67b151

Please sign in to comment.