【发布时间】:2021-10-28 21:22:45
【问题描述】:
我正在使用 vue.js 并尝试使用 jest(with vue utils test) async/await
it('async/await test', async () => {
await wrapper.setData({
foo: 'bar',
});
// ...
});
我可以使用 import 语句,但是当我运行 'yarn test'(jest) 命令时会出现此错误 但我没有从测试文件中导出任何内容
Details:
export default function _asyncToGenerator(fn) {
^^^^^^
SyntaxError: Unexpected token 'export'
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1517:14)
at Object.<anonymous> (__test__/TestComponent.test.js:9:49)
这是我的配置文件
// jest.config.json
{
"verbose": true,
"testEnvironment": "node",
"moduleFileExtensions": ["js", "vue"],
"moduleNameMapper": {
"^@/(.*)$": "<rootDir>/resources/assets/js/components/$1"
},
"transform": {
"^.+\\.js$": "<rootDir>/node_modules/babel-jest",
".*\\.(vue)$": "<rootDir>/node_modules/vue-jest"
},
"snapshotSerializers": ["<rootDir>/node_modules/jest-serializer-vue"]
}
// babel.config.jss
module.exports = {
presets: [
'@vue/app',
['@babel/preset-env', { targets: { node: 'current' } }],
],
plugins: [
'@babel/plugin-proposal-nullish-coalescing-operator',
'@babel/plugin-proposal-optional-chaining',
'@babel/plugin-proposal-logical-assignment-operators',
'@babel/plugin-proposal-numeric-separator',
],
};
环境
System:
OS: Windows 10 10.0.19043
CPU: (8) x64 Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
Binaries:
Node: 14.15.4 - C:\Program Files\nodejs\node.EXE
Yarn: 1.22.10 - C:\Program Files\nodejs\yarn.CMD
npm: 6.14.10 - C:\Program Files\nodejs\npm.CMD
npmPackages:
jest: ^27.0.6 => 27.1.0
+
"@vue/test-utils": "^1.2.2"
"@babel/core": "^7.15.0"
"vue": "^2.6.10"
我该如何解决这个问题? 这是我的示例测试文件代码 https://gist.github.com/charmjeh/544455d49ff184750f17df8bb460b876
【问题讨论】:
-
我认为我们不应该从测试文件中导出任何内容。
-
在函数末尾尝试
export default _asyncToGenerator。 -
我没有导出任何东西,所以我不知道这个错误来自哪里:(这是我的代码gist.github.com/charmjeh/544455d49ff184750f17df8bb460b876
-
你能分享你的 TestComponent.test.js 文件吗?
-
gist.github.com/charmjeh/544455d49ff184750f17df8bb460b876 @arieljuod 这是我的 TestComponent.test.js 代码!
标签: javascript vue.js jestjs