vanilla-colorful icon indicating copy to clipboard operation
vanilla-colorful copied to clipboard

Jest has an issue when trying to import the library

Open iamBevan opened this issue 2 years ago • 5 comments

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.

iamBevan avatar Jan 12 '23 10:01 iamBevan

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.

web-padawan avatar Jan 12 '23 10:01 web-padawan

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")

woahitsjc avatar Jan 25 '23 12:01 woahitsjc

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.

web-padawan avatar Jan 26 '23 06:01 web-padawan

@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

woahitsjc avatar Jan 26 '23 11:01 woahitsjc

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.

web-padawan avatar Jan 26 '23 17:01 web-padawan