Closed Jayakrishnan-MS closed 8 months ago
@Jayakrishnan-MS Are you sure that jest is running the code as ESM? The import in line 2 doesn't look like an ESM import:
import {NF} from './Constants';
ESM requires to add the file extension to the import, unlike CJS which just appends it if it's missing. That would also explain why Chevrotain 10 was working, which delivered a CJS package.
Yeah, you are right. Jest is running the code in CJS even though my project is in ESM. So the error occurred since Chevrotain 11 is ESM-only and jest tries to run it in CJS. So I guess the workaround is to enable ESM support in Jest by running Node.js with the --experimental-vm-modules like this: { "scripts": { "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js" } } But this will affect my other imports. So I'll stick on with the Chevrotain 10 in my project. Thanks for the suggestion!!
I couldn't make jest work with 11. Downgrading to 10.5 for now
Looks like Jest support for ESM relies on experimental APIs
Same here, downgrading to 10.5 to make it work. If you find a fix, please let us know :pray:
yes, downgrading to 10.5.0
works fine, but with 11.0.3
tests failing
You could probably bundle Chevrotain as a commonJS module in your project's postinstall
script
and consume it via the bundled version to work around ESM compatibility issues.
Greetings,
Recently I converted my react project to ESM. Everything works fine until it comes to unit testing. I am using chevrotain latest version ^11.0.0 in my project. While running unit test using jest I am getting the following error:
$ npm run test tokens.test.ts
FAIL src/tokens.test.ts ● Test suite failed to run
The test runs fine when using the older version (^10.0.0) of chevrotain. I think the issue is related to the latest version of chevrotain as no other modules have this issue.