Fixtures Manager MongoDB Driver
An abstraction layer for the mongodb package to facilitate handling database fixtures for testing purposes, in a MongoDB database. This package is ment to be used in conjunction with the dbfixtures package, but can also be used by itself.
Installation
npm install dbfixtures-mongodb-driver
Usage
This package exposes the create({ connectURI: string, connectOptions?: MongoClientOptions, dbName: string, dbOptions?: MongoClientCommonOption }): Promise<IDriver>
function that returns a Promise that resolves with an instance of the driver.
Note1: For detailed information about the connectURI
and connectOptions
arguments, please consult the MongoDB NodeJS driver's connect documentation.
Note2: For detailed information about the MongoClientCommonOption
argument, please consult the MongoDB NodeJS driver's DB documentation.
An instance of the driver exposes the following interface
// truncates the collections (i.e., "tables") with the supplied names Promise<void> // inserts the supplied documents into the specified collection (i.e., "table") Promise<void> // terminates the connection to the database Promise<void>
Example
This example uses Mocha as the potential test runner.
const dbfixtures = ;const fixturesMongoDriver = ; const mongodbDriverInfo = connectURI: 'mongodb://localhost:27017/' connectOptions: useNewUrlParser: true dbName: 'test';const fixtures = 'roles': id: 1 name: 'role 1' id: 2 name: 'role 2' 'users': id: 1 email: 'myemail@test.net' role_id: 2 id: 2 email: 'test@gmail.com' role_id: 1 id: 3 email: 'another@email.org' role_id: 1 ; ;
Testing This Package
-
cd
into the package's directory -
run
npm install
-
run
npm run build
-
for unit tests run
npm test -- test\unit\
-
for integration tests run
npm test -- test\integration\
NOTE: requires an active MongoDB server available atlocalhost:27017
-
for end-to-end tests run
npm test -- test\e2e\
NOTE: requires an active MongoDB server available atlocalhost:27017
Suggestion to setting up a MongoDB server on your local machine
If you are using Docker
, you can run the CLI command docker run --name testmongo -p 127.0.0.1:27017:27017/tcp mongo:4
to raise a container with the MongoDB v4.* image and make it available through localhost:27017
.