ava/prefer-t-throws
February 8, 2026 ยท View on GitHub
๐ Prefer t.throws() or t.throwsAsync() over try/catch.
๐ผ This rule is enabled in the โ
recommended config.
Using try/catch with t.fail() to test that a function throws is verbose and error-prone. AVA provides t.throws() and t.throwsAsync() which are more concise and produce better failure output.
This rule flags try/catch blocks inside test functions where the try block contains a direct t.fail() call, since this is a strong signal the pattern should be replaced with t.throws() or t.throwsAsync().
Examples
import test from 'ava';
// โ
test('main', t => {
try {
foo();
t.fail();
} catch (error) {
t.true(error.message === 'expected');
}
});
// โ
test('main', t => {
const error = t.throws(() => foo());
t.true(error.message === 'expected');
});
// โ
test('main', async t => {
try {
await fetchData();
t.fail();
} catch (error) {
t.is(error.statusCode, 500);
}
});
// โ
test('main', async t => {
const error = await t.throwsAsync(fetchData());
t.is(error.statusCode, 500);
});