babel-plugin-ava-throws-helper
raw JSON → 1.0.0 verified Sat Apr 25 auth: no javascript maintenance
A Babel plugin that provides compile-time protection against improper use of `t.throws()` in the AVA test framework. It transforms code to detect when a value rather than a function is passed, issuing clear error messages. This plugin is internal to AVA and not intended for direct use; version 1.0.0 is the current stable release, with no recent updates. It differs from eslint-plugin-ava by offering a more robust, AST-based solution for a common mistake where users write `t.throws(foo())` instead of `t.throws(() => foo())`.
Common errors
error Error: Couldn't find preset "stage-2" relative to directory ↓
cause Missing required Babel preset
fix
Install the missing preset: npm install babel-preset-stage-2 --save-dev
error ReferenceError: test is not defined ↓
cause AVA test runner not properly set up
fix
Ensure AVA is installed and you are running tests with 'npx ava'
Warnings
breaking This plugin only works with AVA's t.throws() and may not be compatible with future AVA versions. ↓
fix Ensure AVA version compatibility; this plugin is internal and may be removed in future AVA releases.
gotcha The plugin does not prevent all runtime errors; it only aids detection when used via Babel. ↓
fix Always wrap t.throws() arguments in a function as best practice.
Install
npm install babel-plugin-ava-throws-helper yarn add babel-plugin-ava-throws-helper pnpm add babel-plugin-ava-throws-helper Imports
- default wrong
const plugin = require('babel-plugin-ava-throws-helper')correctimport plugin from 'babel-plugin-ava-throws-helper' - ava-throws-helper wrong
{ "plugins": ["ava-throws-helper"] }correct// .babelrc { "plugins": ["babel-plugin-ava-throws-helper"] }
Quickstart
// Install the plugin
npm install babel-plugin-ava-throws-helper --save-dev
// .babelrc
{
"plugins": ["babel-plugin-ava-throws-helper"]
}
// Using AVA test
import test from 'ava';
test('throws test', t => {
t.throws(() => { throw new Error('err'); }); // OK
});
test('invalid throws', t => {
// This would be transformed to produce a helpful error at runtime
t.throws(new Error('err'));
});