0

I am not sure how, but I can not for the life of me figure out how to make this test fail. I honestly don't really know what I'm doing, but seeing as /fail doesn't exist AND all my routes are protected with authentication, which results in a GET /fail 401 the test should fail seeing as it expect(200). So what is going on here?

var app = require('../../app');
var request = require('supertest');
var agent = request.agent(app);

describe('Index Routes', function(done){
  it('should return 200 status code', function(done){
      agent.get('/').expect(500, done);
  });

  it('should fail horribly', function(done){
    agent.get('/fail').expect(200, done);
  })
});

Output:

> istanbul cover node_modules/mocha/bin/_mocha -x database.js

  Index Routes
[0mGET / [32m200 [0m374.389 ms - 170[0m

    1) should return 200 status code
[0mGET /fail [33m401 [0m1.723 ms - 21[0m

    √ should fail horribly


  1 passing (415ms)
  1 failing

  1) Index Routes should return 200 status code:
     Error: expected 500 "Internal Server Error", got 200 "OK"
      at Test._assertStatus (C:\Users\janedoe\WalkingApp-WebService\node_modules\supertest\lib\test.js:232:12)
      at Test._assertFunction (C:\Users\janedoe\WalkingApp-WebService\node_modules\supertest\lib\test.js:247:11)
      at Test.assert (C:\Users\janedoe\WalkingApp-WebService\node_modules\supertest\lib\test.js:148:18)
      at Server.assert (C:\Users\janedoe\WalkingApp-WebService\node_modules\supertest\lib\test.js:127:12)
      at emitCloseNT (net.js:1521:8)


=============================== Coverage summary ===============================
Statements   : 58.05% ( 101/174 )
Branches     : 6.25% ( 2/32 )
Functions    : 12.9% ( 4/31 )
Lines        : 61.59% ( 101/164 )
================================================================================
Dustin
  • 567
  • 2
  • 5
  • 18

2 Answers2

0

Turns out it was an issue with my authentication. Simply as switching the order of res.send().status(); to res.status().send() in my authentication method for Passport.js.

Dustin
  • 567
  • 2
  • 5
  • 18
0

Here is the express v4 documentation for the res.status method. res.status is a chainable alias of Node’s response.statusCode. You may then chain the send function thereafter.

res.status(400).send('Bad Request');
Mikelax
  • 572
  • 3
  • 10