tombraider
tombraider

Reputation: 1167

Mocha test passes with incorrect data

I have the following test using Mocha, Supertest and Chai:

const app = require('../server')
const expect = require('chai').should()
const request = require('supertest')

describe('GET /webhook', function () {
  context('a verify request from Facebook with a correct token', function() {
    it('responds with 200 and displays the challenge query in the response body', function() {
      request(app)
        .get('/webhook?hub.mode=subscribe&hub.challenge=123456789&hub.verify_token=' + process.env.FB_VERIFY_TOKEN)
        .expect(200)
        .end(function (err, res) {
          console.log(res.text)
          res.should.have.property("text", "abc")
          done()
        })
    })
  })
})

As you can see, we're expecting the response to have a property 'text' with the content of 'abc'. However, when I run the tests it passes without any problems.

Here is a shortened version of the response:

ClientRequest{  
   res:IncomingMessage   {  
      headers:{  
         'x-powered-by':'Express',
         'content-type':'text/html; charset=utf-8',
         'content-length':'9',
         etag:'W/"9-JfnnlDI7RTiF9RgfG2JNCw"',
         date:'Mon, 27 Feb 2017 19:45:30 GMT',
         connection:'close'
      },
      rawHeaders:[  
         'X-Powered-By',
         'Express',
         'Content-Type',
         'text/html; charset=utf-8',
         'Content-Length',
         '9',
         'ETag',
         'W/"9-JfnnlDI7RTiF9RgfG2JNCw"',
         'Date',
         'Mon, 27 Feb 2017 19:45:30 GMT',
         'Connection',
         'close'
      ],
      upgrade:false,
      url:'',
      method:null,
      statusCode:200,
      statusMessage:'OK',
      req:[  
         Circular
      ],
      text:'123456789',
   }
}

I'm expecting the test to fail, as abc != 123456789, but unfortunately this isn't the case.

Does anyone have any ideas?

Upvotes: 0

Views: 722

Answers (1)

Srle
Srle

Reputation: 10496

As soon as you have asynchronous operation, you need to tell to test runner to wait for async operation to be done. Using mocha you can either return a promise from test or you can use done callback or call it whatever you want.

In your case while you are calling done callback, you are not passing it to test callback, it should be:

it('should bla bla', function (done) {
  ...
  // call done()
})

Upvotes: 3

Related Questions