I'm using 'sails-memory' as the database for my Sails unit tests and ideally would like to clear the entire database after individual tests. Is there a way I can drop the entire database?
I found another method which seems to work. This emits an event which tells the orm hook to reload before each test. If using the memory db, or the disk db with the 'drop' migrate option, it accomplishes the desired.
beforeEach((done) => {
"use strict";
// Drops database between each test. This works because we use
// the memory database
sails.once('hook:orm:reloaded', done);
sails.emit('hook:orm:reload');
});
You could lift your sails app before each test, rebuilding your database (migrate: 'drop'
). Here is an example:
Sails = require('sails/lib/app');
app = Sails();
var testConfig = {
environment: 'test',
port: 1337,
log: {
level: 'error'
},
connections: {
testDB: {
adapter: 'sails-memory'
}
},
connection: 'testDB',
//wipe/drop ALL my data and rebuild models every time
migrate: 'drop'
};
beforeEach(function (done) {
// start sails app for tests
app.lift(testConfig, function (err, sails) {
done(err);
});
});
//tests...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With