JScripter
JScripter

Reputation: 133

How to mock mysql database?

I want to do unit testing of a few APIs written by myself, which make some queries on a MySQL database. But I learned recently that establishing an actual connection during testing is not a good practice and the test cases for the APIs would modify, delete, or insert into the tables, which I don't want to happen.

I found that there is a node package called mockgoose which does the same thing for MongoDB. Is there something similar for MySQL?

Upvotes: 3

Views: 6676

Answers (1)

l2ysho
l2ysho

Reputation: 3053

In my point of view, good approach is to create a separate DB for testing (you can do this on localhost or in a CI as well). You can create seed files to populate DB with initial data, also you can drop database after each test suite to start with clean setup (you can use a before/after hooks for this, check some testing frameworks like mocha or jest).

If you looking for some best practice with node.js testing check Node.js & JavaScript Testing Best Practices (2019) or nodebestpractices

Upvotes: 4

Related Questions