J
J
jspie2017-08-06 10:32:56
JavaScript
jspie, 2017-08-06 10:32:56

How to fix error when checking POST through chai?

models/note.js

import mongoose from 'mongoose'

const Schema = mongoose.Schema;

const NoteSchema = new Schema({
  title: {type: String, required: true},
  description: {type: String, required: true},
  color:{type: String},
  createAt:{type: Date, required: true},
  author: {type: String, required:true}

}, {collection:'notes'});

mongoose.model('note', NoteSchema);

../Notedb.js
export const createNote = (data) =>{
  const note = new Note({
    title: data.title,
    description: data.description,
    color: data.color,
    createAt: new Date(),
    author: data.author
  });
  return note.save();
}

../noteApi
const router = express.Router();

router.post('/add', (req, res, next)=>{
  return noteDb.createNote(req.body)
    .then((done)=>{
      res.json(done);
    })
    .catch((err)=>{
      res.status(500).send();
    });
});

..test/note.js
describe('/POST note add' , ()=>{
    it('it should POST test', (done)=>{
      let _note = {
        title: "Test note POST!",
        description: "This is test for note api",
        color: "yellow",
        author: "admin"
      }
      chai.request(server)
      .post('/add')
      .send(_note)
      .end((err, res)=>{
        res.should.have.status(200);
        res.body.should.be.a('object');
        //res.body.book.should.have.property('title');
        done();
      });
    });
  })

When testing, the following error occurs: Timeout of 10000ms exceeded. For async tests and hook...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vahe, 2017-08-06
@vahe_2000

By default, mocha tests have a 2 second timeout which means the test must be completed within 2 seconds. You can increment (in miliiseconds) like this: this.timeout(5000); This test may take up to 5 seconds. Try it. mochajs.org/#asynchronous-code

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question