-
In my tests, I only use
This wasn't happening before the change with Other files:
|
Beta Was this translation helpful? Give feedback.
Replies: 11 comments 5 replies
-
Failing PR |
Beta Was this translation helpful? Give feedback.
-
Having the same issue here. Need to stay in v4 for now. |
Beta Was this translation helpful? Give feedback.
-
this is likely because of esm vs commonjs. your project builds as commonjs ( i would stick on chai 4.x until you can move your project to es modules instead (i.e. |
Beta Was this translation helpful? Give feedback.
-
@43081j - does this mean that chai 5.x will not run on anything but ESM? |
Beta Was this translation helpful? Give feedback.
-
@GuyKh i put a little more info in #1561 at the end basically, chai 5.x ships as ESM, which means you can only use it if one or more of the following is true:
many projects will be simple to migrate to ESM, especially those used only in node. i don't have a tutorial but if i find a good example repo to migrate, i'd be happy to write up how i did it. importantly too, it is fine to stick with chai 4.x for now |
Beta Was this translation helpful? Give feedback.
-
After some testing, I can confirm that
const chai = require('chai')
chai.use(require('chai-json'))
chai.use(require('chai-xml'))
import chai from 'chai'
import jsonObj from 'chai-json'
import chaiXml from 'chai-xml'
chai.use(jsonObj);
chai.use(chaiXml); unfortunately, not all plugins have types definitions (like chai-json - see issue) and therefore cannot be used.
import chai from 'chai'
import chaiXml from 'chai-xml'
chai.use(chaiXml); It would be useful to add type definitions to all plugins and update the documentation, which currently no longer conforms to the latest breaking changes (i.e.: https://www.chaijs.com/plugins/chai-json/). |
Beta Was this translation helpful? Give feedback.
-
The problem with Mocha runs a little deeper than changing your project to module. This part in the {
"require": ["ts-node/register", "chai/register-expect.js"],
} Mocha seems to use require under the hood, which won't work with 5.0. The Mocha team might have advice? |
Beta Was this translation helpful? Give feedback.
-
mocha supports ESM just fine, you just need to do this instead: {
"loader": "ts-node/esm",
"require": ["chai/register-expect.js"]
} some more info on that here: https://typestrong.org/ts-node/docs/recipes/mocha/ i'll try put a few examples together tonight if i get time, as this mostly seems like gaps in understanding than limitations |
Beta Was this translation helpful? Give feedback.
-
After several days of playing around, I finally found a solution to use chai v5 with the rest of established toolchain (mocha + ts-node + istanbul). The trick is NOT to import // mocha.env.mjs
import { Assertion, expect } from "chai";
globalThis.Assertion = Assertion;
globalThis.expect = expect;
// these are for ts-node
process.env.NODE_ENV = "test";
process.env.TS_NODE_PROJECT = "test/tsconfig.json"; (Note that chai/register-expect.js is doing similar thing and you can use that one also. I wrote my own because I also need And then add it to your .mocharc.json, in my case it looks like this: {
"extension": [
"ts"
],
"spec": [
"test/specs/**/*.ts"
],
"timeout": "0",
"require": [
"./test/mocha.env.mjs",
"ts-node/register",
"tsconfig-paths/register"
]
} Next you just remove all // chai.d.ts
import type * as chai from "chai";
declare global {
declare const expect: typeof chai.expect;
declare const Assertion: typeof chai.Assertion;
} And that's all you need. No need to change tsconfig.json or package.json. Yes, there are other solutions that make chai v5 works by modifying these two, but those solutions break other toolchains (especially istanbul, and I don't want to use c8). |
Beta Was this translation helpful? Give feedback.
-
you shouldn't need to do that, we use mocha 10, chai 5, and typescript with regular imports. i also tried using ts-node with the i suspect something must be missing if you need to avoid importing it like that |
Beta Was this translation helpful? Give feedback.
-
here's an example of mocha 10 + chai 5 + ts-node: https://gist.github.com/43081j/78ce1392abb5043b02a29355006880a5 |
Beta Was this translation helpful? Give feedback.
here's an example of mocha 10 + chai 5 + ts-node:
https://gist.github.com/43081j/78ce1392abb5043b02a29355006880a5