diff --git a/packages/allure-jest/src/environmentFactory.ts b/packages/allure-jest/src/environmentFactory.ts index 67c6b9586..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) { @@ -194,6 +193,8 @@ const createJestEnvironment = (Base: T): T => 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, @@ -256,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 8b8e0a5ed..260ef6948 100644 --- a/packages/allure-jest/test/spec/hooks.test.ts +++ b/packages/allure-jest/test/spec/hooks.test.ts @@ -111,6 +111,42 @@ 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": `