今天用jest測試項目遇到坑了,錯誤資訊:
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import '../../style/index.less';
^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Unexpected string
原因是項目裡使用了AntDesign。而Jest 不支援 esm 子產品,Ant Design 使用它們。 為了使用 Jest 測試Ant Design 應用程式,必須将以下内容添加到 Jest 配置中:
//jest.config.js
module.exports = {
transform: {
'^.+\\.(ts|tsx|js|jsx)?$': 'ts-jest',
}
};