Inspiration
Writing test suites for your NodeJS code with MongoDB is a quite untrivial task:
- you need to install mongod on your machine (sometimes it hard, cause team may use different OS and in most cases, the mongod version may differ).
- you need to run your test in serial order (but modern test frameworks like Jest or AVA provides parallel execution for speed up. So you need somehow launch different mongod on different ports for that).
What if you don't need to do anything for such configuration?! You even may have no mongod binaries on your machine. mongodb-memory-server package solves all these problems for you.
What it does
mongodb-memory-server package spins up a actual/real MongoDB Server programmatically from NodeJS for testing or mocking during development. By default, it holds the data in memory. Fresh spun up mongod process takes about 7Mb of memory. The server will allow you to connect your favorite ODM or client library to the MongoDB Server and run integration tests isolated from each other.
This package on install downloads the latest MongoDB binaries and save it to the cache folder. So the first run may take time. All further runs will fast, because will be used already downloaded binaries.
This package automatically downloads binaries from https://fastdl.mongodb.org/ according to your operating system. You can see all available versions by the following links Linux (Ubuntu, RHEL, Debian, SUSE, Amazon), OSX, Win.
Every MongoMemoryServer instance creates and starts fresh MongoDB server on some free port. You may start up several mongod simultaneously. When you terminate your script or call stop() MongoDB server(s) will be automatically shut down.
Perfectly works with Travis CI without additional services and addons options in .travis.yml.
How I built it
I need tests for graphql-compose-mongoose and step by step I started to develop this tool.
Challenges I ran into
fastdl.mongodb.orgdoes not provide builds for Linux Alpinelatestpackage onfastdl.mongodb.orgmeans a nightly build from master.
Accomplishments that I'm proud of
This package is downloaded 35,000 times a week. And the total number of downloads is approaching 1,000,000.
But the best accomplishment that I'm proud of is thousands of hours of saved time:
- Parallel testing a GraphQL server with Jest and MongoDB – speed up tests in 3.5 times.
- Blazing Fast Testing In Node With MongoDB
What I learned
There are no perfect things in our life. But we can make it better!
What's next for mongodb-memory-server
More tools built on top of this package. For example https://github.com/shelfio/jest-mongodb
And of course, I'm proud that MongoDB for NodeJS testers and developers became much more comfortable.
Log in or sign up for Devpost to join the conversation.