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);
});