This tutorial will explain from the project setup. To skip the setup you can go to installation
Setup
Create new node.js project
$ npm init --y
Create a simple function to test
/** * Add two numbers * @param {number} a a number * @param {number} b a number * @returns a + b */ exports.add = (a, b) => a + b;
add.js
You can test that function with
console.log
Installation
Install ava
to the project
$ npm i -D ava
Add test
script that runs the ava
binary
{ // ... "scripts": { // ... "test": "ava" } }
package.json
Testing
Create a test file to test the add
function
const test = require('ava'); const { add } = require('./add'); test('should correctly add 1 and 1', t => { t.is(add(1, 1), 2); });
add.test.js
Then you can run the test script
$ npm test
Tips
To get types/intellisense in vscode
, modify how to import ava
- const test = require("ava");
+ const { default: test } = require("ava");
References
- ava Repository https://github.com/avajs/ava
- using TypeScript https://github.com/avajs/ava/blob/main/docs/recipes/typescript.md