npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ava-rethinkdb

v0.1.4

Published

RethinkDB helpers for AVA

Downloads

11

Readme

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