mongodb-memory-serverto create a testing database
mongooseor some other db module, you might not really need to test that code. Mongoose (and presumably all other popular db modules) already has plenty of tests for all of its actions, so if you are just serving up a JSON API and all you're doing is leveraging functions from another module then those operations are already covered.
mongodb-memory-server. You can see the specifics on their github repo, but basically this package will spin up a fresh in-memory mongoDB server that you can connect to with mongoose, and then use for your testing environment. Since it's creating a fresh DB every time you don't have to worry about trying to keep your data in sync, or polluting your production database.
mongodb-memory-serverrepo README should work just fine. Below is a slightly edited version of it. Copy this to a new file called
beforeAllfunction in your testing suite to add a couple of items to the database before running tests.
testingenvironment that uses this
mongodb-memory-server. The Jest Docs demonstrate an alternative (but similar) setup to the simple one we have here. The common element here is that no matter how you accomplish it, our goal is to use this alternative DB when running our tests.