Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 855
JScripter Avatar asked Dec 13 '25 14:12

JScripter


1 Answers

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

like image 190
l2ysho Avatar answered Dec 15 '25 05:12

l2ysho