react-scheduler icon indicating copy to clipboard operation
react-scheduler copied to clipboard

Error importing ES modules while running Vitest

Open chauphongnguyen opened this issue 10 months ago • 1 comments

Hi,

I was using @mui/material v5, and Vitest was working fine. As I need to display a calendar view, I use react-scheduler and need to upgrade to v6 (v6.4.6). Now, despite vite start running normally, all unit tests are broken. I would greatly appreciate any support with this matter.

Please see the message below.

FAIL  src/components/ListSchedules/ListSchedules.test.tsx [ src/components/ListSchedules/ListSchedules.test.tsx ]

Error: Directory import '/home/project/node_modules/@mui/material/styles' is not supported resolving ES modules imported from /home/project/node_modules/@aldabil/react-scheduler/index.js
Did you mean to import "@mui/material/node/styles/index.js"?

This is my vite config:

/// <reference types="vitest" />
import react from "@vitejs/plugin-react";
import { defineConfig, loadEnv } from "vite";
import type { ConfigEnv, UserConfig } from "vite";
import topLevelAwait from "vite-plugin-top-level-await";

// Workaround for top-level await in Vite

// https://vitejs.dev/config/
export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
  process.env = { ...process.env, ...loadEnv(mode, process.cwd(), "") };

  return {
    optimizeDeps: {
      esbuildOptions: {
        target: "esnext",
        supported: {
          "top-level-await": true, //browsers can handle top-level-await features
        },
      },
    },
    build: {
      target: "esnext", //browsers can handle the latest ES features
    },
    plugins: [
      react(),
      {
        name: "vite-config",
        config: () => ({
          define: {
            "process.env": process.env,
          },
        }),
      },
      topLevelAwait({
        // The export name of top-level await promise for each chunk module
        promiseExportName: "__tla",
        // The function to generate import names of top-level await promise in each chunk module
        promiseImportName: i => `__tla_${i}`,
      }) as Plugin,
    ],
    test: {
      environment: "jsdom",
      globals: true,
      css: {
        include: /.*/,
      },
      include: ["src/**/*.test.tsx", "src/**/*.test.ts"],
      deps: {
        web: {
          transformCss: true,
        },
      },
      logHeapUsage: true,
      coverage: {
        provider: "v8",
      },
      maxConcurrency: 1,
    },
  };
});

chauphongnguyen avatar Mar 07 '25 08:03 chauphongnguyen

Is this related? https://github.com/mui/material-ui/issues/35773

aldabil21 avatar Mar 07 '25 22:03 aldabil21

Thank you for your response!

I have looked into this issue, as well as other related problems with ESM in the mui/material-ui repository, but none of the solutions have worked. I also tried upgrading mui/material-ui to version 7.

I only use the Scheduler component in ListSchedules, but unit tests for 20 other components failed. However, when I comment out the Scheduler import, all tests pass.

Additionally, I do not have "type": "module" in my package.json.

chauphongnguyen avatar Mar 10 '25 02:03 chauphongnguyen

Upgrade @mui/material@next and @mui/x-date-pickers@next will resolve loading @mui/material/styles from an ES module. Recommended in mui/material-ui#42544

However, they renamed AdapterDateFnsV3 in mui/mui-x#16082

@aldabil21 Could you please upgrade the version of mui packages, please? Thank you!

chauphongnguyen avatar Mar 12 '25 05:03 chauphongnguyen

Upgrade to v7 on next version

aldabil21 avatar Apr 16 '25 17:04 aldabil21