Mongo Scenario
Quickly and accurately create test data in a MongoDB for repeatable and reliable integration tests.
Quick Start
yarn add -D mongo-scenario
./scenarios/one.yml
description: A basic test scenario
data:
- users:
- one:
_id: 1
name: John
email: john@foo.bar
- two:
_id: 2
name: Jane
email: jane@foo.bar
- tasks:
- three:
name: Take out trash
user: 2
- four:
name: Grocery shopping
user: 1
foo.spec.js
import Scenario from 'mongo-scenario'
const db = connectToMongo()
const scenario = new Scenario(db)
describe('my test', () => {
before(() => {
scenario.load('one')
})
after(() => {
scenario.unloadAll()
})
it('has the data', () => {
assert(users.list.length, 2)
})
})
In the trivial example above, we describe our scenario in terms of the data that we want loaded into our MongoDB. In the setup code of our test, we then load up a new database connection and pass it to the mongo-scenario
constructor. In the before block we load the specified scenario that we want to run tests against. The final step in the after block is to unload the scenario, in this case we unload all scenarios that may be loaded.
Overview
At it's heart, mongo-scenario
is nothing more than a YAML to JavaScript parser that inserts and deletes specific records into a running MongoDB instance. Which MongoDB instance does it use? Any one that give it. It could be a seven shard atlas cluster, a local test database or even in memory.