Issue
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.
Solution
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.
Answered By – Noah
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0