web-padawan / vanilla-colorful

A tiny color picker custom element for modern web apps (2.7 KB) 🎨
https://web-padawan.github.io/vanilla-colorful/
MIT License
784 stars 29 forks source link

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

Open iamBevan opened 1 year ago

iamBevan commented 1 year ago

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)

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 commented 1 year ago

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:

https://github.com/web-padawan/vanilla-colorful/blob/d2afae12bd0324f66a0e3078525915007f91e820/package.json#L16-L17

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

woahitsjc commented 1 year ago

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

Method A got rid of the issue by:

web-padawan commented 1 year ago

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 commented 1 year ago

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

Possibly the same issue as on either of these: https://github.com/facebook/jest/issues/11438 https://github.com/facebook/jest/issues/11956

web-padawan commented 1 year ago

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.