Asool
Asool

Reputation: 14219

Chai.request is not a function

I have the following simple code

import app from '../src/app';
import * as chai from 'chai';
import chaiHttp = require('chai-http');

chai.use(chaiHttp);
const expect = chai.expect;

describe('Get /', () => {
    it('Should say hi there', async () => {
        const response = chai.request(app).get('/');
        console.log(response);
        expect(5).to.equal(5);
    });
});

Each time I run

mocha -r ts-node/register lib/tests/**/sample.spec.ts

I get the following error

TypeError: chai.request is not a function

I looked at the other stackoverflow posts with the same question. They all said that adding

chai.use(chaiHttp) 

should fix the problem

But, as you can see I already have that.

Any ideas?

Upvotes: 0

Views: 1925

Answers (2)

Yonas Alem
Yonas Alem

Reputation: 1

use chai-http with chai

const chai = require("chai");
const chaiHttp = require("chai-http");

then chai.use(chaiHttp); this works for me.

Upvotes: 0

Lin Du
Lin Du

Reputation: 102672

  1. If the esModuleInterop is false in your tsconfig.json, it should work fine.
import * as chai from 'chai';
import chaiHttp = require('chai-http');

chai.use(chaiHttp);
const expect = chai.expect;

describe('Get /', () => {
  it('Should say hi there', async () => {
    expect(chai.request).to.be.a('function');
  });
});

test result:

  Get /
    ✓ Should say hi there


  1 passing (4ms)
  1. If the esModuleInterop is true, you should use import chai from 'chai'; instead of using import * as chai from 'chai';

Upvotes: 1

Related Questions