AVA
- Futuristic JavaScript test runner
- https://github.com/avajs/ava
Why AVA?
- Simple test syntax like tape (e.g. t.deepEqual, t.pass, …)
- No implicit globals like mocha (e.g
describe,it) - Runs tests concurrently (troll!)
- Process isolation for each test file
- Support ES2017 by Babel (stage-4)
- Support:
- Promise
- Generator
- async/await
- Observable
- …
Basic usage
- Take a look
|
|
- Run
|
|
- Simple result

- Verbose result

- Tap style result
- Can integrate with any tap reporters.

- Integrate with tap-nyan

Enhanced assertion messages
- Fail case
|
|
- Result
- case 1

- case 2

- You can use any assertion library (e.g. chai, node assert).
- But the magic assert only supports with default assertion library.
Babel
- Write babel presets in
avafield ofpackage.json.
|
|
- Or just inherit from
.babelrc.
|
|
- If you need to load the extra babel modules, use
requireoption in thebabelfield.
|
|
- Or you want to load from an entrypoint.
|
|
./test/set-babel.js
|
|
Test coverage
|
|

Promise
|
|
Generator
|
|
async/await
|
|
Observable
|
|
Callback
- use
test.cb() t.end()only work withtest.cb(), must be called at the end of callback.
|
|
Assertions
.pass([message])/.fail([message]).truthy(value, [message])/.falsy(value, [message]).true(value, [message])/.false(value, [message]).is(value, expected, [message])/.not(value, expected, [message]).deepEqual(value, expected, [message])/.notDeepEqual(value, expected, [message]).throws(function|promise, [error, [message]])/.notThrows(function|promise, [message]).regex(contents, regex, [message])/.notRegex(contents, regex, [message]).ifError(error, [message]).snapshot(contents, [message])
Snapshot
- AVA can take a snapshot which uses jest-snapshot under the hood.
Example:
hello.jsx
|
|
react.spec.js
|
|
- It will create a
__snapshots__folder in the test folder and contains the following result.- First run

- Snapshot result

- And when the result is not equal to the snapshot, it will fail the test.

- If you want to update the snapshot, just use
--update-snapshotsor-uoptions.
https://d2mxuefqeaa7sj.cloudfront.net/s_40E81CC28D06286B542B06554F0AD203FFC3CC719886DFCA8E86E84FE46DB9D5_1495473968079_image.png
before/after Hooks
test.before(): Hooks before the first test.
|
|
test.after(): Hooks after the last test.
|
|
- You can share the context (
t.context) in each test. (Only works for beforeEach/afterEach hooks)
|
|
Other APIs
test.serial(): Force test serialization.test.only(): Running specific tests (effect all test files).test.skip(): Just skip the test.test.todo(): Add a todo test, AVA will ignore it.test.failing(): Mark known failure.


Mocking
- AVA doesn’t support mocking, just use any other mocking library like Sinon.js or testdouble.js.
Debug in Chrome DevTools
- Launch test file by inspect-process.
- Just use
debuggerkeyword in the test file.
|
|
Debug in VS Code
- Add following configuration in the
launch.json - Set the breakpoints and run.
|
|
Trolls & Bad things
- Must run with ava-cli
- Only can limit the execution time by CLI (global setting)
- ava –timeout=30s
- Cannot set for each test case.
- Must write tests carefully because of the concurrency.
- e.g. Test transactions in a same table.
- Force test serialization
test.serial()$ ava --serial
Other reference
- TypeScript in ava: https://github.com/avajs/ava/blob/master/docs/recipes/typescript.md
- Flow in AVA: https://github.com/avajs/ava/issues/986
- React AVA workshop: https://github.com/kentcdodds/react-ava-workshop
- Testing React components: https://github.com/avajs/ava/blob/master/docs/recipes/react.md
- Configuring Babel: https://github.com/avajs/ava/blob/master/docs/recipes/babelrc.md
- Setting up AVA for browser testing: https://github.com/avajs/ava/blob/master/docs/recipes/browser-testing.md