最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - chai.request is not a function while using request js for http service unit test - Stack Overflow

programmeradmin1浏览0评论

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. (

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. (

Share Improve this question edited May 5, 2016 at 9:25 aka asked May 5, 2016 at 1:32 akaaka 1991 gold badge4 silver badges15 bronze badges 4
  • 13 may be you missed the line chai.use(chaiHttp) ... – mido Commented May 5, 2016 at 1:50
  • @mido i tried chai.use('chai-http') but now gives hrome (Windows 10 0.0.0) ERROR: TypeError{} Chrome (Windows 10 0.0.0): Executed 0 of 0 ERROR (0.029 secs / 0 secs) – aka Commented May 5, 2016 at 2:03
  • var chai = require('chai'); var chaiHttp = require('chai-http'); var expect = chai.expect; var should = chai.should; chai.use(chaiHttp); Module name "chai-http" has not been loaded yet for context: _. Use require([])(…) – aka Commented May 5, 2016 at 7:58
  • 2 var chai = require('chai'), chaiHttp = require('chai-http'), chai.use(chaiHttp); now chai.request(server)... worked for me – Abilash Arjunan Commented Jul 5, 2017 at 15:29
Add a comment  | 

4 Answers 4

Reset to default 8

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

You should add this at the beginning:

var chai = require('chai'), chaiHttp = require('chai-http');

chai.use(chaiHttp);

I got the problem but to solve it I checked what is in the chai library (using console.log) and I found that the request function is under default node.

import * as chai from 'chai';
import chaiHttp = require('chai-http');

chai.use(chaiHttp);

//Parse the assertion library to get the request function as chai.request is not found
let chaiLib = <any>chai;
let chaiRequestLib = chaiLib.default.request;

chaiRequestLib can be used then.

  return chaiRequestLib(server).post('/api/product')
    .send(product)
    .then((res: any) => {
      res.should.have.status(200);
      res.body.should.be.a('object');
      chai.assert.equal(res.body.affectedRows, 1 , '"Dexeryl Cream 250g" product not created');
    })

Try this import method, it worked for me

import app from '../../server'
import chai from 'chai'
import chaiHttp = require('chai-http')
import 'mocha'
const expect = chai.expect;
chai.use(chaiHttp);
发布评论

评论列表(0)

  1. 暂无评论