gutasaputra
gutasaputra

Reputation: 179

Cannot read property 'status' of undefined - Mocha Chai

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();
        });
      });

Upvotes: 1

Views: 19329

Answers (2)

Ashwani Panwar
Ashwani Panwar

Reputation: 4568

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

Upvotes: 1

LostJon
LostJon

Reputation: 2387

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')

Upvotes: 2

Related Questions