-
-
Notifications
You must be signed in to change notification settings - Fork 700
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
v5 - Unknown file extension ".ts"
when running with mocha
#1568
Comments
Failing PR |
Unknown file extension ".ts"
when running mochaUnknown file extension ".ts"
when running with mocha
Having the same issue here. Need to stay in v4 for now. |
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. |
@43081j - does this mean that chai 5.x will not run on anything but ESM? |
@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 |
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/). |
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? |
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 |
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). |
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 |
here's an example of mocha 10 + chai 5 + ts-node: https://gist.github.com/43081j/78ce1392abb5043b02a29355006880a5 |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
In my tests, I only use
import { expect } from "chai"
.npm run test
(mocha) returns:This wasn't happening before the change with
"chai": "4.3.10"
Other files:
The text was updated successfully, but these errors were encountered: