Get started with AVA unit tests

Set up AVA, select tests, handle async assertions, and avoid confusing file isolation with isolation between concurrent tests.

Best practices
Illustration of software development and testing

AVA is a JavaScript test runner with a compact API, built-in assertions, snapshot support, and concurrent test execution. Tests within a file run concurrently by default, so shared mutable state can still cause interference. Check the engines requirement of the AVA version you install before choosing a Node runtime.

Set up a project and write a test

In a new project directory:

npm init -y
npm pkg set type=module
npm install --save-dev ava
npm pkg set scripts.test=ava

Create test.js:

import test from 'ava';

test('adds two numbers', t => {
  t.is(1 + 2, 3);
});

Run npm test. The module setting allows the example to use ESM imports. AVA's assertion interface includes methods such as t.is(), t.deepEqual(), and t.throwsAsync().

Add tests and narrow execution

With the same test import, these examples show independent cases, a focused case, and a skipped case:

test('case A', t => { t.is(2 * 3, 6); });
test('case B', t => { t.deepEqual([1, 2], [1, 2]); });
test.only('focus while debugging', t => { t.true(Number.isInteger(3)); });
test.skip('temporarily skipped', t => { t.pass(); });

Remove .only before committing a focused debugging change so the rest of the suite runs again. Run a single file with npx ava test.js, or match titles with a command such as npx ava --match='*login*'.

Test asynchronous behavior

These fragments assume your application provides fetchUser():

test('returns a user', async t => {
  const user = await fetchUser(1);
  t.is(user.name, 'Alex');
});

test('rejects an invalid user ID', async t => {
  await t.throwsAsync(() => fetchUser(-1));
});

Await asynchronous work and the asynchronous assertion. For mocks and stubs, combine AVA with a tool such as Sinon or a small hand-written dependency stub. AVA concentrates on running tests and checking results rather than providing every mocking facility itself.

Frequently asked questions

When should I choose AVA instead of Jest or Vitest?

AVA is worth evaluating for a Node library when its concurrency model and compact API fit the suite. Jest supplies an integrated set of mocks, snapshots, and related tooling. Vitest integrates with Vite's configuration and transformation workflow. Choose based on the actual environment and required integrations, not an assumed speed ranking.

Why do concurrent tests interfere with one another?

AVA isolates test files, normally using worker threads, with a process-based mode available. It does not give each test in the same file a private copy of every shared variable. Avoid shared mutable state or use test.serial when appropriate. External resources such as databases still need independent records, schemas, or other isolation.

Does AVA run TypeScript automatically?

Follow AVA's TypeScript guidance for precompilation or a compatible loading approach. A tsconfig file does not execute TypeScript by itself. Executing a test also does not necessarily perform type checking; keep that as an explicit check when needed.

References

Related reading

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting