First of all AVA is simple. It’s really easy to set up. First install ava:
npm install ava -g, then run ava by running
ava. AVA will by default detect tests using common patterns. Let’s make one:
This is it, simple as that. Maybe you are thinking, ES6 code? Don’t you have to transpile that? No, AVA supports ES6 by default, awesome!
Tests run in parallel by default, this means that in general they will run faster. You’re able to run them in serial if needed. However, having your tests run in parallel also forces you to make sure all your tests are completely independent of each other, which is always a good practice.
AVA supports asynchronous testing by default:
We let AVA know that one assertion is coming up, so AVA will wait for the timeout to finish. If you return a promise you don’t have to wait at all.
Another nifty feature that you can only run the test that you are working on. This can be handy if you have a lot of tests and you want to focus one fixing one at a time:
It will now only run the second test.
AVA has no mocking built in, to mock functions just use Sinon.js. Install sinon by running
npm install sinon --save-dev. Then use it in your tests:
Installing AVA globally is okay to play around with it. But in a “real” project you want your dependencies to be local. To setup AVA for your project simply run
npm install ava --save-dev. Then add the following script to your package.json:
You can now run your tests using
npm run test or
npm run test:watch if you want to let them automatically run on change.
Getting code coverage reports is also really easy. AVA runs tests using so called “subprocesses”, this is why just using istanbul for code coverage will not work. Luckily there is a wrapper around istanbul that supports subprocesses called nyc. Install nyc using
npm install nyc --save-dev, then run it with