npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongoose-memory

v1.1.2

Published

A simple mongodb-memory-server wrapper for mongoose to make your tests 10x faster.

Downloads

225

Readme

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.)