Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CodeceptJS hooks fixtures #994

Merged
merged 2 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: String.raw`"before all" hook: BeforeSuite for "sample-scenario"`,
befores: expect.arrayContaining([
expect.objectContaining({
status: Status.PASSED,
stage: Stage.FINISHED,
}),
]),
afters: [],
}),
expect.objectContaining({
name: String.raw`"before each" hook: Before for "sample-scenario"`,
befores: expect.arrayContaining([
expect.objectContaining({
status: Status.PASSED,
stage: Stage.FINISHED,
}),
]),
afters: [],
}),
expect.objectContaining({
name: String.raw`"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: String.raw`"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
Loading