Javascript Laravel Vue单元测试错误“;SyntaxError:意外标记{";
我正在尝试为Vue编写单元测试,设置在Laravel环境中。我随后设置了项目进行测试。但它没有按预期工作,我在运行Javascript Laravel Vue单元测试错误“;SyntaxError:意外标记{";,javascript,laravel,unit-testing,vue.js,vuejs2,Javascript,Laravel,Unit Testing,Vue.js,Vuejs2,我正在尝试为Vue编写单元测试,设置在Laravel环境中。我随后设置了项目进行测试。但它没有按预期工作,我在运行npm test时遇到此错误 > @ test G:\Unit testing\Vue-Laravel > mochapack --webpack-config=node_modules/laravel-mix/setup/webpack.config.js --require tests/js/setup.js tests/js/\*\*/\*.spec.js (no
npm test
时遇到此错误
> @ test G:\Unit testing\Vue-Laravel
> mochapack --webpack-config=node_modules/laravel-mix/setup/webpack.config.js --require tests/js/setup.js tests/js/\*\*/\*.spec.js
(node:3224) UnhandledPromiseRejectionWarning: G:\Unit testing\Vue-Laravel\tests\js\setup.js:3
import { mount } from '@vue/test-utils';
^
SyntaxError: Unexpected token {
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at exports.requireOrImport (G:\Unit testing\Vue-Laravel\node_modules\mocha\lib\esm-utils.js:20:12)
at Object.exports.handleRequires (G:\Unit testing\Vue-Laravel\node_modules\mocha\lib\cli\run-helpers.js:94:34)
at mochaChecks (G:\Unit testing\Vue-Laravel\node_modules\mochapack\lib\cli\argsParser\parseArgv\mocha\parseMochaArgs.js:60:19)
(node:3224) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
WEBPACK Compiling...
[=========================] 98% (after emitting)
DONE Compiled successfully in 198ms 12:25:43 AM
[=========================] 100% (completed)
WEBPACK Compiled successfully in 198ms
MOCHA Testing...
0 passing (0ms)
MOCHA Tests completed successfully"
setup.js
App.vue
{{msg}}
导出默认值{
名称:“应用程序组件”,
数据(){
返回{
味精:“你好,Vue!”
}
}
}
Git中心存储库
这包含了整个代码。当我将测试代码提取到一个单独的文件中时,这个问题得到了解决 setup.js
require('jsdom-global')();
test.spec.js
从'@vue/test-utils'导入{mount};
从“expect”导入expect;
从“../../../resources/js/App.vue”导入应用程序;
描述('App.vue',()=>{
它('说你好,Vue!',()=>{
常量包装=装载(应用程序);
expect(wrapper.html()).toContain('hellovue!');
});
});
package.json
{
“测试:单元”:“跨环境节点_env=测试mochapack--webpack config=NODE_modules/laravel mix/setup/webpack.config.js--需要测试/Vue/unit/setup.js tests/Vue/unit/**.spec.js”
}
看看这是否对您有帮助
require('jsdom-global')();
import { mount } from '@vue/test-utils';
import expect from 'expect';
import App from '../../../resources/js/App.vue';
describe('App.vue', () => {
it('says Hello Vue!', () => {
const wrapper = mount(App);
expect(wrapper.html()).toContain('Hello Vue!');
});
});
<template>
<div>
<h1>
{{ msg }}
</h1>
</div>
</template>
<script>
export default {
name: 'app-component',
data(){
return {
msg : "Hello Vue!"
}
}
}
</script>