Comments (8)
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
from mocha.
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.
from mocha.
@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
from mocha.
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/
from 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).
from mocha.
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
from mocha.
I've pointed out a different solution in this discussion:
from mocha.
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! 🤎
from mocha.
Related Issues (20)
- 🐛 Bug: Eleventy template builds crash with 'unexpected token at ": string, msg..."' HOT 1
- 🐛 Bug: mocha does not compatible with node.js test runner HOT 1
- 📝 Docs: Remove Matomo from the website HOT 1
- 🐛 Bug: Failed to run tests with a large tests count HOT 1
- 🛠 Repo: Tests broken on main for xunit after conflicting merge
- 🛠 Repo: Remove `husky` for now HOT 3
- 🛠 Repo: Replace `nps` with ordinary npm scripts
- 🐛 Bug: Audit and fix accessibility for browser reporter
- 🚀 Feature: Unify `--ignore` and `--watch-ignore` and make the patterns recursive
- 🐛 Bug: Unit test cases taking too much time to execute HOT 1
- 🐛 Bug: <short description of the bug>
- `skip` shows "pending" instead of "skipped"
- 🐛 Bug: Test titles not shown for ES dynamic imports
- 🐛 Bug: mocha fails silently on invalid `package.json` section
- Bump chokidar HOT 3
- 🐛 Bug: [email protected] has a performance bug HOT 1
- 🐛 Bug: Error Importing TypeScript Class: Only .js Extension Supported, Not .ts HOT 1
- 🐛 Bug: when using exports interface for tests, there's no file on suites objects HOT 2
- 🚀 Feature: Update glob from 8.1.0 which is no longer supported HOT 1
- 🐛 Bug: Watch + mochaHooks inconsistent state on re-runs
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mocha.