1

so im newbie with mocha-chai things in nodejs env. i dont understand why i cant get the response status while running mochajs.

here is my code :

let chai = require('chai');
let chaiHttp = require('chai-http');
let server = require('server');
let expect = require("chai").expect;
let should = require("should");

let request = require("superagent");
let util = require("util");

chai.use(chaiHttp);

describe('API Clinic Test', function() {

  it('should list ALL clinic on /api/v1/clinic GET', function(done) {
  chai.request(server)
    .get('http://localhost:5000/api/v1/clinic')
    .end(function(err, res){
        // res.should.have.status(200);
        expect(res.status).to.equal(200);
      done();
    });
  });

  it('should list a SINGLE clinic on /api/v1/clinic/<id> GET');
  it('should add a SINGLE clinic on /api/v1/clinic POST');
  it('should update a SINGLE clinic on /api/v1/clinic/<id> PUT');
  it('should delete a SINGLE clinic on /api/v1/clinic/<id> DELETE');
});

everytime i run mocha test.js, i always get this error msg :

Uncaught TypeError: Cannot read property 'status' of undefined

ohya, i use should method too. i got another error msg like : cannot-read-property-should-of-null

i read on this thread.

Should js Cannot read property 'should' of null

thats why i want to change and use expect method.

can you guys please help me.

thank you.

::: update ::: how to fix the issue ? instead of using this line of codes :

it('should list ALL clinic on /api/v1/clinic GET', function(done) {
  chai.request(server)
    .get('http://localhost:5000/api/v1/clinic')
    .end(function(err, res){
        // res.should.have.status(200);
        expect(res.status).to.equal(200);
      done();
    });
  });

i use this :

it('should list ALL clinic on /api/v1/clinic GET', function(done) {
chai.request('localhost:5000') .get('/api/v1/clinic') 
.end(function(err, res){
            // res.should.have.status(200);
            expect(res.status).to.equal(200);
          done();
        });
      });
Community
  • 1
  • 1
gutasaputra
  • 179
  • 1
  • 6
  • 20
  • This just means that your `res` object is undefined, so it doesn't have any properties, including `status` -- i.e., your result isn't coming back. can you issue a simple GET to your target from the command line and confirm that it responds with a 200? – meatspace Jan 05 '17 at 18:53

2 Answers2

1

you are hitting an error most likely...you should have a line similar to the below

if(err) done(err);

Per comments...this led you in the right direction. Moreso you needed to do the below:

chai.request('http://localhost:5000').get('/api/v1/clinic')
LostJon
  • 2,287
  • 11
  • 20
  • within your .end() callback...pretty much where your commented out line is. not a bad idea to use if/else in that block either. To further debug, you can make sure its not some other error by wrapping with try/catch – LostJon Jan 05 '17 at 19:38
  • i dont understand. i follow your instruction, and get another error msg. Error: connect ECONNREFUSED 127.0.0.1:80 – gutasaputra Jan 05 '17 at 19:42
  • thought i would add a bit more. the way you are formatted, you dont need to specify a the localhost url, just the endpoints since you feed chai.request with the express app. so, i should look like **chai.request(server).get('/api/v1/clinic')** – LostJon Jan 05 '17 at 19:48
  • 1
    or...you may want to do something like `chai.request('http://localhost:5000').get('/api/v1/clinic')` – LostJon Jan 05 '17 at 19:50
  • any luck? if so, I will edit my response so you can mark answer? – LostJon Jan 05 '17 at 19:54
  • can you mark as answer..may be able to help other ppl with this similar issue in the future. – LostJon Jan 05 '17 at 20:02
1

In my case I forgot to export module like export defaul mymodule. So check for this.

Ashwani Panwar
  • 3,819
  • 3
  • 46
  • 66