I am trying to seed the database for unit test.
Below is the seed.js
file:
.......
const app = require('./app')
const db = app.get('db')
const saveUsersToDB = (done) => {
db.User.bulkCreate(users)
.then(() => (done))
}
module.exports = {saveUsersToDB};
My app.test.js
file:
.......
const expect = require('expect')
const request = require('supertest')
const {saveUsersToDB} = require('./seed/seed');
before(saveUsersToDB)
When I run the test below is the error I get:
Express listening on port 3000!
1) "before all" hook: saveUsersToDB
0 passing (2s)
1 failing
1) "before all" hook: saveUsersToDB:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
npm ERR! Test failed. See above for more details.
I thought returning .then(() => (done))
was enough? What am I doing wrong?
Because (done)
will actually return the function instead of invoking it. In order to call done
, you need to write it this way.
.then(() => done())
However, I don't recommend using done
along with promises. You just simply need to return the promise then mocha
will handle it automatically.
const saveUsersToDB = () => db.User.bulkCreate(users)