From bf72436c32a9f452b79300595ce24c6face68937 Mon Sep 17 00:00:00 2001 From: Taisia Pitko Date: Thu, 15 Aug 2024 13:50:41 +0300 Subject: [PATCH] [patch] inherit before each/after each hooks step status (#222) --- src/plugins/helper.ts | 9 +- .../common/utils-wrap-hooks.test.ts | 492 ++++++++++++++++++ ...ta-after-each-fail-nested-suite-several.ts | 2 +- .../data-after-each-fail-nested-suite.ts | 2 +- ...a-before-each-fail-nested-suite-several.ts | 2 +- .../data-before-each-fail-nested-suite.ts | 2 +- .../data-before-each-retry-fail.ts | 2 +- .../data-after-each-fail-several-tests.ts | 2 +- .../before-after-each/data-after-each-fail.ts | 2 +- .../data-before-each-fail-several-tests.ts | 2 +- .../data-before-each-fail.ts | 2 +- 11 files changed, 509 insertions(+), 10 deletions(-) create mode 100644 tests/test-folder/common/utils-wrap-hooks.test.ts diff --git a/src/plugins/helper.ts b/src/plugins/helper.ts index d09a017..e533f99 100644 --- a/src/plugins/helper.ts +++ b/src/plugins/helper.ts @@ -127,16 +127,23 @@ export const wrapHooks = ( if (endIndex !== undefined && startIndex !== undefined && startIndex !== endIndex) { const childrenBeforeEach = steps.slice(startIndex, endIndex + 1); + const nonSuccess = childrenBeforeEach.filter(x => x.status !== ('passed' as Status)); + const status = nonSuccess.length > 0 ? nonSuccess[0].status : ('passed' as Status); + const startTime = childrenBeforeEach[0]?.start ?? undefined; + const endTime = childrenBeforeEach[childrenBeforeEach.length - 1].stop ?? undefined; + return [ ...steps.slice(0, startIndex), { name: `${stepName}s`, steps: childrenBeforeEach, - status: 'passed' as Status, + status, statusDetails: {}, stage: 'finished' as Stage, attachments: [], parameters: [], + start: startTime, + stop: endTime, }, ...steps.slice(endIndex + 1), ]; diff --git a/tests/test-folder/common/utils-wrap-hooks.test.ts b/tests/test-folder/common/utils-wrap-hooks.test.ts new file mode 100644 index 0000000..3190012 --- /dev/null +++ b/tests/test-folder/common/utils-wrap-hooks.test.ts @@ -0,0 +1,492 @@ +import { wrapHooks } from '@src/plugins/helper'; + +describe('wrapHooks', () => { + it('should not wrap when no hooks', () => { + const steps = [ + { + attachments: [], + name: 'regular step', + status: 'passed', + }, + ] as any; + const res = wrapHooks('"before each" hook', steps); + expect(res).toEqual([ + { + attachments: [], + name: 'regular step', + status: 'passed', + }, + ]); + }); + + it('should not wrap one hook', () => { + const steps = [ + { + attachments: [], + name: '"before each" hook', + status: 'passed', + }, + + { + attachments: [], + name: 'regular step', + status: 'passed', + }, + ] as any; + const res = wrapHooks('"before each" hook', steps); + expect(res).toEqual([ + { + attachments: [], + name: '"before each" hook', + status: 'passed', + }, + + { + attachments: [], + name: 'regular step', + status: 'passed', + }, + ]); + }); + + it('should wrap two or more hooks', () => { + const steps = [ + { + attachments: [], + name: '"before each" hook', + status: 'passed', + }, + { + attachments: [], + name: '"before each" hook', + status: 'passed', + }, + + { + attachments: [], + name: 'regular step', + status: 'passed', + }, + ] as any; + const res = wrapHooks('"before each" hook', steps); + expect(res).toEqual([ + { + attachments: [], + name: '"before each" hooks', + parameters: [], + stage: 'finished', + status: 'passed', + statusDetails: {}, + steps: [ + { + attachments: [], + name: '"before each" hook', + status: 'passed', + }, + { + attachments: [], + name: '"before each" hook', + status: 'passed', + }, + ], + }, + { + attachments: [], + name: 'regular step', + status: 'passed', + }, + ]); + }); + + it('should have correct start stop times', () => { + const steps = [ + { + attachments: [], + name: '"before each" hook', + status: 'passed', + start: 100, + stop: 103, + }, + { + attachments: [], + name: '"before each" hook', + status: 'passed', + start: 105, + stop: 108, + }, + + { + attachments: [], + name: 'regular step', + status: 'passed', + }, + ] as any; + const res = wrapHooks('"before each" hook', steps); + expect(res).toEqual([ + { + attachments: [], + name: '"before each" hooks', + parameters: [], + stage: 'finished', + start: 100, + stop: 108, + status: 'passed', + statusDetails: {}, + steps: [ + { + attachments: [], + name: '"before each" hook', + status: 'passed', + start: 100, + stop: 103, + }, + { + attachments: [], + name: '"before each" hook', + status: 'passed', + start: 105, + stop: 108, + }, + ], + }, + { + attachments: [], + name: 'regular step', + status: 'passed', + }, + ]); + }); + it('should have correct start stop times - after each', () => { + const steps = [ + { + attachments: [], + name: 'regular step', + status: 'passed', + }, + { + attachments: [], + name: '"after each" hook', + status: 'passed', + start: 101, + stop: 104, + }, + { + attachments: [], + name: '"after each" hook', + status: 'passed', + start: 106, + stop: 109, + }, + ] as any; + const res = wrapHooks('"after each" hook', steps); + expect(res).toEqual([ + { + attachments: [], + name: 'regular step', + status: 'passed', + }, + { + attachments: [], + name: '"after each" hooks', + parameters: [], + stage: 'finished', + start: 101, + stop: 109, + status: 'passed', + statusDetails: {}, + steps: [ + { + attachments: [], + name: '"after each" hook', + status: 'passed', + start: 101, + stop: 104, + }, + { + attachments: [], + name: '"after each" hook', + status: 'passed', + start: 106, + stop: 109, + }, + ], + }, + ]); + }); + + it('should wrap four hooks', () => { + const steps = [ + { + attachments: [], + name: '"before each" hook', + status: 'passed', + }, + { + attachments: [], + name: '"before each" hook', + status: 'passed', + }, + { + attachments: [], + name: '"before each" hook', + status: 'passed', + }, + { + attachments: [], + name: '"before each" hook', + status: 'passed', + }, + + { + attachments: [], + name: 'regular step', + status: 'passed', + }, + ] as any; + const res = wrapHooks('"before each" hook', steps); + expect(res).toEqual([ + { + attachments: [], + name: '"before each" hooks', + parameters: [], + stage: 'finished', + status: 'passed', + statusDetails: {}, + steps: [ + { + attachments: [], + name: '"before each" hook', + status: 'passed', + }, + { + attachments: [], + name: '"before each" hook', + status: 'passed', + }, + { + attachments: [], + name: '"before each" hook', + status: 'passed', + }, + { + attachments: [], + name: '"before each" hook', + status: 'passed', + }, + ], + }, + { + attachments: [], + name: 'regular step', + status: 'passed', + }, + ]); + }); + + it('should wrap after each hooks', () => { + const steps = [ + { + attachments: [], + name: '"before each" hook', + status: 'passed', + }, + + { + attachments: [], + name: 'regular step', + status: 'passed', + }, + { + attachments: [], + name: '"after each" hook', + status: 'passed', + }, + { + attachments: [], + name: '"after each" hook', + status: 'passed', + }, + { + attachments: [], + name: '"after each" hook', + status: 'passed', + }, + ] as any; + const res = wrapHooks('"after each" hook', steps); + expect(res).toEqual([ + { + attachments: [], + name: '"before each" hook', + status: 'passed', + }, + { + attachments: [], + name: 'regular step', + status: 'passed', + }, + { + attachments: [], + name: '"after each" hooks', + parameters: [], + stage: 'finished', + status: 'passed', + statusDetails: {}, + steps: [ + { + attachments: [], + name: '"after each" hook', + status: 'passed', + }, + { + attachments: [], + name: '"after each" hook', + status: 'passed', + }, + { + attachments: [], + name: '"after each" hook', + status: 'passed', + }, + ], + }, + ]); + }); + + it('should have non-success status of children hook', () => { + const steps = [ + { + attachments: [], + name: '"before each" hook', + status: 'passed', + }, + { + attachments: [], + name: '"before each" hook', + status: 'failed', + }, + { + attachments: [], + name: '"before each" hook', + status: 'passed', + }, + { + attachments: [], + name: '"before each" hook', + status: 'broken', + }, + + { + attachments: [], + name: 'regular step', + status: 'passed', + }, + ] as any; + const res = wrapHooks('"before each" hook', steps); + expect(res).toEqual([ + { + attachments: [], + name: '"before each" hooks', + parameters: [], + stage: 'finished', + status: 'failed', + statusDetails: {}, + steps: [ + { + attachments: [], + name: '"before each" hook', + status: 'passed', + }, + { + attachments: [], + name: '"before each" hook', + status: 'failed', + }, + { + attachments: [], + name: '"before each" hook', + status: 'passed', + }, + { + attachments: [], + name: '"before each" hook', + status: 'broken', + }, + ], + }, + { + attachments: [], + name: 'regular step', + status: 'passed', + }, + ]); + }); + + it('should have non-success status of children hook - after each', () => { + const steps = [ + { + attachments: [], + name: '"before each" hook', + status: 'passed', + }, + + { + attachments: [], + name: 'regular step', + status: 'passed', + }, + { + attachments: [], + name: '"after each" hook', + status: 'passed', + }, + { + attachments: [], + name: '"after each" hook', + status: 'broken', + }, + { + attachments: [], + name: '"after each" hook', + status: 'passed', + }, + ] as any; + const res = wrapHooks('"after each" hook', steps); + expect(res).toEqual([ + { + attachments: [], + name: '"before each" hook', + status: 'passed', + }, + { + attachments: [], + name: 'regular step', + status: 'passed', + }, + { + attachments: [], + name: '"after each" hooks', + parameters: [], + stage: 'finished', + status: 'broken', + statusDetails: {}, + steps: [ + { + attachments: [], + name: '"after each" hook', + status: 'passed', + }, + { + attachments: [], + name: '"after each" hook', + status: 'broken', + }, + { + attachments: [], + name: '"after each" hook', + status: 'passed', + }, + ], + }, + ]); + }); +}); diff --git a/tests/test-folder/mocha-events/hooks/before-after-each--nested-suite/data-after-each-fail-nested-suite-several.ts b/tests/test-folder/mocha-events/hooks/before-after-each--nested-suite/data-after-each-fail-nested-suite-several.ts index 3dbafa6..ceb99e1 100644 --- a/tests/test-folder/mocha-events/hooks/before-after-each--nested-suite/data-after-each-fail-nested-suite-several.ts +++ b/tests/test-folder/mocha-events/hooks/before-after-each--nested-suite/data-after-each-fail-nested-suite-several.ts @@ -149,7 +149,7 @@ describe('${rootSuite}', () => { }, { name: '"after each" hooks', - status: 'passed', + status: 'failed', attachments: [], steps: [ { diff --git a/tests/test-folder/mocha-events/hooks/before-after-each--nested-suite/data-after-each-fail-nested-suite.ts b/tests/test-folder/mocha-events/hooks/before-after-each--nested-suite/data-after-each-fail-nested-suite.ts index eb143d6..8c91c12 100644 --- a/tests/test-folder/mocha-events/hooks/before-after-each--nested-suite/data-after-each-fail-nested-suite.ts +++ b/tests/test-folder/mocha-events/hooks/before-after-each--nested-suite/data-after-each-fail-nested-suite.ts @@ -113,7 +113,7 @@ describe('${rootSuite}', () => { }, { name: '"after each" hooks', - status: 'passed', + status: 'failed', attachments: [], steps: [ { diff --git a/tests/test-folder/mocha-events/hooks/before-after-each--nested-suite/data-before-each-fail-nested-suite-several.ts b/tests/test-folder/mocha-events/hooks/before-after-each--nested-suite/data-before-each-fail-nested-suite-several.ts index 16405b3..bb1fc5b 100644 --- a/tests/test-folder/mocha-events/hooks/before-after-each--nested-suite/data-before-each-fail-nested-suite-several.ts +++ b/tests/test-folder/mocha-events/hooks/before-after-each--nested-suite/data-before-each-fail-nested-suite-several.ts @@ -85,7 +85,7 @@ describe('${rootSuite}', () => { expected: [ { name: '"before each" hooks', - status: 'passed', + status: 'failed', attachments: [], steps: [ { diff --git a/tests/test-folder/mocha-events/hooks/before-after-each--nested-suite/data-before-each-fail-nested-suite.ts b/tests/test-folder/mocha-events/hooks/before-after-each--nested-suite/data-before-each-fail-nested-suite.ts index 09794b9..afd5528 100644 --- a/tests/test-folder/mocha-events/hooks/before-after-each--nested-suite/data-before-each-fail-nested-suite.ts +++ b/tests/test-folder/mocha-events/hooks/before-after-each--nested-suite/data-before-each-fail-nested-suite.ts @@ -62,7 +62,7 @@ describe('${rootSuite}', () => { expected: [ { name: '"before each" hooks', - status: 'passed', + status: 'failed', attachments: [], steps: [ { diff --git a/tests/test-folder/mocha-events/hooks/before-after-each--retry/data-before-each-retry-fail.ts b/tests/test-folder/mocha-events/hooks/before-after-each--retry/data-before-each-retry-fail.ts index 1569cd1..01cf2a3 100644 --- a/tests/test-folder/mocha-events/hooks/before-after-each--retry/data-before-each-retry-fail.ts +++ b/tests/test-folder/mocha-events/hooks/before-after-each--retry/data-before-each-retry-fail.ts @@ -191,7 +191,7 @@ describe('${rootSuite} @beforeEachRetry', { retries: 1 }, () => { expected: [ { name: '"before each" hooks', - status: 'passed', + status: 'failed', attachments: [], steps: [ { diff --git a/tests/test-folder/mocha-events/hooks/before-after-each/data-after-each-fail-several-tests.ts b/tests/test-folder/mocha-events/hooks/before-after-each/data-after-each-fail-several-tests.ts index 0f604a3..b456522 100644 --- a/tests/test-folder/mocha-events/hooks/before-after-each/data-after-each-fail-several-tests.ts +++ b/tests/test-folder/mocha-events/hooks/before-after-each/data-after-each-fail-several-tests.ts @@ -112,7 +112,7 @@ describe('${rootSuite}', () => { }, { name: '"after each" hooks', - status: 'passed', + status: 'failed', attachments: [], steps: [ { diff --git a/tests/test-folder/mocha-events/hooks/before-after-each/data-after-each-fail.ts b/tests/test-folder/mocha-events/hooks/before-after-each/data-after-each-fail.ts index bcf4f09..b6e2c0b 100644 --- a/tests/test-folder/mocha-events/hooks/before-after-each/data-after-each-fail.ts +++ b/tests/test-folder/mocha-events/hooks/before-after-each/data-after-each-fail.ts @@ -107,7 +107,7 @@ describe('${rootSuite}', () => { }, { name: '"after each" hooks', - status: 'passed', + status: 'failed', attachments: [], steps: [ { diff --git a/tests/test-folder/mocha-events/hooks/before-after-each/data-before-each-fail-several-tests.ts b/tests/test-folder/mocha-events/hooks/before-after-each/data-before-each-fail-several-tests.ts index 8527ebd..dc13d22 100644 --- a/tests/test-folder/mocha-events/hooks/before-after-each/data-before-each-fail-several-tests.ts +++ b/tests/test-folder/mocha-events/hooks/before-after-each/data-before-each-fail-several-tests.ts @@ -80,7 +80,7 @@ describe('${rootSuite}', () => { expected: [ { name: '"before each" hooks', - status: 'passed', + status: 'failed', attachments: [], steps: [ { diff --git a/tests/test-folder/mocha-events/hooks/before-after-each/data-before-each-fail.ts b/tests/test-folder/mocha-events/hooks/before-after-each/data-before-each-fail.ts index 19c81fa..8265c6b 100644 --- a/tests/test-folder/mocha-events/hooks/before-after-each/data-before-each-fail.ts +++ b/tests/test-folder/mocha-events/hooks/before-after-each/data-before-each-fail.ts @@ -60,7 +60,7 @@ describe('${rootSuite}', () => { expected: [ { name: '"before each" hooks', - status: 'passed', + status: 'failed', attachments: [], steps: [ {