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 🙏

© 2026 – Pkg Stats / Ryan Hefner

mongodb-sandbox

v1.0.1

Published

Launch a stand-alone MongoDB Topology for use within a Test Suite.

Readme

mongodb-sandbox

Launch a stand-alone MongoDB Topology for use within a Test Suite.

Build status License

Purpose

mongodb-sandbox provides an easy way to run a self-contained MongoDB Topology within the runtime execution of a Test Suite, participating in its lifecycle hooks;

Installation

npm install --save-dev mongodb-sandbox

This module supports Node.js 6.x and above.

Features

All Collections in the Sandbox Database get purged at the end of every Test Case.

As a safety measure, immediately after launching its Topology's daemon, the Lifecycle connects to the Sandbox and detects the presence of Documents. After all, if it's truly a volatile Sandbox, then no Documents should be found, right? If any are found, then it's possible that the Client has connected to a "real" Database by mistake;

  • the Test Suite gets terminated
  • the "real" Database is left untouched

MongoDB binaries are automatically downloaded, if not already present. By default, each version of the binaries is cached locally to ensure that your Test Suite runs quickly on subsequent launches.

The binaries are downloaded on-demand, vs. during 'postinstall'. Your first Test Suite execution will take some extra time on launch. The Test Case timeout is programatically extended to 90s to accomodate the time spent on

  • downloading the MongoDB binaries
  • launching the Topology

Examples

Please consult the JSDocs for additional Examples.

Here are ways to embed a Lifecycle into your Test Framework, and within a Test Case, connect your own MongoClient instance to the Sandbox.

The trick here is to capture the Test Framework context and provide it to the Lifecycle so that it can extend the Test Case timeout, as mentioned in Features.

Example for the Mocha Framework

In mocha, use a before block to capture the Test Framework context, construct your Lifecycle around it, and then register the rest of the hooks into the Framework.

const { expect } = require('chai');
const { MongoClient } = require('mongodb');
const { createSandbox } = require('mongodb-sandbox');


const sandbox = createSandbox();

before(function() {
  const lifecycle = sandbox.lifecycle(this);

  before(lifecycle.beforeAll);
  beforeEach(lifecycle.beforeEach);
  afterEach(lifecycle.afterEach);
  after(lifecycle.afterAll);
});


describe('the Sandbox', () => {
  it('is running', () => {
    expect(sandbox.isRunning).to.equal(true);
  });

  it('can be pinged', () => {
    const { url } = sandbox.config;

    return MongoClient.connect(url, { useNewUrlParser: true })
    .then((client) => {
      return client.db().admin().ping()
      .then((response) => {
        expect(response).to.deep.equal({ ok: 1 });

        return client.close();
      });
    });
  });
});

Examples for other Frameworks

Please submit a Pull Request if you come up with a good one :+1: .

That'll likely involve

  • enhancing the Lifecycle to detect & adjust the Test Framework context
  • adding a good-lookin' Example
  • checking it off the TODO list :ballot_box_with_check:

Configuration Options

Please consult the JSDocs.

Documentation

Please consult the JSDocs.

TODO

  • [ ] Test Framework Examples for
  • [ ] Auto-timeout extension for Test Frameworks
    • [x] mocha
    • [ ] tape
    • [ ] ava
    • [ ] jest
    • [ ] Your Favorite
  • [ ] Multi-process concurrency support, because this
  • [ ] Support for ReplicaSet Topology (Server-only for now)

On the Shoulders of Giants

mongodb-sandbox is an assemblage of

The module produces additional logging via debug.

// when running the Test Suite for your project ...
DEBUG=mongodb-sandbox npm test

Documentation is generated by jsdoc-to-markdown.

Why Mocha?

C'mon buddy, that's the "nobody ever got fired for buying IBM equipment" of JavaScript testing frameworks. Couldn't you use something a bit cooler?

Honestly, I tried, but

  • tape doesn't seem to have the necessary Lifecycle hook support.
  • ava's multi-process concurrency makes it incredibly difficult to start up a Sandbox, particularly at the juncture of allocating & reserving a unique port. So for the moment, ava and other concurrent-running Test Frameworks are not supported pending a TODO.
  • I find jest to be too opinionated and React-focused.

Whereas mocha is perfectly suited for the job at hand.

So that's why.

License

WTFPL. Copyright (C) 2004 Sam Hocevar [email protected]