Skip to content

fix: detect synchronous throws of falsy values in t.throwsAsync() - #3472

Open
tsushanth wants to merge 1 commit into
avajs:mainfrom
tsushanth:fix/throwsAsync-falsy-synchronous-throw
Open

fix: detect synchronous throws of falsy values in t.throwsAsync()#3472
tsushanth wants to merge 1 commit into
avajs:mainfrom
tsushanth:fix/throwsAsync-falsy-synchronous-throw

Conversation

@tsushanth

Copy link
Copy Markdown

Bug

When the function passed to t.throwsAsync() throws a falsy value synchronously — for example throw 0, throw false, throw null, or throw "" — the guard that detects the synchronous throw uses a bare truthiness check:

let actual = null;
try {
    retval = thrower();
} catch (error) {
    actual = error;
}

if (actual) { // ← misses falsy thrown values

Because 0, false, null, and "" are all falsy, the if (actual) branch is skipped. The code falls through to the isPromise(retval) check (retval is undefined since the function threw), which also fails, so AVA ultimately reports "Function returned: undefined" instead of the correct "Function threw synchronously. Use t.throws() instead: ".

Fix

Change the sentinel check to actual !== null, matching the initial value assigned before the try/catch.

if (actual !== null) {

This correctly detects any synchronous throw — including falsy thrown values — without affecting the normal case where no exception is thrown (actual remains null).

@tsushanth

Copy link
Copy Markdown
Author

The Node.js (^26, ubuntu-latest) failure appears pre-existing and unrelated to this PR — the main branch CI also has intermittent Node 26 failures (run #25970521309). This PR passes on Node 22, 24, and Node 26 on Windows and macOS.

@tsushanth
tsushanth force-pushed the fix/throwsAsync-falsy-synchronous-throw branch 7 times, most recently from e7b487b to 2f06e3e Compare August 15, 2026 11:26
@tsushanth
tsushanth force-pushed the fix/throwsAsync-falsy-synchronous-throw branch from 2f06e3e to d2d46aa Compare August 16, 2026 06:26
@tsushanth
tsushanth force-pushed the fix/throwsAsync-falsy-synchronous-throw branch from d2d46aa to e4ba68d Compare August 17, 2026 15:40
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.

2 participants