amcharts / amcharts5

The newest, fastest, and most advanced amCharts charting library for JavaScript and TypeScript apps.
Other
353 stars 94 forks source link

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

Open davidjb opened 10 months ago

davidjb commented 10 months ago

Bug description

I've encountered the same problem as https://github.com/amcharts/amcharts5/issues/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)

Additional context

A simpler alternate solution to this issue may be to add "type": "module" to amCharts' package.json (e.g. https://github.com/amcharts/amcharts5/issues/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 commented 10 months ago

The fix to this is dependent on us finding the solution for https://github.com/amcharts/amcharts5/issues/1265

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