Repository navigation
Closed
Labels
test_runnerIssues and PRs related to the test runner subsystem.
Description
Version
18.9.0
Platform
Darwin Eorzea.local 21.6.0 Darwin Kernel Version 21.6.0: Mon Aug 22 20:19:52 PDT 2022; root:xnu-8020.140.49~2/RELEASE_ARM64_T6000 arm64
Subsystem
node:test
What steps will reproduce the bug?
Nested beforeEach contexts do not work with describe/it:
import { beforeEach, describe, it } from 'node:test'
import { strict as assert } from 'node:assert'
let obj = {}
describe('outer', () => {
beforeEach(() => {
obj.outer = 'ok'
})
describe('inner', () => {
beforeEach(() => {
obj.inner = 'ok'
})
it('works', () => {
assert.deepEqual(obj, { outer: 'ok', inner: 'ok' })
})
})
})The output looks like this, a failure, due to what seems is only the inner beforeEach being run:
Expected values to be strictly deep-equal:
+ actual - expected
{
inner: 'ok',
- outer: 'ok'
}The same style of suite beforeEach nesting works with test
import { test } from 'node:test'
import { strict as assert } from 'node:assert'
let obj = {}
test('outer', async outer => {
outer.beforeEach(() => {
obj.outer = 'ok'
})
await outer.test('inner', async inner => {
inner.beforeEach(() => {
obj.inner = 'ok'
})
await inner.test('works', () => {
assert.deepEqual(obj, { outer: 'ok', inner: 'ok' })
})
})
})Outputs:
TAP version 13
# Subtest: inner
# Subtest: works
ok 1 - works
---
duration_ms: 1.353708
...
# Subtest: outer
1..1
ok 1 - inner
---
duration_ms: 2.2555
...
1..1
ok 1 - outer
---
duration_ms: 2.722792
...
1..1
# tests 1
# pass 1
# fail 0
# cancelled 0
# skipped 0
# todo 0
# duration_ms 4.435458How often does it reproduce? Is there a required condition?
Always reproduces
What is the expected behavior?
Nested test contexts should work the same whether you are using describe/it or test
What do you see instead?
The expected and actual outputs are in the repro example above
Additional information
No response
Metadata
Metadata
Assignees
Labels
test_runnerIssues and PRs related to the test runner subsystem.
Activity
cjihrig commentedon Oct 16, 2022
It looks like nested
beforeEach()does work withdescribe()/it()syntax. However,describe()with no tests does not trigger the hook. If you add anit()at the same level asdescribe('inner'), then it works as expected.@MoLow is this a bug or intended behavior? I don't know what other test runners do in this case, but I'm leaning toward bug.
[-]Nested suite beforeEach context works for `test` but not `describe`[/-][+]`describe` block without `it` does not trigger hooks[/+]scally commentedon Oct 16, 2022
FWIW, this works in Jest
MoLow commentedon Oct 18, 2022
this is not intended
test_runner: call {before,after}Each() on suites
test_runner: call {before,after}Each() on suites
test_runner: call {before,after}Each() on suites
test_runner: call {before,after}Each() on suites
test_runner: call {before,after}Each() on suites
fix: call {before,after}Each() on suites
fix: call {before,after}Each() on suites
fix: call {before,after}Each() on suites