Reputation: 14219
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
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
Reputation: 102672
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)
esModuleInterop
is true
, you should use import chai from 'chai';
instead of using import * as chai from 'chai';
Upvotes: 1