Skip to content

Support for async describe body in node:test #36876

Description

@brc-dd

#28837 was closed but the async suit thing mentioned in the issue body is still not implemented.

In Node.js and Bun this works fine:

import assert from 'node:assert'
import { describe, it } from 'node:test'

describe('Async Test Suite', async () => {
  const i = await Promise.resolve(42)
  let assertionCount = 0

  it('should handle async operations', async () => {
    const result = await Promise.resolve(i * 2)
    assertionCount++
    assert.strictEqual(result, 84)
  })

  describe('Nested Async Suite', async () => {
    const j = await Promise.resolve(7)

    it('should handle nested async operations', async () => {
      const result = await Promise.resolve(i + j)
      assertionCount++
      assert.strictEqual(result, 49)
    })

    describe('Even More Nested Async Suite', async () => {
      const k = await Promise.resolve(3)

      it('should handle deeply nested async operations', async () => {
        const result = await Promise.resolve(i * j * k)
        assertionCount++
        assert.strictEqual(result, 882)
      })
    })
  })

  it('should have the correct number of assertions after async tests', () => {
    assert.strictEqual(assertionCount, 3)
  })
})
❯ node --test
▶ Async Test Suite
  ✔ should handle async operations (0.286208ms)
  ▶ Nested Async Suite
    ✔ should handle nested async operations (0.076709ms)
    ▶ Even More Nested Async Suite
      ✔ should handle deeply nested async operations (0.057375ms)
    ✔ Even More Nested Async Suite (0.120917ms)
  ✔ Nested Async Suite (0.306166ms)
  ✔ should have the correct number of assertions after async tests (0.046083ms)
✔ Async Test Suite (1.104875ms)
ℹ tests 4
ℹ suites 3
ℹ pass 4
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 84.935208

❯ bun test
bun test v1.3.14 (0d9b296a)

foo.test.ts:
✓ Async Test Suite > should handle async operations [0.43ms]
✓ Async Test Suite > Nested Async Suite > should handle nested async operations [0.04ms]
✓ Async Test Suite > Nested Async Suite > Even More Nested Async Suite > should handle deeply nested async operations [0.03ms]
✓ Async Test Suite > should have the correct number of assertions after async tests

 4 pass
 0 fail
Ran 4 tests across 1 file. [146.00ms]

While on Deno, this throws:

Check foo.test.ts
running 1 test from ./foo.test.ts
Async Test Suite ...
  should handle async operations ... INCOMPLETE
Async Test Suite ... FAILED (due to incomplete steps) (2ms)

 ERRORS 

Async Test Suite ... should handle async operations => ./foo.test.ts:8:3
error: Didn't complete before parent. Await step with `await t.step(...)`.

 FAILURES 

Async Test Suite ... should handle async operations => ./foo.test.ts:8:3

FAILED | 0 passed | 1 failed (1 step) (3ms)

error: Test failed

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions