Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chai import error #338

Open
rnura1234 opened this issue Aug 30, 2024 · 3 comments
Open

chai import error #338

rnura1234 opened this issue Aug 30, 2024 · 3 comments

Comments

@rnura1234
Copy link

I am import chai as chaiModule, but getting error like
import chaiModule from "chai";
^^^^^^^^^^
SyntaxError: The requested module 'chai' does not provide an export named 'default'

Code for Reference

import chaiModule from "chai";
import chaiHttp from "chai-http";

const chai = chaiModule.use(chaiHttp);
describe('Users API Testing', () => {

    it('GET all the users', (done) => {
        chai.request('http://localhost:3000')
        .get('/')
        .get('/api/users')
        .end((err, res) => {
            if (err) done(err);
            expect(res).to.have.status(200);
            expect(res.body).to.be.an('array');
            done();
        });
    });
});

and when Import like
import * as chai from 'chai',
getting error - chai.request is not a function,

I tried different way to import , but getting same error.

@43081j
Copy link
Contributor

43081j commented Aug 30, 2024

import * as chaiModule from "chai";
import chaiHttp from "chai-http";

const chai = chaiModule.use(chaiHttp);

Should work as expected

@boly38
Copy link
Contributor

boly38 commented Dec 14, 2024

I'm trying to find a good way to use chai http chai.request and especially chai http agent chai.request.agent (agent doc) with latest versions of chai and chai-http but unfortunately, I always encounter issue

    "chai": "^5.1.2",
    "chai-http": "^5.1.1",

With

import * as chaiModule from "chai";
import chaiHttp from "chai-http";
const chai = chaiModule.use(chaiHttp);

or

import {use} from 'chai';
import chaiHttp from "chai-http";
const chai = use(chaiHttp);

I got

# chai.request('http://localhost:3000')
=> request is not a function

#  const agent = chai.request.agent(appListening);
#  agent.post('/login')
=> Cannot read properties of undefined (reading 'agent')`.

Are they compatible? I would love a fully fonctionnal doc/sample/gist for request & request.agent latest compatible versions.

I noticed that

  • with chai in ^4.0.0 (currently 4.5.0) + chai-http version (5.1.1) : .request.agent(app).get is working but no .request(url)
  • with chai in ^4.0.0 (currently 4.5.0) + and chai-http in ^4.0.0 (4.4.0) : .request.agent(app).get AND .request(url) both works

@43081j
Copy link
Contributor

43081j commented Dec 15, 2024

They are compatible.

The examples throughout the current readme should be working fine. They import request instead of accessing it from the chai object. Though you should be able to access it from the chai object too

You gave good example code but none for the actual test file where the problem arises. Just that "request is not a function". Maybe if you show us code you're trying to run that causes that error, we will figure something out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants