Home > Software engineering >  Getting Invalid Chai property: status error which was working fine
Getting Invalid Chai property: status error which was working fine

Time:07-25

would need bit of help. I have written a code for API Test which is below, but I am getting an error Invalid Chai property: status. Can anyone of you please guide what am I doing wrong. This was working fine till few hours before.

Api is working fine too I have tallied by doing it manually.

import supertest from "supertest";
var chai = require('chai');
import { expect } from 'chai';
chai.use(require('chai-json-schema'));

const request = 
    supertest("https://graana.rocks/api/");

describe('Area_Details', () => {
    it('GET /areadetails', (done) => {
        process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
        request.get('area/1921').end((err,res) => {
            expect(res).to.have.status(200);
            let array1 = Object.entries(res.body);
            expect(array1).to.be.a('array');
            expect(res.body).to.be.jsonSchema(CheckSchema);
            done();
        });
    });
});

CodePudding user response:

You need the chai-http plugin to be able to do:

expect(res).to.have.status(200);

Reference: https://www.chaijs.com/plugins/chai-http/

CodePudding user response:

From the doc chai-http

The Chai HTTP module provides a number of assertions for the expect and should interfaces.

.status (code) assertion is one of them.

supertest will not provide these assertions for the expect and should interfaces of chai.

See chai-http/blob/4.3.0/lib/http.js#L79

 Assertion.addMethod('status', function (code) {
    var hasStatus = Boolean('status' in this._obj || 'statusCode' in this._obj);
    new Assertion(hasStatus).assert(
        hasStatus
        , "expected #{act} to have keys 'status', or 'statusCode'"
        , null // never negated
        , hasStatus // expected
        , this._obj // actual
        , false // no diff
    );

    var status = this._obj.status || this._obj.statusCode;

    this.assert(
        status == code
      , 'expected #{this} to have status code #{exp} but got #{act}'
      , 'expected #{this} to not have status code #{act}'
      , code
      , status
    );
  });
  • Related