3

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?

JScripter
  • 123
  • 2
  • 9
  • I think what you need is: https://stackoverflow.com/questions/8389149/how-do-you-mock-mysql-without-an-orm-in-node-js – SkyAo Aug 23 '19 at 08:07
  • 1
    this is a good idea, but for me it would be better to have a mysql in memory database which I can connect to. – JScripter Aug 23 '19 at 08:17

1 Answers1

4

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

l2ysho
  • 2,542
  • 1
  • 20
  • 41