首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel-Vue单元测试错误"SyntaxError:意外的标记{“

Laravel-Vue单元测试错误"SyntaxError:意外的标记{“
EN

Stack Overflow用户
提问于 2020-06-21 03:42:59
回答 1查看 180关注 0票数 1

我正在尝试为Vue编写单元测试,设置在Laravel环境中。我跟随this article设置了用于测试的项目。但它并没有像预期的那样工作,当我运行npm test时,我得到了这个错误。

代码语言:javascript
复制
> @ 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

代码语言:javascript
复制
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!');
  });
});

App.vue

代码语言:javascript
复制
<template>
  <div>
      <h1>
          {{ msg }}
      </h1>
  </div>
</template>

<script>
export default {
    name: 'app-component',
    data(){
        return {
            msg : "Hello Vue!"
        }
    }
}
</script>

Git集线器存储库

repository包含完整的代码。

EN

回答 1

Stack Overflow用户

发布于 2021-03-11 13:52:34

当我将测试代码提取到一个单独的文件中时,这个问题得到了修复。

setup.js

代码语言:javascript
复制
require('jsdom-global')();

test.spec.js

代码语言:javascript
复制
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!');
  });
});

package.json

代码语言:javascript
复制
{
   "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"
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62490846

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档