Wipe all tables in a schema – sequelize nodejs

To perform unit testing on a mysql database using sequelize as ORM, I need to refresh my database when each test start to run. Actually I wrote a solution like this:

beforeEach(() => {
   table1.destroy({ where: {} })
   table2.destroy({ where: {} })
   table3.destroy({ where: {} })

but every time I create tables I have to add another instruction. I would implement a single instruction to perform a complete wipe of the entire schema

Something like:

beforeEach(() => db.clean())


You should not touch the database when doing unit testing.

If you’re testing business logic that talks to sequelize, create a mock of the interface to sequelize and inject that into the unit being tested. You can then make assertions or expectations with regard to calls to the mock interface’s methods. Without more details of the testing environment, it’s impossible to provide more concrete direction than that, but you might investigate the sequelize-mocking package to facilitate that.

If you’re testing that sequelize is actually talking to your database, then you are doing a whole lot more than unit testing, and I would argue that you would want an out-of-band way of initializing and managing the environment, but I’d also point out that sequelize has a whole suite of tests already.