You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

迁移Jest至Vitest时遭遇@mui/icons-material ES模块兼容问题

Jest迁移Vitest时@mui/icons-material模块兼容问题

我正在将测试套件从Jest迁移至Vitest,大部分测试正常运行,但仍有少数测试报错,错误信息如下:

Module /home/d/code/frontend/node_modules/@mui/icons-material/esm/VisibilityOff.js:3 seems to be an ES Module but shipped in a CommonJS package. You might want to create an issue to the package "@mui/icons-material" asking them to ship the file in .mjs extension or add "type": "module" in their package.json.

As a temporary workaround you can try to inline the package by updating your config:

// vitest.config.js
export default {
  test: {
    server: {
      deps: {
        inline: [
          "@mui/icons-material"
        ]
      }
    }
  }
}

我尝试了上述临时解决方案,但没有效果。还尝试了以下几种配置:

尝试1:配置resolve别名(匹配具体图标路径)

resolve: {
alias: [
  { find: '/@src', replacement: './src' },
  {
    find: /^@mui\/icons-material\/(.*)/,
    replacement: '@mui/icons-material/esm/$1',
  },
],
},

尝试2:添加optimizeDeps配置

optimizeDeps: {
  include: ['@mui/icons-material'],
},

尝试3:配置resolve别名(直接指向esm目录)

resolve: {
    alias: {
      "@mui/icons-material": "@mui/icons-material/esm",
    },
  },

尝试4:修改Rollup外部依赖配置

const baseRollupOptions: RollupOptions = {
   external: ['fs/promises', '@mui/icons-material'],
}

尝试5:配置deps优化器与外部依赖

deps: {
    optimizer: {
      web: {
        include: [
          'react-cookie',
          'redux-signalr',
          '@mui/x-charts',
          '@babel/runtime',
          'd3-color',
          'd3-format',
          'd3-interpolate',
          'd3-scale',
          'd3-shape',
          'd3-time',
          'd3-time-format',
          'd3-path',
          'd3-array',
          'internmap',
          '@mui/icons-material',
        ],
        exclude: [
          'node_modules/(?!(@mui/icons-material|react-cookie|redux-signalr|@mui/x-charts|@babel/runtime|d3-(color|format|interpolate|scale|shape|time|time-format|path|array)|internmap)/)',
        ],
      },
    },
    external: [
      'node_modules/(?!(react-cookie|redux-signalr|@mui/x-charts|@babel/runtime|d3-(color|format|interpolate|scale|shape|time|time-format|path|array)|internmap)/)',
    ],
  },

目前所有方案均无效。奇怪的是,失败的测试似乎并未使用该MUI图标,而其他使用该图标的测试却能正常运行。我每次运行测试前都会清理缓存:rm -rf node_modules/.vite。

内容的提问来源于stack exchange,提问作者Damian Green

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 08:57:17