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

When the function passed to `t.throwsAsync()` throws a falsy value
(e.g. `throw 0`, `throw false`, `throw null`, `throw ""`), the previous
truthiness check `if (actual)` did not detect the throw. The code would
fall through and produce a misleading "Function returned: undefined"
error rather than the intended "Function threw synchronously. Use
`t.throws()` instead" message.

Fix by checking `actual !== null` instead, matching the sentinel value
used to initialise `actual` before the try/catch.
@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.

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