Skip to content

fix(prefer-t-throws): detect try/catch asserting on caught error#360

Closed
Jackyzhangyi wants to merge 1 commit into
avajs:mainfrom
Jackyzhangyi:fix/prefer-t-throws-156
Closed

fix(prefer-t-throws): detect try/catch asserting on caught error#360
Jackyzhangyi wants to merge 1 commit into
avajs:mainfrom
Jackyzhangyi:fix/prefer-t-throws-156

Conversation

@Jackyzhangyi

@Jackyzhangyi Jackyzhangyi commented Feb 24, 2026

Copy link
Copy Markdown

Summary

This PR extends ava/prefer-t-throws to also flag a common pattern where a test uses try/catch to capture an error and assert on it, without using t.fail(). Fixes #156

Example currently used:

try {
  await request(requestOptions);
} catch (error) {
  t.true(error.statusCode === 500);
}

Preferred:

const error = await t.throwsAsync(request(requestOptions));

t.true(error.statusCode === 500);

What changed

  • Rule now detects two patterns:
  1. try block contains a direct t.fail() after code that should throw (existing)
  2. try block runs a single call (possibly awaited/returned) and the catch block asserts on the caught error (new)
  • Added unit tests for the new pattern (sync + async).
  • Updated docs for prefer-t-throws to describe the additional detection.

Notes

  • This PR does not attempt an auto-fix for the new pattern (non-trivial to rewrite while preserving variable usage). It only reports and guides toward t.throws() / t.throwsAsync().

IssueHunt Summary

Referenced issues

This pull request has been submitted to:


@sindresorhus

Copy link
Copy Markdown
Member

Thanks but I'm not interested in fully AI generated PRs. It's clear you didn't even look it over after submitting.

@sindresorhus

Copy link
Copy Markdown
Member

I suggest opening an issue instead about how you want the rule extended.

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.

Rule proposal: prefer-t-throws

2 participants