From 22ad8751fa24ef6c1a9bde594fe82c702b96f8b9 Mon Sep 17 00:00:00 2001 From: epszaw Date: Wed, 5 Jun 2024 20:07:58 +0200 Subject: [PATCH] implement hooks handlers for codeceptjs --- packages/allure-codeceptjs/src/reporter.ts | 46 +++++++------- .../allure-codeceptjs/test/spec/hooks.test.ts | 60 +++++++++++++++---- .../test/spec/simple.test.ts | 2 +- 3 files changed, 76 insertions(+), 32 deletions(-) diff --git a/packages/allure-codeceptjs/src/reporter.ts b/packages/allure-codeceptjs/src/reporter.ts index 575dd5ba4..90979b704 100644 --- a/packages/allure-codeceptjs/src/reporter.ts +++ b/packages/allure-codeceptjs/src/reporter.ts @@ -90,6 +90,9 @@ export class AllureCodeceptJsReporter { // Hooks event.dispatcher.addListener(event.hook.started, this.hookStarted.bind(this)); event.dispatcher.addListener(event.hook.passed, this.hookPassed.bind(this)); + // Suite + event.dispatcher.addListener(event.suite.before, this.suiteBefore.bind(this)); + event.dispatcher.addListener(event.suite.after, this.suiteAfter.bind(this)); // Test event.dispatcher.addListener(event.test.started, this.testStarted.bind(this)); event.dispatcher.addListener(event.test.skipped, this.testSkipped.bind(this)); @@ -102,34 +105,35 @@ export class AllureCodeceptJsReporter { event.dispatcher.addListener(event.step.comment, this.stepComment.bind(this)); } - hookStarted(hook: CodeceptHook) { - const currentTest = hook?.ctx?.currentTest; + suiteBefore() { + this.allureRuntime!.startScope(); + } - if (!currentTest) { - return; - } + suiteAfter() { + this.allureRuntime!.writeScope(); + } - // @ts-ignore - this.startAllureTest(currentTest); - // TODO: group before hooks into fixture - this.allureRuntime!.startStep( - { - name: "before hook", - }, - this.currentAllureResultUuid!, - ); + hookStarted(hook: CodeceptHook) { + const currentRunnable = hook?.ctx?.test; + const hookType = currentRunnable!.title.match(/^"(?.+)" hook/)!.groups!.hookType; + const fixtureType = /before/.test(hookType) ? "before" : "after"; + + this.allureRuntime!.startFixture(fixtureType, { + name: currentRunnable!.title, + stage: Stage.RUNNING, + start: Date.now(), + }); } hookPassed() { - if (!this.currentAllureResultUuid) { - return; - } - - this.allureRuntime!.updateStep((result) => { + this.allureRuntime!.updateFixture((result) => { result.status = Status.PASSED; result.stage = Stage.FINISHED; - }, this.currentAllureResultUuid); - this.allureRuntime!.stopStep({ uuid: this.currentAllureResultUuid }); + }); + + const fixtureUuid = this.allureRuntime!.stopFixture({ stop: Date.now() }); + + this.allureRuntime!.writeFixture(fixtureUuid); } testStarted(test: CodeceptTest & { tags: string[] }) { diff --git a/packages/allure-codeceptjs/test/spec/hooks.test.ts b/packages/allure-codeceptjs/test/spec/hooks.test.ts index 17fee3e16..c4a6a5137 100644 --- a/packages/allure-codeceptjs/test/spec/hooks.test.ts +++ b/packages/allure-codeceptjs/test/spec/hooks.test.ts @@ -3,11 +3,20 @@ import { Stage, Status } from "allure-js-commons"; import { runCodeceptJsInlineTest } from "../utils.js"; it("handles hooks", async () => { - const { tests } = await runCodeceptJsInlineTest({ + const { tests, groups } = await runCodeceptJsInlineTest({ "sample.test.js": ` + Feature("sample-feature"); + + BeforeAll(() => {}); + AfterAll(() => {}); + Before(() => {}); + After(() => {}); + + // these hooks shouldn't be reported because codeceptjs doesn't provide any information about them + BeforeSuite(() => {}); + AfterSuite(() => {}); - Feature("sample-feature"); Scenario("sample-scenario", async ({ I }) => { I.pass(); }); @@ -15,18 +24,49 @@ it("handles hooks", async () => { }); expect(tests).toHaveLength(1); - expect(tests[0].steps).toHaveLength(2); - expect(tests[0].steps).toEqual( + expect(tests[0].steps).toHaveLength(1); + expect(groups).toHaveLength(4); + expect(groups).toEqual( expect.arrayContaining([ expect.objectContaining({ - name: "before hook", - status: Status.PASSED, - stage: Stage.FINISHED, + name: "\"before all\" hook: BeforeSuite for \"sample-scenario\"", + befores: expect.arrayContaining([ + expect.objectContaining({ + status: Status.PASSED, + stage: Stage.FINISHED, + }), + ]), + afters: [], + }), + expect.objectContaining({ + name: "\"before each\" hook: Before for \"sample-scenario\"", + befores: expect.arrayContaining([ + expect.objectContaining({ + status: Status.PASSED, + stage: Stage.FINISHED, + }), + ]), + afters: [], + }), + expect.objectContaining({ + name: "\"after each\" hook: After for \"sample-scenario\"", + befores: [], + afters: expect.arrayContaining([ + expect.objectContaining({ + status: Status.PASSED, + stage: Stage.FINISHED, + }), + ]), }), expect.objectContaining({ - name: "I pass", - status: Status.PASSED, - stage: Stage.FINISHED, + name: "\"after all\" hook: AfterSuite for \"sample-scenario\"", + befores: [], + afters: expect.arrayContaining([ + expect.objectContaining({ + status: Status.PASSED, + stage: Stage.FINISHED, + }), + ]), }), ]), ); diff --git a/packages/allure-codeceptjs/test/spec/simple.test.ts b/packages/allure-codeceptjs/test/spec/simple.test.ts index 735a4fb19..c135677e6 100644 --- a/packages/allure-codeceptjs/test/spec/simple.test.ts +++ b/packages/allure-codeceptjs/test/spec/simple.test.ts @@ -2,7 +2,7 @@ import { expect, it } from "vitest"; import { Stage, Status } from "allure-js-commons"; import { runCodeceptJsInlineTest } from "../utils.js"; -it("simple scenarios", async () => { +it("handles simple scenarios", async () => { const { tests } = await runCodeceptJsInlineTest({ "nested/login.test.js": ` Feature("login-feature");