Reputation: 451
i want to write a test for test createUser
.
i write this code :
const User = require("../src/Entite/User");
describe("Create User", () => {
it(" Save and Create User ", (done) => {
const addUser = new User({
name: "Kianoush",
family: "Dortaj",
});
addUser
.save()
.then(() => {
assert(!addUser.isNew);
done();
});
});
});
when i run the test use was created in database but it show me this error and test fail :
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (F:\Projects\Nodejs\MongooDB\test\create-user_test.js) at listOnTimeout (internal/timers.js:549:17) at processTimers (internal/timers.js:492:7)
whats the problem ? how can i solve that ??
Upvotes: 7
Views: 7328
Reputation: 1995
Here a few solutions can be checked.
"scripts": {
"test": "mocha --timeout 10000" <= increase this from 1000 to 10000
},
#### OR ###
it("Test Post Request", function(done) {
this.timeout(10000); //add timeout.
});
Test-specific timeouts may also be applied, or the use of this.timeout(0)
to disable timeouts all together:
it('should take less than 500ms', function(done){
this.timeout(500);
setTimeout(done, 300);
});
If both do not work. Try this
const delay = require('delay')
describe('Test', function() {
it('should resolve', async function() {
await delay(1000)
})
})
Somehow the presence of the done argument in the async function breaks the test, even if it's not used, and even if done() is called at the end of the test.
Upvotes: 3