AVA - a futuristic test runner

14
AVA - a futuristic test runner What it is, what problems it solves and when to use it

Transcript of AVA - a futuristic test runner

Page 1: AVA - a futuristic test runner

AVA - a futuristic test runner

What it is, what problems it solves and when to use it

Page 2: AVA - a futuristic test runner

About me• Several production-grade node apps in the wild

• muuuf• Up & Front• EthPort (blockchain 2.0)• Several big sites where I freelanced

• Maintainer of jade/pug and named-routes, help out at CoffeeScript, bluebird and sequelize

• Run London & Berlin Node.js meetup groups• Actually a Maths PhD

Page 3: AVA - a futuristic test runner

My problemI implemented something that was essentially a HTTP proxy. I wanted a lot of integrations tests using real HTTP requests. Once you study the HTTP protocol, you’ll want to test a lot of cases incl. different transfer encodings, cookies, proxy caches and tcp keep-alives.

Especially the cache/max-age tests were wasteful since you need to wait at least 2 full seconds for a cached resource to be invalided.

My test suite quickly took multiple minutes to run even though the CPU was not utilized.

Page 4: AVA - a futuristic test runner

Introducing AVA

AVA is a new test runner for node.js that• takes advantage of node’s async nature and runs your

tests concurrently• runs test files in parallel as separate processes, for

better performance and an isolated environment• comes with batteries included; it bundles babel,

power-assert and others to hit the ground running

Page 5: AVA - a futuristic test runner

ConcurrencySimple test example in mocha:

These 500 tests are run serially, or “blocking”. Each test must finish completely before the next one can start, even if the CPU is at 0%, waiting for I/O.

Page 6: AVA - a futuristic test runner

ConcurrencyThe same test example in ava:

As soon as the first test yields to the event queue (`await request`), the second one is started. When it yields to the event queue, the third one is started, and so on.This achieves concurrency on 1 cpu core without using threads.Also forces you to isolate your tests from each other.

Page 7: AVA - a futuristic test runner

ParallelismMocha runs a parent process with the tests running in one child process, utilizing at most 1 cpu core:

AVA runs a parent process with every test file running in a separate child process, utilizing as many cpu cores as there are test files:

This forces you to avoid implicit globals. You can use the `require` property to load configurations into each process.

Page 8: AVA - a futuristic test runner

Batteries includedAVA comes with babel and power-assert to support:• ES2015 language features• Enhanced assertion messages• Promise support• Generator function support• Async function support• Observable support• Optional TAP output• Clean stack traces

Page 9: AVA - a futuristic test runner

Batteries includedIn particular, power-assert gives error messages like these:

Page 10: AVA - a futuristic test runner

Simple configurationAVA can be configured right inside your package.json:

Page 11: AVA - a futuristic test runner

Demo time

Or, how I stopped worrying and learned to love live-coding

See the code at https://github.com/alubbe/avatalk

Page 12: AVA - a futuristic test runner

Demo time

Or, how I stopped worrying and learned to love live-coding

Watch at https://raw.githubusercontent.com/alubbe/avatalk/master/video%20mocha.gif

Page 13: AVA - a futuristic test runner

Demo time

Or, how I stopped worrying and learned to love live-coding

Watch at https://raw.githubusercontent.com/alubbe/avatalk/master/video%20ava.gif

Page 14: AVA - a futuristic test runner

To wrap up• AVA is great for test suites with a lot of async

operations• It helps you write atomic tests that do not depend on

global state or the state of other tests• If you do have tests that depend on each other, you can

ava to execute them serially• AVA supports the .only & .skip syntax and can run any

TAP-compatible reporter for the output • But, it will not make your unit tests run faster on a

machine with 1 cpu core• It can potentially DDOS the thing you are testing and it

won’t speed up karma processes or database queries