jest icon indicating copy to clipboard operation
jest copied to clipboard

[Bug]: jest can not transform antd/es

Open singsad opened this issue 3 years ago • 7 comments

Version

24.1.0

Steps to reproduce

my jest config

transformIgnorePatterns: ['!/node_modules/','/dist/'], transform: { '\.[jt]sx?$': 'babel-jest', }, my package.json

"jest": "^24.1.0",
"@babel/preset-env": "^7.0.0",
"@babel/preset-react": "^7.0.0",
"@babel/preset-typescript": "^7.8.3",
"babel-jest": "28.1.1",

When i test antd components, The test suite was failed

/Users/xxx/Desktop/gitlab/fbs-frontend/node_modules/antd/es/select/style/index.js:1 ({"Object.":function(module,exports,require,__dirname,__filename,global,jest){import '../../style/default.less'; SyntaxError: Cannot use import statement outside a module

Expected behavior

antd/es can be transform

Actual behavior

/Users/xxx/Desktop/gitlab/fbs-frontend/node_modules/antd/es/select/style/index.js:1 ({"Object.":function(module,exports,require,__dirname,__filename,global,jest){import '../../style/default.less'; SyntaxError: Cannot use import statement outside a module

Additional context

babel.config.js 'plugins': [ [ 'import', { 'libraryName': 'antd', 'libraryDirectory': 'es', 'style': true, }, ], ], 'presets': [ '@babel/preset-env', '@babel/preset-react', [ '@babel/preset-typescript', { 'isTSX': true, 'allExtensions': true, }, ], ],

When i use antd by config 'libraryDirectory': 'es'. My jest reported an error. But when i use this config 'libraryDirectory': 'lib'. My jest will work. I don't think babel-jest treats the esmodule of antd as commonjs.

Environment

System:
OS: macOS 11.6.4
Binaries:
Node: 16.14.2 - ~/.nvm/versions/node/v16.14.2/bin/node
Yarn: 1.22.10 - /usr/local/bin/yarn
npm: 8.5.0 - ~/.nvm/versions/node/v16.14.2/bin/npm
npmPackages:
@babel/cli: ^7.2.3 => 7.14.8
@babel/core: ^7.3.4 => 7.15.0
@babel/plugin-proposal-class-properties: ^7.0.0 => 7.14.5
@babel/plugin-proposal-decorators: ^7.0.0 => 7.14.5
@babel/plugin-proposal-object-rest-spread: ^7.0.0 => 7.14.7
@babel/plugin-proposal-optional-chaining: ^7.9.0 => 7.14.5
@babel/plugin-proposal-private-methods: ^7.16.0 => 7.16.0
@babel/plugin-proposal-private-property-in-object: ^7.16.0 => 7.16.0
@babel/plugin-transform-runtime: ^7.0.0 => 7.15.0
@babel/polyfill: ^7.2.5 => 7.12.1
@babel/preset-env: ^7.0.0 => 7.15.0
@babel/preset-react: ^7.0.0 => 7.14.5
@babel/preset-typescript: ^7.8.3 => 7.15.0
@babel/runtime: ^7.0.0 => 7.15.3
babel-jest: 28.1.1 => 28.1.1
babel-loader: ^8.2.3 => 8.2.3
babel-plugin-import: ^1.11.0 => 1.13.3
babel-plugin-lodash: ^3.3.4 => 3.3.4
babel-plugin-react-require: ^3.0.0 => 3.1.3
babel-plugin-react-transform: ^3.0.0 => 3.0.0
eslint: ^7.11.0 => 7.32.0
jest: ^24.1.0 => 24.9.0
webpack: ^5.65.0 => 5.65.0

singsad avatar Jun 13 '22 02:06 singsad

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Jul 13 '22 03:07 github-actions[bot]

i have the same problem

/code/ops-console/node_modules/antd/es/card/style/index.js:1 ({"Object.":function(module,exports,require,__dirname,__filename,jest){import '../../style/default.less'; ^^^^^^

SyntaxError: Cannot use import statement outside a module

ycshill avatar Jul 27 '22 02:07 ycshill

都是国人我直接说中文了,我已经解决了,如果你有需要可以交流一下

singsad avatar Jul 27 '22 02:07 singsad

都是国人我直接说中文了,我已经解决了,如果你有需要可以交流一下

求一个解决方法

zxiaohong avatar Jul 27 '22 03:07 zxiaohong

你的问题跟我一样吗? 环境, 依赖,版本等

singsad avatar Jul 27 '22 03:07 singsad

你的问题跟我一样吗? 环境, 依赖,版本等

这些不太一样,但是问题是一样的,你是怎么解决的呢

ycshill avatar Jul 27 '22 05:07 ycshill

单元测试配置跑单测的时候不去找es模块 找lib模块就可以

singsad avatar Jul 27 '22 06:07 singsad

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Aug 26 '22 06:08 github-actions[bot]

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

github-actions[bot] avatar Sep 25 '22 06:09 github-actions[bot]

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

github-actions[bot] avatar Sep 25 '22 06:09 github-actions[bot]

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

github-actions[bot] avatar Oct 26 '22 00:10 github-actions[bot]