Coder Social home page Coder Social logo

ava-rethinkdb's Introduction

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

import test from 'ava'
import { init, cleanup } from 'ava-rethinkdb'

test.before(init())
test.after.always(cleanup)

test('I should have a RethinkDB instance', async t => {
  let connection = await r.connect({})

  await r.dbCreate('MyDatabase')
})

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

import test from 'ava'
import { init, cleanup } from 'ava-rethinkdb'

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: '[email protected]' },
      { username: 'heya', email: '[email protected]' }
    ]
  }
}

test.before(init(TEST_DATA))
test.after.always(cleanup)

test('These documents should exist', async t => {
  let conn = await r.connect({ db: 'my_database' })
  let results = await r.table('my_table').run(conn)
  let data = await results.toArray()

  console.log(data)
  t.truthy(data)
})

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 = require('express')
const r = require('rethinkdb')

let app = express()

app.get('/users', (req, res) => {
  r.connect({ db: 'app' })
    .then(conn => r.table('users').run(conn))
    .then(results => results.toArray())
    .then(users => res.status(200).send({ users }))
    .catch(e => res.status(500).send(e))
})

module.exports = { app }
// test/integration/users-test-1.js
import test from 'ava'
import request from 'supertest-as-promised'
import { init, cleanup } from 'ava-rethinkdb'

import { app } from '../../app.js'

const TEST_DATA = {
  app: {
    users: [
      { name: 'UserA' },
      { name: 'UserB' }
    ]
  }
}

test('Users should be returned from /users', t => {
  return request(app)
    .get('/users')
    .expect(200)
})
// test/integration/users-test-2.js
import test from 'ava'
import request from 'supertest-as-promised'
import { init, cleanup } from 'ava-rethinkdb'

import { app } from '../../app.js'

const TEST_DATA = {
  app: {
    users: [
      { name: 'UserC' },
      { name: 'UserD' }
    ]
  }
}

test('Different users should be returned from /users', t => {
  return request(app)
    .get('/users')
    .expect(200)
})

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

ava-rethinkdb's People

Contributors

codelenny avatar dylnslck avatar rrdelaney avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.