I like RethinkDB for its simplicity, API and local admin tool. Recently I wanted to unit test NodeJS code that interfaces with a RethinkDB on a continuous build server. I love using TravisCI, Codeship and CircleCI and consider well tested code a prerequisite to a solid application. Here is how to install and run the database while executing BDD tests (in my case using Mocha).
Install RethinkDB
To install RethinkDB on CircleCI instance I added the following config file
1 | dependencies: |
This will install the latest available 1.16 version of the database. If you want to install the 2.x version
instead use sudo apt-get install rethinkdb=2.\*
command.
Unit tests
To start RethinkDB process I wrote the following helper js code
1 | var spawn = require('child_process').spawn; |
The above function returns another function that can kill the started database process.
To start the DB before running the unit tests I call startRethinkDB
inside the Gulpfile.js
.
1 | var startRethinkDb = require('./start-rethinkdb'); |
There is a short timeout to let db to come up, then another short delay after the tests are finished to close all open DB connections.
The CI is now happy and runs the tests against the database. Just remember that any local DB data persists unless you delete the saved database folder.