x icon indicating copy to clipboard operation
x copied to clipboard

在next15.2.1中的jest引入Bubble报错

Open solid-component opened this issue 9 months ago • 3 comments

antd版本:5.24.3 "@ant-design/x": "^1.0.6" 包管理:pnpm

  1. 根据https://nextjs.org/docs/app/building-your-application/testing/jest的配置好jest环境后,

MyComponent.tsx

import { Bubble } from "@ant-design/x";

export function MyComponent() {
  return <Bubble />
}

demo.test.tsx

import { MyComponent } from "MyComponent";

console.log(MyCompoent)

结果

报错: /node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/antd/es/theme/useToken.js:9 import React from 'react'; ^^^^^^

SyntaxError: Cannot use import statement outside a module

solid-component avatar Mar 24 '25 13:03 solid-component

这个错误通常是由于 Jest 无法正确处理 ES 模块的导入语句造成的。以下是一些可能的解决方案:

  1. 检查 Jest 配置: 确保你的 Jest 配置中包含了对 ES 模块的支持。你可以在 jest.config.js 中添加以下配置:

    module.exports = {
      transform: {
        '^.+\\.(js|jsx|ts|tsx)$': 'babel-jest',
      },
      moduleNameMapper: {
        '\\.(css|less)$': 'identity-obj-proxy',
      },
    };
    
  2. 使用 Babel: 确保你已经安装并配置了 Babel,以便 Jest 可以正确地转换 ES 模块。你需要安装 babel-jest 和相关的 Babel 插件。

    npm install --save-dev babel-jest @babel/core @babel/preset-env @babel/preset-react
    

    然后在项目根目录下创建一个 .babelrc 文件,内容如下:

    {
      "presets": ["@babel/preset-env", "@babel/preset-react"]
    }
    
  3. 检查 Node 版本: 确保你的 Node.js 版本支持 ES 模块。如果不支持,可以尝试升级 Node.js 或使用 Babel 进行转换。

  4. 调整 package.json: 确保你的 package.json 中的 type 字段设置为 module,以便 Node.js 识别 ES 模块。

如果以上方法仍然无法解决问题,请提供更多的上下文信息或错误日志,以便我能更好地帮助你。

🪧 Tips For further assistance, please describe your question in the comments and @petercat-assistant to start a conversation with me.

petercat-assistant[bot] avatar Mar 24 '25 13:03 petercat-assistant[bot]

这个错误通常是由于 Jest 无法正确处理 ES 模块的导入语句造成的。以下是一些可能的解决方案:

  1. 检查 Jest 配置: 确保你的 Jest 配置中包含了对 ES 模块的支持。你可以在 jest.config.js 中添加以下配置: module.exports = { transform: { '^.+\.(js|jsx|ts|tsx)$': 'babel-jest', }, moduleNameMapper: { '\.(css|less)$': 'identity-obj-proxy', }, };

  2. 使用 Babel: 确保你已经安装并配置了 Babel,以便 Jest 可以正确地转换 ES 模块。你需要安装 babel-jest 和相关的 Babel 插件。 npm install --save-dev babel-jest @babel/core @babel/preset-env @babel/preset-react

    然后在项目根目录下创建一个 .babelrc 文件,内容如下: { "presets": ["@babel/preset-env", "@babel/preset-react"] }

  3. 检查 Node 版本: 确保你的 Node.js 版本支持 ES 模块。如果不支持,可以尝试升级 Node.js 或使用 Babel 进行转换。

  4. 调整 package.json: 确保你的 package.json 中的 type 字段设置为 module,以便 Node.js 识别 ES 模块。

如果以上方法仍然无法解决问题,请提供更多的上下文信息或错误日志,以便我能更好地帮助你。

🪧 Tips

node版本:23.7.0,都加上了,貌似没用

solid-component avatar Mar 24 '25 13:03 solid-component

同样的配置, antd5是正常的

solid-component avatar Mar 24 '25 13:03 solid-component