语法错误:无法在模块 { AXIOS } 外部使用 import 语句

1 vue.js axios vue-testing-library

您好,我将代码从 fetch 更改为 axios,当我运行测试时,我遇到了这个问题...任何人都可以帮助我吗?

\n
\n

语法错误:无法在模块外部使用 import 语句

\n
> 1 | import axios from "axios";\n
Run Code Online (Sandbox Code Playgroud)\n
\n

Jest 遇到意外令牌

\n
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.\n\nBy default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".\n\nHere's what you can do:\n \xe2\x80\xa2 If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/en/ecmascript-modules for how to enable it.\n \xe2\x80\xa2 To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.\n \xe2\x80\xa2 If you need a custom transformation specify a "transform" option in your config.\n \xe2\x80\xa2 If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.\n\nYou'll find more details and examples of these config options in the docs:\n
Run Code Online (Sandbox Code Playgroud)\n

Spe*_*ord 5

对我来说帮助了以下jest.config.js设置:

transformIgnorePattern: [
 '<rootDir>/node_modules/(?!axios)/'
]
Run Code Online (Sandbox Code Playgroud)

与...一起

moduleNameMapper: {
  '^axios$': require.resolve('axios'),
}
Run Code Online (Sandbox Code Playgroud)