Reputation: 1702
I have a Typscript app and API. I wrote the below test per numerous Google searches and some examples found here on SO and other places. I see no issue in the test code. Googling TypeError: chai.request is not a function
, so far is getting me now where. Do you see my error below?
Thank you, thank you, thank you for any help :-)
Upvotes: 9
Views: 16187
Reputation: 2120
Other solutions fails as Chai 5 is an ES Module. It cannot be imported as a default. You can use chai 5.1 and chai-http 5.1 as below in your test.mjs
files:
import { expect, use } from 'chai';
import { default as chaiHttp, request } from 'chai-http';
use(chaiHttp);
// execute method will take express app as input and returns ChaiHttp.Agent
req = request.execute(global.app);
// Use req object to request API calls on app
req.get('/')
Upvotes: 0
Reputation: 1
import { use, expect } from 'chai'
import chaiHttp from 'chai-http'
const chai = use(chaiHttp)
chai.request()
Using imports like this will save your day.
Upvotes: -1
Reputation: 121
Use :
const chai =require('chai');
const chaiHttp = require('chai-http');
chai.use(chaiHttp);
Then use
describe('Chat', () => {
it('should return all data',async()=>{
chai.request("https://google.com")
.get('/')
.end((err, res) => {
expect(res).to.have.status(200);
done();
});
})
})
Upvotes: 0
Reputation: 5452
You are mixing import
and require
syntax, it's a bad idea!
Use only import
syntax :
import * as chai from 'chai';
import * as chai-http from 'chai-http';
chai.use(chai-http);
Edit
Unfortunately, it seems that the es6 module syntax is not supported in chai-http. You can see the issue here
import * as chai from 'chai';
import chaiHttp = require('chai-http');
chai.use(chai-http);
Upvotes: 4
Reputation: 1213
I solved this by the following approach (Express.js with TypeScript)
import chai from 'chai';
import chaiHttp from 'chai-http';
chai.use(chaiHttp);
Hope it helps.
Remember to install @types/chai and other type definition packages for the block above to work
Upvotes: 12
Reputation: 148
install from github
"devDependencies": {
"chai-http": "git+https://github.com/chaijs/chai-http.git",
},
Upvotes: -1
Reputation: 1702
Thank you very much for the replies! Ultimately I had to change where/how chai.request
was being imported and rewrite the test a good bit. Based on the code in the test you might think 1 or more of the first 5 lines are not necessary, but they all are. Assuming the rest api is started up on 3000, the following code works and the test passes.
Am I writing this api request test correctly? I'm just now learning Mocha/Chai so it's probably wrong...
import * as chai from 'chai';
import chaiHttp = require('chai-http');
chai.use(chaiHttp);
import { Response } from 'superagent';
import { request, expect } from 'chai';
describe('AppController', () => {
describe('Route GET /app', () => {
it('Should GET to /app', async () => {
const res: Response = await request('http://0.0.0.0:3000').get('/app');
expect(res).to.have.status(200);
expect(res).to.be.a('object');
});
});
});
Upvotes: 2
Reputation: 8443
I can reproduce the issue on my own machine. This is how I solve it.
import * as chai from 'chai';
import chaiHttp = require('chai-http');
chai.use(chaiHttp);
I also need to install @types/chai-http so the compiler knows.
npm install @types/chai-http --save-dev
Hope it helps
Upvotes: 0
Reputation: 30999
I can reproduce the problem if I enable the esModuleInterop
compiler option. When this option is enabled, import * as chai from 'chai';
only imports the members that the chai
module has at the time it is imported. Indeed, I believe it's considered dodgy to add exports to an ES module at runtime. Try import chai from 'chai';
or import chai = require('chai');
instead; either one is working for me.
Upvotes: 10