Testing utilities for RethinkDB and AVA
npm install --save-dev ava-rethinkdb
This is a hacky way of using the NodeJS RethinkDB and AVA together. It uses undocumented features of the RethinkDB driver, and should be considered experimental.
Basic Testing
By running init
and cleanup
you get a fully managed database instance for
your tests! Everything is cleaned up at the end, so there's no leftover fixtures
testtestafter
Seeding
The problem is that if you want to do multiple tests they all happen at the same time due to the magic of AVA. Luckily you can seed the database with a simple JSON structure
const TEST_DATA = my_database: // The top level is the database to create my_table: // Next is a table in the database. This holds an array of documents to insert name: 'A' value: 1 name: 'B' value: 2 users: username: 'daniel' email: 'wry@gmail.com' username: 'heya' email: 'ayeh@outlook.com' testtestafter
Different Database Instances
This is where the magic really is. Every single test file is given its own
RethinkDB instance. This makes it perfect for integration tests against
endpoints, because now they can all be used in parallel! The magic comes
from modifying the default port the driver looks at, making it different
in each process, then spinning up a RethinkDB instance at that port.
Check out the test
directory for a good example.
// app.js const express = const r = let app = app moduleexports = app
// test/integration/users-test-1.js const TEST_DATA = app: users: name: 'UserA' name: 'UserB'
// test/integration/users-test-2.js const TEST_DATA = app: users: name: 'UserC' name: 'UserD'
The TEST_DATA
contained in each file creates a new database to be used for
each file!
Debugging
To view the output from all the server logs, set the environment variable
AVA_RETHINKDB_DEBUG=on