0

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?

Asool
  • 7,423
  • 4
  • 25
  • 41

2 Answers2

1
  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';
slideshowp2
  • 38,463
  • 29
  • 127
  • 255
0

use chai-http with chai

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

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

Khalid Ali
  • 1,104
  • 1
  • 6
  • 12