Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SyntaxError: Unexpected token 'export' with jest on version 4.0.0 #10

Closed
jakedaleweb opened this issue Apr 6, 2021 · 4 comments
Closed

Comments

@jakedaleweb
Copy link

jakedaleweb commented Apr 6, 2021

Hi there,

I'm upgrading a project from version 3.1.0 -> 4.0.0 of this project but am having an issue when trying to run jest tests.
When running tests I see SyntaxError: Unexpected token 'export' referencing the root index.js file of this project.

I am running node 14.16.0.

Google suggests this could be related to the way in which the export is defined and how that works with node - https://stackoverflow.com/questions/38296667/getting-unexpected-token-export

I am importing as import {stringifyEntities} from 'stringify-entities' and calling as stringifyEntities(value, {escapeOnly: true})

Any ideas? 😃

Thanks,
Jake.

@ChristianMurphy
Copy link
Collaborator

The switch to ESM is intentional unifiedjs/unified#121

There was some discussion about Jest support in https://twitter.com/kentcdodds/status/1369803853005209602 and sindresorhus/meta#15

Short answer this is something that needs to be fixed in Jest and is in progress https://jestjs.io/docs/ecmascript-modules
In the meantime there are work around like https://github.com/aelbore/esbuild-jest and https://gist.github.com/rstacruz/511f43265de4939f6ca729a3df7b001c which allow ES2015 to be used in Jest.

@jakedaleweb
Copy link
Author

@ChristianMurphy Thanks for the details - you can consider this issue resolved now. Will comment again or raise a new one should it be needed.

Thank you!

@ZeldOcarina
Copy link

Hey @ChristianMurphy do you happen to know if this has ever been resolved? I still struggle to use unified in Jest, it throws this very error for me.

@ChristianMurphy
Copy link
Collaborator

Heya @ZeldOcarina!
Sorry you ran into a spot of trouble.

The answer remains the same as previous comments.
This is not a bug in stringify-entities.
EcmaScript Modules (ESM) are the official module format of JavaScript and have been since 2015.
Some libraries and toolchains still don't support ESM, which is something which can be raised with the tool lacking support itself.
And if the tool is still unable or unwilling to support standard JavaScript all these years later, consider using something different 😅

Jest is one of the libraries that has struggles.
It has experimental support, but has hit a number of roadblocks with how it uses an internal vm.
If you're using Jest you're most likely using it in the context of a React project, you can see some threads discussing how to configure Jest which may work, here: facebook/create-react-app#11946 and here: remarkjs/react-markdown#635

Though with the challenges folks have faced, I come back to:

if the tool is still unable or unwilling to support standard JavaScript all these years later, consider using something different 😅

There are alternatives like:

I'm going to keep this issue closed, as fixing JavaScript support in jest isn't something with stringify-entities.

Feel free to follow up with the Jest community if you have questions on how to configure jest.
Or to try out any of the alternative testing libraries listed above to see if they suit your needs.

Repository owner locked as resolved and limited conversation to collaborators Feb 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants