Create a connection to a brand new MongoDB Database before each test.
This pacakge exports two functions:
Creates a new mongoose connection to mongodb://localhost/test_database_${randomnumber}
.
This function is supposed to be run before every test which is using the databse.
This ensure a few things:
- Every test is run in complete isolation
- Test can be run in parralel
- No need to reset the db after each test as every test starts with a clean db.
Drops the database from the default mongoose connection. Supposed to be run after each test.
npm install --save-dev mongoose-fresh-db
Currently only tested with jest.
This package assumes that the tested code itself doesn't create a mongoose connection!
import { createNewDatabase, dropDatabase } from 'mongoose-fresh-db';
beforeEach(createNewDatabase);
afterEach(dropDatabase);
test('this test will have a shiny new db instance', () => {});
For a more detailed example you can check out the tests of this package.