mochajs / mocha

β˜•οΈ simple, flexible, fun javascript test framework for node.js & the browser

Home Page:https://mochajs.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

πŸ› Bug: Mocha (10.x) and Chai (5.x) don't work together

GuyKh opened this issue Β· comments

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.

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

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.

@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

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/

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).

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

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

chaijs/chai#1575 (comment)

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! 🀎