Unhandled Promise Rejection Warning: TypeError: first argument must be a string or buffer

The question seems to be duplicate, but I've been working on this solution in the last 3 hours. Basically I use supertest

and mocha

to test my API. I cannot figure out which promise is not being resolved.

app.post('/todos', (req, res) => {
 var todo = new Todo({
 text : req.body.text
});
 todo.save().then( (doc) => {
  res.status(200).send(doc)
  }, (e) => {
  res.status(400).end(e);
 });
});

      

Below is the test I wrote:

const expect = require('expect');
const request = require('supertest');

var {app} = require('./../server');
var {Todo} = require('./../models/todo');

// Setup database 
beforeEach((done) => {
  Todo.remove({})
 .then(() => done())
 .catch((e)=> done(e));
}); 

describe('Post /todos', () => {
it('should create a new todo', (done) => {

var text = 'Testing text';

// supertest to get, post
request(app)
  .post('/todos')
  .send({text}) // Automatic conversion to json - ES6
  .expect(200) // assertions
  .expect((res) => { // custom parsing
    expect(res.body.text).toBe(text);
  })
  .end((err, res) => {  // done directly accepted, but rather let be more precise now - check mongodb
    if (err) {
      return done(err);
    }
    Todo.find()
      .then((todos) => {
        expect(todos.length).toBe(1);
        expect(todos[0].text).toBe(text);
        done();
      })
      .catch((e) => done(e));
  });
 });
});

      

Please help solve this problem. Here's the whole msg error:

mocha server / ** / *. test.js Listen on port: 3000 Message / todos (node: 1882) UnhandledPromiseRejectionWarning: Rejection of an unhandled promise (rejection id: 1): TypeError: first argument must be a string or buffer (node: 1882) [DEP0018] Deprecated. Warning. Deviations from unhandled promises are deprecated. In the future, a rejection promise that is not handled will terminate the Node.js process with a non-zero exit code. 1) should create a new todo 0 pass (2s) 1 unsuccessful 1) Post / todos should create a new todo: Error: timeout exceeded 2000ms. For asynchronous tests and interceptors, make sure to call "done ()"; if you are returning a promise, make sure it is resolved.

+3


source to share





All Articles