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

Jest has an issue when trying to import the library #99

Open
iamBevan opened this issue Jan 12, 2023 · 5 comments
Open

Jest has an issue when trying to import the library #99

iamBevan opened this issue Jan 12, 2023 · 5 comments

Comments

@iamBevan
Copy link

Current Behavior

When trying to run Jest in a repo that is importing vanilla-colorful, I get the following error message: "Cannot use import statement outside a module"

Steps to Reproduce

  1. Import vanilla-colorful into project
  2. Try to run Jest

Context (Environment)

  • vanilla-colorful 0.7.2
  • jest 27.5.1
  • vue 2.6.14

Detailed Description

Currently I am importing vanilla-colorful in to a Vue project. When I try to run Jest I get the following error message:
image

I have tried what Jest suggests; adding experimental decorators and get the following message:
image

I have also tried tried adding vanilla-colorful as a custom transformIgnorePattern.

Any insight on this would be appreciated, thanks.

@web-padawan
Copy link
Owner

Hi, this is likely an issue with Jest configuration. AFAIK it has some issues with using ES modules.
The component has correct fields in package.json indicating that is a ES module package:

"module": "hex-color-picker.js",
"type": "module",

Here is an example of using transformIgnorePatterns with a set of packages, hope it helps.

@woahitsjc
Copy link

woahitsjc commented Jan 25, 2023

Got it working by using one of the approaches from https://gist.github.com/rstacruz/511f43265de4939f6ca729a3df7b001c

Method A got rid of the issue by:

  • Running a jest script with cross-env required: NODE_OPTIONS=--experimental-vm-modules
  • Making sure to use dynamic import: import("vanilla-colorful")

@web-padawan
Copy link
Owner

Thanks for sharing! I will try it myself and consider adding this solution to README. The only thing that looks confusing is the need for dynamic import.

@woahitsjc
Copy link

woahitsjc commented Jan 26, 2023

@web-padawan I spoke too soon. When running jest in CI, getting memory leak.

Possibly the same issue as on either of these:
jestjs/jest#11438
jestjs/jest#11956

@web-padawan
Copy link
Owner

Got it, thanks. BTW, have you tried Vitest that uses happy-dom implementation instead of jsdom used by Jest?
It should be much easier to get it working with Vanilla Colorful because of built-in ES modules support.

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

No branches or pull requests

3 participants