Code Monkey home page Code Monkey logo

Comments (8)

43081j avatar 43081j commented on June 1, 2024 3

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.

MuTsunTsai avatar MuTsunTsai commented on June 1, 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.

from mocha.

voxpelli avatar voxpelli commented on June 1, 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

from mocha.

43081j avatar 43081j commented on June 1, 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/

from mocha.

JakobJingleheimer avatar JakobJingleheimer commented on June 1, 2024

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.

43081j avatar 43081j commented on June 1, 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

from mocha.

MuTsunTsai avatar MuTsunTsai commented on June 1, 2024

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

chaijs/chai#1575 (comment)

from mocha.

JoshuaKGoldberg avatar JoshuaKGoldberg commented on June 1, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.