Mocking database in node.js?

I don’t think database related code can be properly tested without testing it with the database software. That’s because the code you’re testing is not just javascript but also the database query string. Even though in your case the queries look simple you can’t rely on it being that way forever.

So any database emulation layer will necessarily implement the entire database (minus disk storage perhaps). By then you end up doing integration testing with the database emulator even though you call it unit testing. Another downside is that the database emulator may end up having a different set of bugs compared to the database and you may end up having to code for both the database emulator and the database (kind of like the situation with IE vs Firefox vs Chrome etc.).

Therefore, in my opinion, the only way to correctly test your code is to interface it with the real database.

Leave a Comment