Andres Rodriguez Andres Rodriguez - 3 months ago 10
Node.js Question

How to reset the loopback memory DB

I am running several loopback tests via

mocha
(let's call them
test1.js
,
test2.js
and
test3.js
).

When i run the independently everything works well. However, when I ask
mocha
to run them all, things that are created in the first test in the in-memory DB collide with tests being done later on (test 2 or 3).

Is there a way to ensure we start each test with an empty DB? Something like:

app.dataSources.db.reset()


Thanks a lot!

UPDATE: What I ended up doing: I looked at the DataSource code and found that you can do
automigrate
on the memory DB.

before("wipe DB (if used with other tests)", function(done) {
app.dataSources.db.automigrate(function(err) {
done(err);
});
});

Answer

Get a hold of the db via app.dataSources.db and execute automigrate as in:

before("wipe DB (if used with other tests)", function(done) {
    app.dataSources.db.automigrate(function(err) {
        done(err);
    });
});

Cheers.