我有一个Vue.js应用程序,其中有两个文件包含:
import axios from "axios"
这些文件位于应用程序的src/lib中,第一行包含import语句。
无论package.json怎么说,在Github上运行测试都会导致Axios 1.0.0被安装,现在任何涉及这些文件的测试都会失败,并出现上述错误。
将语句更改为const axios = require("axios")
也失败;node_modules/axios/index.js在第1行包含一条import语句,并在那里引发异常。
对于此类问题,我经常看到的一个建议是将"type": "module"
添加到package.json中(与src/处于同一级别(。这导致所有测试都失败,并要求将vue.config.js重命名为vue.config.cjs。这样做会让我:Error: You appear to be using a native ECMAScript module configuration file, which is only supported when running Babel asynchronously
,我不理解。
有人能建议在这里做什么吗?
我通过添加来强制jest导入commonjs axios构建,从而修复了这个错误
"jest": {
"moduleNameMapper": {
"axios": "axios/dist/node/axios.cjs"
}
},
到我的CCD_ 5。使用transformIgnorePatterns
的其他解决方案对我不起作用。
axios index.js文件的0.x.x版本
module.exports = require('./lib/axios');
axiox index.js文件的1.x.x版本
import axios from './lib/axios.js';
export default axios;
基本上,jest运行在Node.js环境中,所以它使用了遵循CommonJS的模块。如果您想使用高达1.x.x的axios,则必须将JavaScript模块从ECMAScript类型转换为CommonJS类型。Jest基本上忽略了/node_modules/
目录的转换。
- https://jestjs.io/docs/27.x/configuration#transformignorepatterns-阵列
所以您必须覆盖transformIgnorePatterns
选项。有两种方法可以覆盖transformIgnorePatterns
选项。
jest.config.js
如果您的vue项目使用jest.config.js
文件,则添加此选项。
module.exports = {
preset: "@vue/cli-plugin-unit-jest",
transformIgnorePatterns: ["node_modules/(?!axios)"],
...other options
};
package.json
如果您的vue项目将package.json
文件用于jest,则添加此选项。
{
...other options
"jest": {
"preset": "@vue/cli-plugin-unit-jest",
"transformIgnorePatterns": ["node_modules/(?!axios)"]
}
}
这个正则表达式可以帮助您转换axios模块,并忽略node_modules目录下的其他模块。
- https://regexper.com/#node_modules%5C%2F%28%3F!axios%29
将jest
版本更新到v29在我的项目中修复了这一问题。可能是您有一个不兼容的jest
版本。
快速修复
从更新npm运行测试脚本
"test": "react-scripts test",
至
"test": "react-scripts test --transformIgnorePatterns "node_modules/(?!axios)/"",
这对我有效:
在jest.config.js中添加moduleNameMapper
moduleNameMapper: {
axios: 'axios/dist/node/axios.cjs',
},
我遇到了同样的问题,并且能够通过使用jest-mock-axios库来解决这个问题
使用NestJs如果您面临此问题,请使用以下包版本升级您的package.json。
"jest": "^29.0.0"
"ts-jest": "^29.0.0"
在我的例子中,我不得不在jest-config中向moduleNameMapper对象添加以下行:
axios: '<rootDir>/node_modules/axios/dist/node/axios.cjs',
我遇到了类似的问题,但错误是由jest
引起的。所有尝试导入axios
的测试都失败,并抛出相同的异常:
Test suite failed to run
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/en/ecmascript-modules for how to enable it.
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html
Details:
/monorepo/node_modules/axios/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import axios from './lib/axios.js';
^^^^^^
SyntaxError: Cannot use import statement outside a module
1 | import { describe, expect, it } from '@jest/globals'
> 2 | import axios from 'axios'
解决方案是简单地告诉jest
axios
应该用babel
:进行转换
const esModules = ['lodash-es', 'axios'].join('|')
# add these entries in module.exports
transform: {
[`^(${esModules}).+\.js$`]: 'babel-jest',
},
transformIgnorePatterns: [`node_modules/(?!(${esModules}))`],
注意:我使用的是Quasar Vue,这是它们的实现。
我也遇到了同样的问题。首先使用的变体(在package.json文件中(:
"jest": {
"moduleNameMapper": {
"axios": "axios/dist/node/axios.cjs"
}
},
但对于mock,我使用axios mock适配器。我得到了错误:
类型错误:mock.onNet不是一个函数
我将代码更改为:
"jest": {
"transformIgnorePatterns": [
"/node_modules/(?!(axios)/)"
]
}
这个解决方案是有效的。版本:
";axios":"1.4.0";
"axios模拟适配器":"1.21.5〃;
"types/axios模拟适配器":"1.10.0〃;
作为使用Jest和Typescript时的另一种方法,您可以模拟测试所需的各个函数:
import axios from 'axios'
jest.mock('axios', () => ({
post: jest.fn(),
get: jest.fn()
}))
const mockedAxios = axios as jest.Mocked<typeof axios>
mockedAxios.post.mockResolvedValue({})
尽管这种方法对我有效,但让babel转换axios也起到了作用:
// jest.config.js
module.exports = {
transformIgnorePatterns: [
'/node_modules/(?!(axios)/).*'
],
}
对于Axios,请将以下代码添加到您的package.json中;
"jest": {
"transformIgnorePatterns": [
"/node_modules/(?!(axios)/)"
]
}
我也遇到了同样的问题,当将axios更改为fetch时,它运行良好。
axios(失败(
try {
const response = await axios("api/fruit/all");
return response.data;
} catch (error) {
return error;
}
提取(工作正常(
try {
const response = await fetch("api/fruit/all",{method:"GET"});
const data = await response.json();
return data;
} catch (error) {
return error;
}