Chevrotain / chevrotain

Parser Building Toolkit for JavaScript
https://chevrotain.io
Apache License 2.0
2.44k stars 199 forks source link

Cannot use chevrotain with Jest #2028

Closed ccastillo232 closed 2 months ago

ccastillo232 commented 2 months ago

I'm trying to test a component that uses chevrotain. When I run it I get the following:

FAIL src/tests/Lexer.test.ts ● Test suite failed to run

Cannot find module 'chevrotain' from 'src/app/utils/parserDefinition.ts'

It looks like this is due to a missing main: configuration in chevrotain's package.json. (see this as an example: https://stackoverflow.com/questions/53466859/jest-cannot-find-module-from-node-modules/68724825#68724825 )

I'm not sure how to resolve this even temporarily.

Edit: after debugging the jest-resolve defaultResolver.js the specific error thrown is: Error: No known conditions for "." specifier in "chevrotain" package

msujew commented 2 months ago

Hey @ccastillo232,

most "older" testing frameworks (jest, mocha) don't support ESM-only packages. I would recommend you to switch to version 10.5.0 and see whether the issue occurs there. Version 11 and 10 are identical in features, but 10 exposes CJS code, while 11 only has ESM. Alternatively, I can recommend to use vitest, which is running in ESM by default. I've been using that together with chevrotain@11 successfully.

ccastillo232 commented 2 months ago

Thanks @msujew the switch to 10.5.0 resolved the issue. I did briefly go down the road of configuring jest for ESM, but then thought better of it. I may give vitest a try.