chai.request is not a function while using request js for http service unit test

aka picture aka · May 5, 2016 · Viewed 10.5k times · Source

I have been using karma+requestjs + mocha + chai and sinon. i have been using chai-http module yet receives chai.request is not a function.please suggest where i am making mistake i have googled lot no luck yet.

(function() {
  var specFiles = null;
  var baseUrl = '';
  var requirejsCallback = null;
  if (typeof window != 'undefined' && window.__karma__ != undefined) {

    baseUrl = '/base';
    requirejsCallback = window.__karma__.start;
    specFiles = [];
    for (var file in window.__karma__.files) {
      if (window.__karma__.files.hasOwnProperty(file)) {
        if (/.*\/js\/spec\/.+_spec\.js$/.test(file)) {
          specFiles.push(file);
        }
      }
    }
  }

  requirejs.config({
      baseUrl: baseUrl,

      paths: {
        'chai': './node_modules/chai/chai',
        'sinon': './node_modules/sinon/pkg/sinon',
         'chaihttp': './node_modules/chai-http/dist/chai-http',
      },

      deps: specFiles,
      callback: requirejsCallback
  });
})();


**Spect-Test.js**

 define(['chai', 'sinon', 'chaihttp'], function (chai, sinon, chaihttp) {

        var expect = chai.expect;

          describe('Service', function () {

              it('abctest', function () {
                  var abccode = { "abc": "1" };
                  var url = 'http://localhost:1234';
                  chai.request(url)
                      .post('test/testService')
                      .send(abccode )

                      .end(function (err, res) {
                          if (err) {
                              throw err;
                          }
                          expect(res.status).to.equal(200);
                          done();
                      });

             });

        });
    });

Error TypeError: chai.request is not a function at Context. (

Answer

Adam F picture Adam F · Aug 15, 2017

As per mido's comment on this question, using chai.use(chaiHttp) worked for me.