ava - a futuristic test runner

Post on 14-Apr-2017

487 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

AVA - a futuristic test runner

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

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

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.

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

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.

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.

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.

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

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

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

Demo time

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

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

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

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

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

top related