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

@types/chai seems not to be adapted to version 5.0.0 #1578

Open
dhjnam opened this issue Jan 14, 2024 · 11 comments
Open

@types/chai seems not to be adapted to version 5.0.0 #1578

dhjnam opened this issue Jan 14, 2024 · 11 comments
Labels

Comments

@dhjnam
Copy link

dhjnam commented Jan 14, 2024

import chai from 'chai' does not work with chai version 5.0.0.

// test.spec.ts
import chai from 'chai';
chai

and executing

ts-mocha ./test.spec.ts"

Raises the following error

Error [ERR_REQUIRE_ESM]: require() of ES Module <project-root>/node_modules/chai/chai.js from <project-root>/test.spec.ts not supported.
Instead change the require of chai.js in <project-root>/test.spec.ts to a dynamic import() which is available in all CommonJS modules.

at Object.<anonymous> (<project-root>/test.spec.ts:6:32)
at m._compile (<project-root>/node_modules/ts-mocha/node_modules/ts-node/dist/index.js:327:29)
at require.extensions.<computed> [as .ts] (<project-root>/node_modules/ts-mocha/node_modules/ts-node/dist/index.js:329:16)
at <project-root>/node_modules/mocha/lib/mocha.js:414:36
at Array.forEach (<anonymous>)
at Mocha.loadFiles (<project-root>/node_modules/mocha/lib/mocha.js:411:14)
at Mocha.run (<project-root>/node_modules/mocha/lib/mocha.js:972:10)
at Object.run (<project-root>/node_modules/mocha/lib/cli/watch-run.js:263:22)
at FSWatcher.<anonymous> (<project-root>/node_modules/mocha/lib/cli/watch-run.js:184:14)

Other imports work fine.

tsconfig.json:

{
  "compilerOptions": {
    "composite": true,
    "target": "ES6",
    "module": "Node16",
    "rootDir": "./",
    "moduleResolution": "Node16",
    "typeRoots": [
      "./node_modules/@types/",
    ],
    "declaration": true,
    "declarationMap": true,
    "sourceMap": true,                
    "outDir": "./dist/",
    "declarationDir": "./dist/@types",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "skipLibCheck": true,
  },
}

package.json:

{
  "name": "chai5",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "ts-mocha test.spec.ts"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/chai": "^4.3.11",
    "@types/mocha": "^10.0.6",
    "chai": "^5.0.0",
    "mocha": "^10.2.0",
    "ts-mocha": "^10.0.0",
    "typescript": "^5.3.3"
  }
}
@43081j
Copy link
Contributor

43081j commented Jan 14, 2024

This is because the project is esm only now.

As the error message says, you would have to use a dynamic import to import it since you're in a commonjs project.

Your package.json doesn't have type: "module", so typescript (with node16/nodenext resolution) will detect your project as commonjs and parse it as such.

I have an example here:
https://gist.github.com/43081j/78ce1392abb5043b02a29355006880a5

if you're unable to make your project ESM-only, you will have to use a dynamic import (const chai = await import('chai')), or stick to 4.x until you're able to migrate forward.

We're still publishing 4.x versions for security etc.

@rufreakde
Copy link

With mocha I did someting like so:

const chaiHttp = require("chai-http");
let chai;
before(async () => {
  await import("chai").then((result) => {
    chai = result;
    // Configure chai
    chai.use(chaiHttp);
    chai.should();
  });
});

issue is that somehow chai.request is failing...

TypeError: chai.request is not a function

is chai-http 4.4.0 not compatible with chai 5.x?

@rufreakde
Copy link

Okay it seems with the new version the use functionality is broken...
#1569 interesting

@43081j
Copy link
Contributor

43081j commented Jan 20, 2024

for now (maybe permanently, depending on the outcome of 1569), you can do:

await import("chai").then((result) =>
  chai = result.use(chaiHttp);
});

@caiosantos10
Copy link

You can try import just methods from 'chai' like this:

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

@melroyvandenberg
Copy link

I can't take it anymore with Chai + Cha-http with ESM (even when not using TS, but just JS).

It just impossible to import those two libs. I get SyntaxError: The requested module 'chai' does not provide an export named 'request' and if I use import * as chai from 'chai to allow me to use chai.request I now get: TypeError: chai.request is not a function

@caiosantos10
Copy link

I can't take it anymore with Chai + Cha-http with ESM (even when not using TS, but just JS).

It just impossible to import those two libs. I get SyntaxError: The requested module 'chai' does not provide an export named 'request' and if I use import * as chai from 'chai to allow me to use chai.request I now get: TypeError: chai.request is not a function

You can try this way:

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

and then call:

chai.request()

@melroyvandenberg
Copy link

Ow so use returns a new object?.. interesting! Thanks. I still believe chai should improve this ESM import / usage.

@43081j
Copy link
Contributor

43081j commented Apr 16, 2024

yes, use returns an extended chai

import {use} from 'chai';
import chaiHttp from 'chai-http';

const chai = use(chaiHttp);

we have an issue open to investigate if there's any way we can improve this pattern

@melroyvandenberg
Copy link

melroyvandenberg commented Sep 20, 2024

Today I suffer again with the same problem but this time just using plain ES6 javascript (no typescript): TypeError: chai.request is not a function. Whenupgrading chai-http package to version: 5.0.0..

Yes my code looks like this (I removed the should & expect imports for clarity):

import { use } from 'chai'
import chaiHttp from 'chai-http'
import app from '../src/index.js'

const chai = use(chaiHttp)

// ... some it(... ) test.... And then:
chai.request(app) // <-- this line is failing still!!
      .get("/example")

Vscode is saying this is valid:

image

OK, moving to chai.request.execute solved the problem. But I believe the index.d.ts is still outdated.

@khawaja-h3
Copy link

This worked for me:

process.env.NODE_ENV = "test";

import chaiHttp from "chai-http";
import app from "../main.js";
import * as chai from "chai";
const should = chai.should();
const expect = chai.expect;
const use = chai.use;

const request = use(chaiHttp).request.execute;
describe("/First Test Collection", () => {
  it("test default API welcome route...", (done) => {
    request(app)
      .get("/") // Endpoint to test
      .end((err, res) => {
        res.should.have.status(200); // Check status code
        res.body.should.be.a("object");
        done(); // Signal async completion
      });
  });

  it("expects two values to be equal....", () => {
    // given
    let expectedValue = 10; // when
    let actualValue = 5;
    expect(actualValue).not.to.equal(expectedValue); // then
  });
});

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

No branches or pull requests

7 participants