Mock/Test Mongodb Database Node.js

In order to spin up a real in-memory mongodb for testing, https://github.com/nodkz/mongodb-memory-server helps as well.


If your database access is a separate module, you can mock out the module or parts of the module (e.g., the configuration part) using one of the following:

  • sinon
  • rewire
  • horaa
  • sandboxed-module

The answers to the following related question lists a number of possible solutions/approaches: How do you mock MySQL (without an ORM) in Node.js?