简体   繁体   中英

supertest test express middleware

found the following hint on how to test middleware in express:
https://github.com/visionmedia/express/blob/master/test/req.xhr.js
I was wondering why my tests were always passing. Until I noticed that when i copied the test from express they behaved the same. I tried screwing them up but they keep passing: https://github.com/visionmedia/express/blob/master/test/req.xhr.js

What am I missing here?

it('should return true when X-Requested-With is xmlhttprequest', function(done){
  var app = express();

  app.use(function(req, res){
    req.xhr.should.be.false; //set to false, to fail the test but it still passes
    res.end();
  });

  request(app)
  .get('/')
  .set('X-Requested-With', 'xmlhttprequest')
  .end(function(res){
    done();
  })
})

You didn't miss anything, it is the express test req.xhr that will never fail.

If running your example, you will see the error stacktrace but the test passes because:

  1. it didn't catch the error during test.
  2. no error information passed to done() function call.

My fixes are in PR #2053 :

  1. use expect() to return the assertion error to .end() .
  2. pass any error information to done() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM