mui-x icon indicating copy to clipboard operation
mui-x copied to clipboard

[charts][ESM] `@mui/x-charts` does not work with jest

Open Notalifeform opened this issue 1 year ago • 12 comments

The problem in depth 🔍

Link to example: https://github.com/rocsys/mui-x-charts-jest-issue-minimal-reproduction/

Steps:

  1. run yarn test --watchAll=false

jest will fail with

 FAIL  src/App.test.tsx
  ● Test suite failed to run

    Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:

    /Users/rbakker/work/rocsys/code/t/test-mui-chart/node_modules/d3-scale/src/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export {
                                                                                      ^^^^^^

    SyntaxError: Unexpected token 'export'

    > 1 | import { ChartContainer, BarPlot } from '@mui/x-charts';
        | ^
      2 |
      3 | const uData = [4000, 3000, 2000, 2780, 1890, 2390, 3490];
      4 | const xLabels = [

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
      at Object.<anonymous> (node_modules/@mui/x-charts/context/CartesianContextProvider.js:11:16)
      at Object.<anonymous> (node_modules/@mui/x-charts/context/index.js:18:33)
      at Object.<anonymous> (node_modules/@mui/x-charts/index.js:24:16)
      at Object.<anonymous> (src/TinyBarChart.tsx:1:1)
      at Object.<anonymous> (src/App.tsx:4:1)
      at Object.<anonymous> (src/App.test.tsx:3:1)
      at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
      at runJest (node_modules/@jest/core/build/runJest.js:404:19)
      at _run10000 (node_modules/@jest/core/build/cli/index.js:320:7)
      at runCLI (node_modules/@jest/core/build/cli/index.js:173:3)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.677 s

I attempted to transpile the mui/x-chart module, but it does not seem to work?

https://github.com/rocsys/mui-x-charts-jest-issue-minimal-reproduction/tree/feature/attempt-to-transpile

our project uses without any issue:

   "@mui/icons-material": "^5.14.18",
    "@mui/material": "^5.14.18",
    "@mui/x-data-grid-pro": "^6.18.1",
    "@mui/x-date-pickers-pro": "^6.18.1",
    "@mui/x-license-pro": "^6.10.2",

but jest breaks when adding

    "@mui/x-charts": "^6.18.4",

Your environment 🌎

npx @mui/envinfo
    System:
    OS: macOS 14.0
  Binaries:
    Node: 18.16.1 - ~/.nvm/versions/node/v18.16.1/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v18.16.1/bin/yarn
    npm: 9.7.2 - /opt/homebrew/bin/npm
  Browsers:
    Chrome: 120.0.6099.129
    Edge: Not Found
    Safari: 17.0
  npmPackages:
    @emotion/react: ^11.11.3 => 11.11.3 
    @emotion/styled: ^11.11.0 => 11.11.0 
    @mui/base:  5.0.0-beta.29 
    @mui/core-downloads-tracker:  5.15.2 
    @mui/material: ^5.15.2 => 5.15.2 
    @mui/private-theming:  5.15.2 
    @mui/styled-engine:  5.15.2 
    @mui/styles: ^5.15.2 => 5.15.2 
    @mui/system:  5.15.2 
    @mui/types:  7.2.11 
    @mui/utils:  5.15.2 
    @mui/x-charts: ^6.18.4 => 6.18.4 
    @types/react: ^18.0.0 => 18.2.46 
    react: ^18.2.0 => 18.2.0 
    react-dom: ^18.2.0 => 18.2.0 
    typescript: ^4.4.2 => 4.9.5 

Search keywords: x-charts jest Order ID: 70120

Notalifeform avatar Jan 03 '24 19:01 Notalifeform