Skip to content

describe block without it does not trigger hooks #45028

Description

@scally

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.435458

How 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

Activity

  1. cjihrig commented on Oct 16, 2022

    @cjihrig
    Contributor

    It looks like nested beforeEach() does work with describe()/it() syntax. However, describe() with no tests does not trigger the hook. If you add an it() at the same level as describe('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.

  2. changed the title [-]Nested suite beforeEach context works for `test` but not `describe`[/-] [+]`describe` block without `it` does not trigger hooks[/+] on Oct 16, 2022
  3. scally commented on Oct 16, 2022

    @scally
    Author

    FWIW, this works in Jest

  4. MoLow commented on Oct 18, 2022

    @MoLow
    Member

    this is not intended

  5. added
    test_runnerIssues and PRs related to the test runner subsystem.
    on Oct 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    test_runnerIssues and PRs related to the test runner subsystem.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions