Giter Site home page Giter Site logo

mongoose-memory's Introduction

mongoose-memory

To make sure that your API (that uses mongoose) works correctly, you definitely should create tests with a real database. But writing everything to disk takes a lot of time. If you keep everything in memory and don't write anything to disk, then your tests can speed up by 10x.

You probably want to do the following things while testing your API that connects to MongoDB via mongoose:

  • start a MongoDB instance
  • connect to that MongoDB instance with mongoosse (to a specific db)
  • create test data before each and every test case
  • purge the test data after the test cases
  • drop the test db and disconnect
  • stop the MongoDB instance

This lib is a wrapper for mongodb-memory-server and implements exactly the mentioned functionalities above, except for the test data creation. (You will have to do it yourself.)

Everything is stored in memory only, so your tests will be super fast!

On top of the fact, that everything is stored in memory, you can run your tests in parallel, by starting separate mongodb-memory-server instances for all of your test suites. (For example, you don't need the --runInBand flag when testing with Jest.) This truly makes your tests super fast!

Installation

npm i --save-dev

Initialization

Have to pass a mongoose instance to the creator function.

import mongoose from 'mongoose'
import createMongooseMemoryServer from 'mongoose-memory'

const mongooseMemoryServer = createMongooseMemoryServer(mongoose)

Instance Functions

After initializing with a mongoose instance, the creator function returns an object with the following functions:

async start()

Starts a new mongodb-memory-server instance.

await mongooseMemoryServer.start()

You can start multiple instances at the same time. (They will start separate mongodb-memory-server instances bound to different ports.)

The default storage engine is 'ephemeralForTest' which does not provide indices. If your tests rely on indices (for example, your API should return an error in a duplicate key) you will need to use the 'wiredTiger' storage engine.

await mongooseMemoryServer.start({ storageEngine: 'wiredTiger' })

async connect(dbName)

Connects to your test database (dbName - string).

await mongooseMemoryServer.connect(dbName)

async purge()

Deletes all of the collections in your database.

await mongooseMemoryServer.purge()

async disconnect()

Drops the database, mongoose disconnects.

await mongooseMemoryServer.disconnect()

async stop()

Stops the mongodb-memory-server instance.

await mongooseMemoryServer.stop()

You should invoke the stop function for every instance you started after your tests are finished running.

Full-fledged Example with Jest

This Jest example shows you how to create a mongodb-memory-server instance and connect to a test db, how to purge the test data after every test, and finally how to disconnect and stop the db.

import mongoose from 'mongoose'
import createMongooseMemoryServer from 'mongoose-memory'

// import other things that are needed for your tests

const mongooseMemoryServer = createMongooseMemoryServer(mongoose)

describe('Test suite', () => {
  beforeAll(async () => {
    await mongooseMemoryServer.start()
    await mongooseMemoryServer.connect('test-db')
  })
  beforeEach(async () => {
    // initialize your default db
  })
  afterEach(async () => {
    await mongooseMemoryServer.purge()
  })
  afterAll(async () => {
    await mongooseMemoryServer.disconnect()
    await mongooseMemoryServer.stop()
  })

  test('Example test', async () => {
    // Write your tests here
  })
})

As you can see above, you can do it for all of your test suites. It means, that each test suite will start it's own mongodb-memory-server instance. All the test suites will connect to separate instances, so you will be able to run your tests in parallel. (You don't need to use the --runInBand flag with Jest.)

mongoose-memory's People

Contributors

gyulanemeth avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.