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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 Bug: Mocha (10.x) and Chai (5.x) don't work together #5073

Closed
3 of 4 tasks
GuyKh opened this issue Jan 3, 2024 · 8 comments
Closed
3 of 4 tasks

馃悰 Bug: Mocha (10.x) and Chai (5.x) don't work together #5073

GuyKh opened this issue Jan 3, 2024 · 8 comments
Labels
status: wontfix typically a feature which won't be added, or a "bug" which is actually intended behavior

Comments

@GuyKh
Copy link

GuyKh commented Jan 3, 2024

Bug Report Checklist

  • I have read and agree to Mocha's Code of Conduct and Contributing Guidelines
  • I have searched for related issues and issues with the faq label, but none matched my issue.
  • I have 'smoke tested' the code to be tested by running it outside the real test suite to get a better sense of whether the problem is in the code under test, my usage of Mocha, or Mocha itself.
  • I want to provide a PR to resolve this

Expected

mocha --recursive to work.

Actual

$ mocha --recursive

TypeError: Unknown file extension ".ts" for /Users/me/git/myProj/test/app.test.ts
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:160:9)
    at defaultGetFormat (node:internal/modules/esm/get_format:203:36)
    at defaultLoad (node:internal/modules/esm/load:143:22)
    at async ModuleLoader.load (node:internal/modules/esm/loader:409:7)
    at async ModuleLoader.moduleProvider (node:internal/modules/esm/loader:291:45)
    at async link (node:internal/modules/esm/module_job:76:21)

Minimal, Reproducible Example

described in this issue:
GuyKh/10bis.Slackbot#1021

Versions

Mocha - 10.2.0
Chai - 5.0.0

Additional Info

As described in this issue in Chai repo - it seems that Chai 5.x is ESM (=Module).

Settings examples are there.

@GuyKh GuyKh added the type: bug a defect, confirmed by a maintainer label Jan 3, 2024
@43081j
Copy link

43081j commented Jan 5, 2024

they do work together, chai has just moved to shipping pure ESM which means CJS consumers must now use dynamic import or a bundler.

the error will be from trying to import chai into a commonjs module

you can instead do a dynamic import, const chai = await import('chai');. or stick to chai 4.x for now

@MuTsunTsai
Copy link

the error will be from trying to import chai into a commonjs module

In my case all my test modules are ESM already, but still the same error. Besides, the error is about "unknown .ts extension" of the test/spec file itself, which means that the execution didn't even enter the test module yet. In that case, I don't think the way chai being imported in the test module makes a difference. But again, I'm doing import { expect } from "chai" already.

@voxpelli
Copy link
Member

voxpelli commented Jan 6, 2024

@MuTsunTsai Is your TypeScript configured to compile your imports to ESM or CommonJS? Often projects write ESM imports in TypeScript only to have it compile it to CommonJS

@43081j
Copy link

43081j commented Jan 6, 2024

the error will be from trying to import chai into a commonjs module

In my case all my test modules are ESM already, but still the same error. Besides, the error is about "unknown .ts extension" of the test/spec file itself, which means that the execution didn't even enter the test module yet. In that case, I don't think the way chai being imported in the test module makes a difference. But again, I'm doing import { expect } from "chai" already.

you are right that the error isn't necessarily about trying to import CJS

i suspect it is from using ts-node with mocha, and having require: 'ts-node/require'. which is correct for a commonjs project, but in an ESM project, it should be loader: 'ts-node/esm' (or both):

https://typestrong.org/ts-node/docs/recipes/mocha/

@JakobJingleheimer
Copy link

I'm using mocha & chai in a pure ESM project, before and after upgrading chai to 5.x.

The problem appears to come from how Mocha is including Chai. Chai declares itself as ESM and Mocha is trying to require it via mocha/lib/nodejs/esm-utils.js.

PS I think the comment within Mocha's formattedImport() is outdated (I think nodejs does now provide the info you need).

@43081j
Copy link

43081j commented Jan 10, 2024

here's an example which works:

https://gist.github.com/43081j/78ce1392abb5043b02a29355006880a5

if you don't use ts-node (like me), you can just build the typescript and run the resulting JS the same way.

mocha 10 does work with chai 5, and with most (or all) other ESM packages

@MuTsunTsai
Copy link

I've pointed out a different solution in this discussion:

chaijs/chai#1575 (comment)

@JoshuaKGoldberg
Copy link
Member

Per chaijs/chai#1575, Mocha does work with Chai 5.x. It's just up to users to configure the ESM shenanigans correctly.

Filed mochajs/mocha-examples#77 to track adding in an example to our examples repo.

Thanks for the discussion all! 馃

@JoshuaKGoldberg JoshuaKGoldberg closed this as not planned Won't fix, can't repro, duplicate, stale Feb 7, 2024
@JoshuaKGoldberg JoshuaKGoldberg added status: wontfix typically a feature which won't be added, or a "bug" which is actually intended behavior and removed type: bug a defect, confirmed by a maintainer labels Feb 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: wontfix typically a feature which won't be added, or a "bug" which is actually intended behavior
Projects
None yet
Development

No branches or pull requests

6 participants