Skip to content

Instantly share code, notes, and snippets.

@stackdumper
Last active January 20, 2019 17:18
Show Gist options
  • Save stackdumper/b44f8214652e2e970c6311040e139b20 to your computer and use it in GitHub Desktop.
Save stackdumper/b44f8214652e2e970c6311040e139b20 to your computer and use it in GitHub Desktop.
import ava from 'ava'
import nanoid from 'nanoid'
import mongoose from 'mongoose'
/*
test wrapper for ava
creates new database and drops it afterwards
*/
export const test = (message, cb) =>
ava(message, async (t) => {
const db = await mongoose.createConnection(`mongodb://localhost/test-${nanoid(5)}`)
try {
await cb(t, { db })
} catch (error) {
throw error
} finally {
await db.connection.dropDatabase()
await db.connection.close()
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment