Skip to content

fix(ext/node): prevent top-level await test(...) deadlock in node:test - #33947

Merged
bartlomieju merged 3 commits into
mainfrom
claude/fix-deno-32326-ZawOY
May 13, 2026
Merged

bartlomieju merged 3 commits into
mainfrom
claude/fix-deno-32326-ZawOY

Conversation

@lunadogbot

Copy link
Copy Markdown
Contributor

Summary

Fixes #32326.

The test() and suite() entry points in node:test returned a promise
that only resolved when the underlying Deno.test() callback executed.
Because Deno.test() defers execution until after the module finishes
evaluating, the following common Node.js pattern would deadlock:

import test from "node:test";
import assert from "node:assert";

await test("a test", () => {
  assert.strictEqual(1 + 1, 2);
});

The module could not finish evaluating because of the await, and the
test could not start because the module had not finished evaluating,
producing error: Top-level await promise never resolved.

The fix returns an already-resolved promise from prepareDenoTest and
prepareDenoTestForSuite. The test/suite is still registered with the
Deno test runner and runs (and is reported) normally; only the awaited
promise no longer waits for completion. Subtests and await t.test(...)
inside a parent test are unaffected because they go through
denoContext.step(), which executes synchronously inside the parent.

Test plan

  • Original reproduction from the issue now passes:
    $ deno test -A a.test.mjs
    running 1 test from ./a.test.mjs
    a test ... ok (0ms)
    ok | 1 passed | 0 failed
    
  • Multiple sequential await test(...) calls register and run, including failures.
  • await suite(...) works the same way.
  • Subtests via await t.test(...) still work.
  • Existing tests/specs/node/node_test_* spec tests still produce the same output.
  • New regression spec test added at tests/specs/node/node_test_top_level_await/.

Generated by Claude Code

The `test()` and `suite()` functions in `node:test` returned a promise that
only resolved when the underlying `Deno.test()` callback executed. Because
`Deno.test()` defers execution until after the module finishes evaluating,
top-level `await test(...)` would block module evaluation forever, producing
"Top-level await promise never resolved".

Return an already-resolved promise from the top-level entry points so the
await unblocks. The test still runs and is reported by the Deno test runner.

Fixes #32326
@CLAassistant

CLAassistant commented May 10, 2026 •

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ bartlomieju
❌ claude
You have signed the CLA already but the status is still pending? Let us recheck it.

@fibibot fibibot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two issues — one mechanical CI fix, one substance question.

Mechanical: CI is red because of a missing trailing newline

All 14 test specs shards fail on the new node_test_top_level_await::suite_await spec test. The CI log on test specs (1/2) debug linux-x86_64 (job id 75243625358) shows the actual output matching the expected output character-for-character — <matched everything> per the matcher debug. The failure reason is:

!!!! PROBLEM: Missing final newline at end of expected output !!!!

The tests/specs/node/node_test_top_level_await/suite.out file is missing its trailing newline. (test.out apparently has it — test_await passes; suite_await fails the same matcher.) Adding a \n to the end of suite.out should clear this. The runner is strict about it because spec output files without trailing newlines have historically masked real assertion drift.

Substance: test() returning resolved-immediately diverges from Node

See inline. Not necessarily a blocker, but worth a maintainer's call.

// completion: top-level `await test(...)` would deadlock the module. The
// test still runs and is reported by the runner; we return an already
// resolved promise so the await unblocks.
return PromiseResolve();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix unblocks the deadlock — agreed that's a real bug worth fixing. But returning PromiseResolve() immediately changes the semantic contract of node:test's test() in a way that diverges from Node. Per Node's docs:

Returns: <Promise> Fulfilled with undefined once the test completes...

With this PR, the following Node pattern produces different behavior:

import test from 'node:test';

await test('first', async (t) => { /* slow async work */ });
console.log('AFTER FIRST'); // Node: runs AFTER first completes. Deno (after PR): runs BEFORE first completes.
await test('second', async (t) => { /* depends on side-effects of first */ });

In Node, the console.log line runs after first completes; user code can rely on side-effects of the awaited test. In Deno after this PR, both awaits unblock immediately and console.log and second race against first's actual execution (which the Deno runner schedules after module evaluation).

Correctness-wise the issue tracker (#32326) is a deadlock and a clear bug, so this fix is net-positive. But it introduces a different subtle bug: code that awaits test() for sequencing purposes will silently run out of order. Worth either:

  1. Documenting the divergence in the JSDoc on prepareDenoTest, so future readers understand why this isn't matching Node.
  2. Tracking the deeper fix (driving the test runner forward eagerly when await test() is hit at top-level, so the awaited promise can actually resolve on completion). The comment at :579 hints at this — "only executes registered tests after the module has finished evaluating" — and a follow-up issue would let users know there's a known semantic gap.

Neither is blocking; the deadlock fix is the right priority. Just want a maintainer to weigh in on whether the divergence needs an issue/docstring before merge.

- Add trailing newline to suite.out so the spec runner matches the
  two trailing blank lines in `deno test` suite output.
- Make the inline comment in `prepareDenoTest` explicit about diverging
  from Node's contract (Node resolves the returned promise on
  completion; we resolve immediately to avoid deadlock) and call out
  the ordering trade-off for `await test('a'); await test('b')`.
@bartlomieju

Copy link
Copy Markdown
Member

Thanks for the review.

  • Mechanical (newline): Fixed. suite.out now ends with \n\n to match the two trailing blank lines that deno test prints after the summary for suite output (which is what tripped the strict matcher — test.out already matched because non-suite output only emits one trailing blank line).
  • Substance (Node divergence): Tightened the inline comment in prepareDenoTest to make the divergence explicit — Node resolves the returned promise on completion, we resolve immediately to avoid the deadlock, and code that relies on await test('a'); await test('b') for sequencing will see them run out of order. The deeper fix (driving the runner forward when top-level await hits) would be a larger change; I'd lean toward shipping this and revisiting if users hit the ordering trade-off in practice.

The expected output was missing the trailing blank line that
`deno test` emits after the summary, causing the test_await
spec test to fail on CI.
@fibibot

fibibot commented May 12, 2026

Copy link
Copy Markdown
Contributor

Build infra flake — all 6 failed jobs hit fatal: unable to access 'https://github.com/denoland/deno/': The requested URL returned error: 500 during git clone, unrelated to this PR. Re-running.

@bartlomieju
bartlomieju merged commit 8cb7e56 into main May 13, 2026
247 of 253 checks passed
@bartlomieju
bartlomieju deleted the claude/fix-deno-32326-ZawOY branch May 13, 2026 15:55
littledivy pushed a commit to crowlKats/deno that referenced this pull request Jun 10, 2026
…est (denoland#33947)

Fixes denoland#32326.

The `test()` and `suite()` entry points in `node:test` returned a
promise
that only resolved when the underlying `Deno.test()` callback executed.
Because `Deno.test()` defers execution until after the module finishes
evaluating, the following common Node.js pattern would deadlock:

```js
import test from "node:test";
import assert from "node:assert";

await test("a test", () => {
  assert.strictEqual(1 + 1, 2);
});
```

The module could not finish evaluating because of the `await`, and the
test could not start because the module had not finished evaluating,
producing `error: Top-level await promise never resolved`.

The fix returns an already-resolved promise from `prepareDenoTest` and
`prepareDenoTestForSuite`. The test/suite is still registered with the
Deno test runner and runs (and is reported) normally; only the awaited
promise no longer waits for completion. Subtests and `await t.test(...)`
inside a parent test are unaffected because they go through
`denoContext.step()`, which executes synchronously inside the parent.
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

Successfully merging this pull request may close these issues.

node:test top-level await test() pattern fails in Deno

5 participants