javascriptava

How can I use globally installed AVA for unit tests?


I am trying to implement unit testing with AVA for my javascript app.

I have installed AVA globally.

I have created a basic ava.config.cjs file:

module.exports = {
    files: ['tests/**', '!tests/**/{fixtures,helpers}/**']
};

And I have a test file:

import test from 'ava';
// const { test } = require('ava');

test('foo', t => {
    t.pass();
});

test('bar', async t => {
    const bar = Promise.resolve('bar');
    t.is(await bar, 'bar');
});

I have tried both 'import', and 'require' but neither work.

When I run ava from the command line I get an error that the module 'ava' can't be found.

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'ava'

Or

Error: Cannot find module 'ava'

Depending on which method I try to include AVA...

What would I need to do to get this working?


Solution

  • This depends on your machine setup. Node.js here can't find AVA.

    That said, installing AVA globally is not recommended. Ideally you install within your project and run with npx ava (or preferred-package-manager-equivalent).