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?
mocha my-spec.js. The default timeout is 2000 milliseconds. A command-line parameter can be used to overwrite it for partial testing: mocha my-spec.js --timeout 5000.
Whenever you run Mocha at the command line, it will read this file and set a timeout of 5 seconds by default. Another way which may be better depending on your situation is to set it like this in a top level describe call in your test file: describe("something", function () { this. timeout(5000); // tests... });
Asynchronous Mocha Tests To correct this, Mocha can provide a “done” callback method that you can call from the beforeEach call, afterEach call, it calls, and more. This “done” parameter, when present in your callback function, tells Mocha that you are writing an asynchronous test.
By default, Mocha tests have a 2 second timeout (which means that the test needs to be completed in 2 seconds).
You can increase it (in milliseconds) as follows:
this.timeout(5000); // this test can take up to 5 seconds
https://mochajs.org/#timeouts
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