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

Cannot import due to require() of ESM Module for d3-shape #1266

Open
davidjb opened this issue Jan 3, 2024 · 1 comment
Open

Cannot import due to require() of ESM Module for d3-shape #1266

davidjb opened this issue Jan 3, 2024 · 1 comment
Labels
enhancement Feature request

Comments

@davidjb
Copy link

davidjb commented Jan 3, 2024

Bug description

I've encountered the same problem as #1161, attempting to run Jest tests with amCharts. Here's a simplified worked example that reproduces the issue:

https://gist.github.com/davidjb/39eb1105e82cfde7ad7f200cbc00f4c6

To reproduce:

git clone https://gist.github.com/davidjb/39eb1105e82cfde7ad7f200cbc00f4c6.git example
cd example
npm i
npm t
```bash
$ npm t

> amcharts-demo@1.0.0 test
> NODE_OPTIONS="--experimental-vm-modules" jest

(node:4181) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
(node:4181) ExperimentalWarning: VM Modules is an experimental feature and might change at any time
 FAIL  ./chart.test.mjs
  ✕ example (243 ms)

  ● example

    Must use import to load ES Module: /amcharts-demo/node_modules/d3-shape/src/index.js

      at Runtime.requireModule (node_modules/jest-runtime/build/index.js:850:21)
      at Object.require (node_modules/src/.internal/core/render/Slice.ts:5:1)
      at Object.require (node_modules/src/index.ts:34:1)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        0.566 s, estimated 2 s
Ran all test suites.

Despite the transformIgnorePatterns being set, the line
import { arc } from "d3-shape"; in .internal/core/render/Slice.js is still triggering the issue. Temporarily removing the line moves to the problem to the next import of d3-shape elsewhere in amCharts.

I've also tried with the latest versions of Jest and associated packages but the issue still persists. The example is using effectively the same as what's in https://www.amcharts.com/docs/v5/getting-started/integrations/jest/.

Environment (if applicable)

  • amCharts version: 5.7.4

Additional context

A simpler alternate solution to this issue may be to add "type": "module" to amCharts' package.json (e.g. #1265). Doing that allowed me to directly have Jest run, without any transformation or other effort required. A worked example with passing tests:

https://gist.github.com/davidjb/6c6492848dc924a9551b1e2d97f8a847

That said, that solution will solve things for folks who are using ESM for their testing, but still won't solve this overall issue for those writing tests in CJS.

@martynasma
Copy link
Collaborator

The fix to this is dependent on us finding the solution for #1265

Let's keep this open, until we are able to figure type: "module".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Feature request
Projects
None yet
Development

No branches or pull requests

2 participants