diff --git a/packages/allure-jest/src/environmentFactory.ts b/packages/allure-jest/src/environmentFactory.ts index dbc990a6d..dc826f3cc 100644 --- a/packages/allure-jest/src/environmentFactory.ts +++ b/packages/allure-jest/src/environmentFactory.ts @@ -95,6 +95,9 @@ const createJestEnvironment = (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; @@ -124,15 +127,11 @@ const createJestEnvironment = (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) { @@ -191,10 +190,11 @@ const createJestEnvironment = (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, @@ -216,7 +216,7 @@ const createJestEnvironment = (Base: T): T => ...getEnvironmentLabels(), ], }, - [scopeUuid], + this.runContext.scopes, ); this.runtime.updateTest(testUuid, (result) => { @@ -257,6 +257,22 @@ const createJestEnvironment = (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(); diff --git a/packages/allure-jest/test/spec/hooks.test.ts b/packages/allure-jest/test/spec/hooks.test.ts index 1e918dcf8..5237c7f67 100644 --- a/packages/allure-jest/test/spec/hooks.test.ts +++ b/packages/allure-jest/test/spec/hooks.test.ts @@ -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": `