robzolkos
robzolkos

Reputation: 2286

Cleaning out test database before running tests

What is the best way to clean out a database before running a test suite (is there a npm library or recommended method of doing this).

I know about the before() function.

I'm using node/express, mocha and sequelize.

Upvotes: 28

Views: 23137

Answers (4)

saumyajain125
saumyajain125

Reputation: 168

Add this in your test file . This would undo all the migrations and create new tables while adding the seeded data to let you do testing on the seeded data.

const SCRIPT_TO_TRUNCATE_AND_SEED_DATABASE = 'cd apps/backend && npx sequelize-cli db:migrate:undo:all && npx sequelize-cli db:migrate && cd ../.. && npx sequelize-cli db:seed:all'
test(
    'TRUNCATE_AND_SEED_DATABASE',
    done => {
      exec(SCRIPT_TO_TRUNCATE_AND_SEED_DATABASE, (err, out) => {
        try {
          console.log(out);
          expect(err).toBe(null);
          done();
        } catch (e) {
          done(e);
        }
      });
    },
    TIME_CONSTANT.ONE_MINUTE,
  );

Upvotes: 1

Noah
Noah

Reputation: 34333

The before function is about as good as you will do for cleaning out your database. If you only need to clean out the database once i.e. before you run all your tests, you can have a global before function in a separate file

globalBefore.js

before(function(done) {
   // remove database data here
   done()
}) 

single-test-1.js

require('./globalBefore')
// actual test 1 here

single-test-2.js

require('./globalBefore')
// actual test 2 here

Note that the globalBefore will only run once even though it has been required twice

Testing Principles

Try to limit the use of external dependecies such as databases in your tests. The less external dependencies the easier the testing. You want to be able to run all your unit tests in parallel and a shared resource such as a database makes this difficult.

Take a look at this Google Tech talk about writing testable javascript http://www.youtube.com/watch?v=JjqKQ8ezwKQ

Also take look at the rewire module. It works quite well for stubbing out functions.

Upvotes: 29

Diogo Menezes
Diogo Menezes

Reputation: 57

I made this lib to clean and import fixtures for your test.

This way, you can import fixtures, test and then clean your database.

Take a look at the following:

before(function (done) {
   prepare.start(['people'], function () {
      done();
   });
});

after(function () {
   prepare.end();
});

https://github.com/diogolmenezes/test_prepare

Upvotes: -3

robertklep
robertklep

Reputation: 203419

I usually do it like this (say for a User model):

describe('User', function() {
  before(function(done) {
    User.sync({ force : true }) // drops table and re-creates it
      .success(function() {
        done(null);
      })
      .error(function(error) {
        done(error);
      });
  });

  describe('#create', function() {
    ...
  });
});

There's also sequelize.sync({force: true}) which will drop and re-create all tables (.sync() is described here).

Upvotes: 19

Related Questions