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

test runner: before and after hooks in a test or outside a describe method are not executed #47944

Closed
kalmanl opened this issue May 10, 2023 · 2 comments

Comments

@kalmanl
Copy link

kalmanl commented May 10, 2023

Version

v20.1.0

Platform

Darwin Kalmans-MacBook-Pro.local 22.4.0 Darwin Kernel Version 22.4.0: Mon Mar 6 21:00:41 PST 2023; root:xnu-8796.101.5~3/RELEASE_ARM64_T8103 x86_64

Subsystem

test_runner

What steps will reproduce the bug?

run the following code, logs are not written to console

import { describe, it, test, before, after } from 'node:test'

before(() => {
    console.log("before outside")
})

after(() => {
    console.log("after outside")
})

describe("describe block", () => {
    test("test inside", () => {
        before(() => {
            console.log("before inside test")
        });

        console.log("test inside")
    })

    it("test inside", () => {
        before(() => {
            console.log("before inside it")
        })

        console.log("it inside")
    })
})

test("outside test", () => {
    console.log("test outside")
})

it("outside it", () => {
    console.log("it outside")
})

How often does it reproduce? Is there a required condition?

each execution, no required condition

What is the expected behavior? Why is that the expected behavior?

before and after hooks should be executed

What do you see instead?

test inside
it inside
before outside
test outside
it outside
▶ describe block
  ✔ test inside (0.9725ms)
  ✔ test inside (0.610209ms)
▶ describe block (2.832542ms)

✔ outside test (0.516125ms)
✔ outside it (0.097375ms)
ℹ tests 4
ℹ suites 1
ℹ pass 4
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 13.303583

Additional information

Hi,
I've been playing with the test framework and trying to migrate tests from jest to nodes new tests runner. I found a few cases where before and after hooks are not executed

I don't know if this is a bug or works as designed, but the documentation could maybe be more clear when the hooks our not supported.
maybe node can warn that the hook is not supposed.
In jest for example a hook in the test itself is not supported and if doing so a warning is displayed

Hooks cannot be defined inside tests. Hook of type "beforeAll" is nested within "test inside".

If there are changes needed I would be glad to try to do the modification if it's not too complicated

@MoLow
Copy link
Member

MoLow commented May 10, 2023

this duplicates #47518 (that has already been fixed on node 20.1.0)

@MoLow MoLow closed this as completed May 10, 2023
@MoLow MoLow closed this as not planned Won't fix, can't repro, duplicate, stale May 10, 2023
@MoLow MoLow closed this as completed May 10, 2023
@kalmanl
Copy link
Author

kalmanl commented May 10, 2023

Hi @MoLow,
Thanks for the reply

I tied to run the code with node 20.1.0

  • I see that the before hook is run after the test in the describe block.
  • also, the after hook was not run at all
  • also the before hooks in the inside tests didn’t run. is this an issue or this is not supposed like in jest?

the output:

test inside
it inside
before outside
test outside
it outside
▶ describe block
  ✔ test inside (0.9725ms)
  ✔ test inside (0.610209ms)
▶ describe block (2.832542ms)

✔ outside test (0.516125ms)
✔ outside it (0.097375ms)
ℹ tests 4
ℹ suites 1
ℹ pass 4
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 13.303583

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants