chaijs / chai-http

HTTP Response assertions for the Chai Assertion Library.
http://chaijs.com/plugins/chai-http
633 stars 113 forks source link

How to use chai-http and chai 5? #313

Closed topd5 closed 8 months ago

topd5 commented 8 months ago

Chai was updated to 5th version. With new version chai-http doesn't work:

SyntaxError: The requested module 'chai' does not provide an export named 'request'

How to use it with 5th version of chai?

43081j commented 8 months ago

you will have to do something like this:

import {chai as chaiModule} from 'chai';

const chai = chaiModule.use(chaiHttp);
topd5 commented 8 months ago

Thanks, but this is not a solution.

import { expect, use, request } from 'chai';
SyntaxError: The requested module 'chai' does not provide an export named 'request'
keithamus commented 8 months ago

request is not part of chai, it is part of chai-http. One of the breaking changes in v5 of Chai is that it no longer re-exposes exports from libraries. You'll need to do this:

import { expect, use } from 'chai';
import { request } from 'chai-http';
topd5 commented 8 months ago

@keithamus It doesn't work:

import chaiHttp, { request } from 'chai-http';
                   ^^^^^^^
SyntaxError: Named export 'request' not found. 

In "node_modules/chai-http/lib/http.js" there is code:

module.exports = function (chai, _) {
  // ...
  chai.request = require('./request');
keithamus commented 8 months ago

If you follow @43081j's notes and avoid importing request:

import {chai as chaiModule} from 'chai';
const chai = chaiModule.use(chaiHttp);
const request = chai.request;
topd5 commented 8 months ago

Thank you. The way it helped:

import * as chaiModule from 'chai';
const chai = chaiModule.use(chaiHttp);

then usage:

const resp = await chai.request(app)
// ...