Search code examples
node.jsmongoosesupertest-as-promised

expect is not defined in supertest-as-promised


When I used bellow code the error shown expect is not defined (inside of then )

Documentation link

it("should return error", function () {
  return request(app).get("/verify")
    .expect(200)
    .then(function (res) {
      return expect(res.body.error[0].message).to.equal("NEW_CODE_REQUIRED");
    });
});

how can I check this?


Solution

  • I solved it by following process. Added a function to check expected error that return error if got unexpected value and this function called from .expect()

    function checkErrorMessage(res) { // this function throw error if got unexpected result
       if(res.body.error[0].message === 'NEW_CODE_REQUIRED') {
         return false; // return false means no error (got expected result)
       } else {
         return true; // return true means return error (got unexpected result)
       }
    }
    
    it("should return error", function () {
      return request(app).get("/verify")
        .expect(200)
        .expect(checkErrorMessage);
    });