Skip to content

Conversation

@miorel
Copy link
Contributor

@miorel miorel commented Sep 21, 2024

Getting Jest to work with ESM modules has been painful, and the guide at https://jestjs.io/docs/ecmascript-modules doesn't seem to work when the Jest config is in a TypeScript file.

Since I'm stubborn, I decided to try to find a workaround, and I found this one! We make the config a runnable script that runs the Jest command, rather than relying on the Jest command to read the config. This is a bit funny, but it seems to work, because Jest requires configuration to be JSON-serializable: https://jestjs.io/docs/configuration and the command-line config option (https://jestjs.io/docs/cli#--configpath) accepts a JSON string!

Getting Jest to work with ESM modules has been painful, and the guide at https://jestjs.io/docs/ecmascript-modules doesn't seem to work when the Jest config is in a TypeScript file.

Since I'm stubborn, I decided to try to find a workaround, and I found this one! We make the config a runnable script that runs the Jest command, rather than relying on the Jest command to read the config. This is a bit funny, but it seems to work, because Jest requires configuration to be JSON-serializable: https://jestjs.io/docs/configuration and the command-line config option (https://jestjs.io/docs/cli#--configpath) accepts a JSON string!
@miorel miorel merged commit 7337a6f into main Sep 22, 2024
6 checks passed
@miorel miorel deleted the jest-ts-config-esm branch September 22, 2024 08:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants