fix: detect synchronous throws of falsy values in t.throwsAsync() - #3472
Open
tsushanth wants to merge 1 commit into
Open
fix: detect synchronous throws of falsy values in t.throwsAsync()#3472tsushanth wants to merge 1 commit into
tsushanth wants to merge 1 commit into
Conversation
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.
Author
|
The |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
When the function passed to
t.throwsAsync()throws a falsy value synchronously — for examplethrow 0,throw false,throw null, orthrow ""— the guard that detects the synchronous throw uses a bare truthiness check:Because
0,false,null, and""are all falsy, theif (actual)branch is skipped. The code falls through to theisPromise(retval)check (retval isundefinedsince the function threw), which also fails, so AVA ultimately reports "Function returned: undefined" instead of the correct "Function threw synchronously. Uset.throws()instead: ".Fix
Change the sentinel check to
actual !== null, matching the initial value assigned before thetry/catch.This correctly detects any synchronous throw — including falsy thrown values — without affecting the normal case where no exception is thrown (
actualremainsnull).