Skip to content

Commit

Permalink
implement hooks handlers for codeceptjs
Browse files Browse the repository at this point in the history
  • Loading branch information
epszaw committed Jun 6, 2024
1 parent 7d24e58 commit d9aab94
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 32 deletions.
46 changes: 25 additions & 21 deletions packages/allure-codeceptjs/src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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(/^"(?<hookType>.+)" 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[] }) {
Expand Down
60 changes: 50 additions & 10 deletions packages/allure-codeceptjs/test/spec/hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,70 @@ 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();
});
`,
});

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,
}),
]),
}),
]),
);
Expand Down
2 changes: 1 addition & 1 deletion packages/allure-codeceptjs/test/spec/simple.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit d9aab94

Please sign in to comment.