@ tes-6ren">
gpt4 book ai didi

javascript - Laravel-Vue 单元测试错误 "SyntaxError: Unexpected token {"

转载 作者:行者123 更新时间:2023-12-03 20:51:33 24 4
gpt4 key购买 nike

我正在尝试为 Vue 编写单元测试,并在 Laravel 环境中设置。我跟着 this article 设置了测试项目。但它没有按预期工作,并且在运行 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"
设置.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>
Git 中心存储库
这个 repository 包含整个代码。

最佳答案

当我将测试代码提取到一个单独的文件时,这个问题得到了解决。
设置.js

require('jsdom-global')();
测试规范.js
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!');
});
});
包.json
{
"test:unit": "cross-env NODE_ENV=testing mochapack --webpack-config=node_modules/laravel-mix/setup/webpack.config.js --require tests/Vue/Unit/setup.js tests/Vue/Unit/**/*.spec.js"
}

关于javascript - Laravel-Vue 单元测试错误 "SyntaxError: Unexpected token {",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62490846/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com