While testing with Mocha I am getting the following error on running server.test.js
1) "before each" hook for "should get all todos":
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
server.test.js
const expect = require('expect');
const request = require('supertest');
const {app} = require('./../server');
const {Todo} = require('./../todos');
const todos = [
{
text: 'This is text 1'
},
{
text: 'This is text 2'
}
];
beforeEach((done) => {
Todo.remove({}).then(() => {
return Todo.insertMany(todos);
}).then(() => done());
});
describe('GET /todos', () => {
it('should get all todos', (done) => {
request(app)
.get('/todos')
.expect(200)
.expect(res => {
expect(res.body.length).toBe(2);
})
.end(done);
});
});
But if I do some changes in beforeEach() method like:
updated server.test.js
const expect = require('expect');
const request = require('supertest');
const {app} = require('./../server');
const {Todo} = require('./../todos');
const todos = [
{
text: 'This is text 1'
},
{
text: 'This is text 2'
}
];
beforeEach((done) => {
Todo.remove({}).then(() => {
Todo.insertMany(todos);
done();
})
});
describe('GET /todos', () => {
it('should get all todos', (done) => {
request(app)
.get('/todos')
.expect(200)
.expect(
expect(res.body.length).toBe(2);
})
.end(done);
});
});
Then I am getting no errors. Basically, by chaining promises in beforeEach() method I am running into an error but without that everything is fine. Could anyone explain why is it happening?
server.js
var express = require('express');
var body_parser = require('body-parser');
const {mongoose} = require('./mongoose.js');
const {Todo} = require('./todos');
const {Todo_1} = require('./todos');
var app = express();
app.use(body_parser.json());
// using GET method
app.get('/todos', (req, res) => {
Todo.find().then((todos) => {
res.send(todos);
}, (err) => {
res.status(400).send(err);
});
});
module.exports = {app}
app.listen(3000, () => {
console.log('Server is up on the port 3000');
})