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

fix(allure-jest): beforeEach/afterEach affect all tests in the suite, beforeAll/afterAll don't affect tests in sub-suites #1052

Merged
merged 2 commits into from
Jul 8, 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
32 changes: 24 additions & 8 deletions packages/allure-jest/src/environmentFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ const createJestEnvironment = <T extends typeof JestEnvironment>(Base: T): T =>
case "test_start":
this.#handleTestStart(event.test);
break;
case "test_done":
this.#handleTestDone();
break;
case "test_todo":
this.#handleTestTodo(event.test);
break;
Expand Down Expand Up @@ -124,15 +127,11 @@ const createJestEnvironment = <T extends typeof JestEnvironment>(Base: T): T =>
}

#handleSuiteStart() {
const scopeUuid = this.runtime.startScope();

this.runContext.scopes.push(scopeUuid);
this.#startScope();
}

#handleSuiteEnd() {
const scopeUuid = this.runContext.scopes.pop()!;

this.runtime.writeScope(scopeUuid);
this.#stopScope();
}

#handleHookStart(hook: Circus.Hook) {
Expand Down Expand Up @@ -191,10 +190,11 @@ const createJestEnvironment = <T extends typeof JestEnvironment>(Base: T): T =>
return;
}

const scopeUuid = last(this.runContext.scopes);
const threadLabel = ALLURE_THREAD_NAME || JEST_WORKER_ID || process.pid.toString();
const hostLabel = ALLURE_HOST_NAME || hostname;
const packageLabel = dirname(this.testPath).split(sep).join(".");

this.#startScope();
const testUuid = this.runtime.startTest(
{
name: test.name,
Expand All @@ -216,7 +216,7 @@ const createJestEnvironment = <T extends typeof JestEnvironment>(Base: T): T =>
...getEnvironmentLabels(),
],
},
[scopeUuid],
this.runContext.scopes,
);

this.runtime.updateTest(testUuid, (result) => {
Expand Down Expand Up @@ -257,6 +257,22 @@ const createJestEnvironment = <T extends typeof JestEnvironment>(Base: T): T =>
});
}

#handleTestDone() {
this.#stopScope();
}

#startScope() {
const scopeUuid = this.runtime.startScope();

this.runContext.scopes.push(scopeUuid);
}

#stopScope() {
const scopeUuid = this.runContext.scopes.pop()!;

this.runtime.writeScope(scopeUuid);
}

#handleTestPass(test: Circus.TestEntry) {
const testUuid = this.runContext.executables.pop();

Expand Down
65 changes: 65 additions & 0 deletions packages/allure-jest/test/spec/hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,71 @@ it("reports before and after each hooks", async () => {
);
});

it("should report one beforeEach/afterEach per test", async () => {
const { tests, groups } = await runJestInlineTest({
"sample.test.js": `
beforeEach(() => {});
afterEach(() => {});

it("foo", () => {});
it("bar", () => {});
`,
});

const [{ uuid: test1Uuid }, { uuid: test2Uuid }] = tests;

expect(groups).toHaveLength(4);
expect(groups).toEqual(
expect.arrayContaining([
expect.objectContaining({
name: "beforeEach",
children: [test1Uuid],
}),
expect.objectContaining({
name: "beforeEach",
children: [test2Uuid],
}),
expect.objectContaining({
name: "afterEach",
children: [test1Uuid],
}),
expect.objectContaining({
name: "afterEach",
children: [test2Uuid],
}),
]),
);
});

it("should report beforeAll/afterAll for tests in sub-suites", async () => {
const { tests, groups } = await runJestInlineTest({
"sample.test.js": `
beforeAll(() => {});
afterAll(() => {});

describe("", () => {
it("foo", () => {});
});
`,
});

const [{ uuid: testUuid }] = tests;

expect(groups).toHaveLength(2);
expect(groups).toEqual(
expect.arrayContaining([
expect.objectContaining({
name: "beforeAll",
children: [testUuid],
}),
expect.objectContaining({
name: "afterAll",
children: [testUuid],
}),
]),
);
});

it("reports failed hooks", async () => {
const { tests, groups } = await runJestInlineTest({
"sample.test.js": `
Expand Down
Loading