$ mount()上的Vuejs单元测试组件错误

时间:2017-04-25 19:38:40

标签: unit-testing vue.js vuejs2 karma-mocha

我正在尝试按照veujs文档设置的标准模式来安装组件,以便通过生命周期钩子在不同状态下访问其数据,但是,任何尝试在扩展后调用$ mount()函数具有Vue实例的组件会触发渲染错误,并在此后立即跳过套件中的断言。

执行来自包json的命令:

"cross-env BABEL_ENV=test karma start test/unit/karma.conf.js --single-run"

规范文件设置和挂载函数调用:

describe('component.vue', () => {
  let Ctor
  let vm

  beforeEach(() => {
    Ctor = Vue.extend(component)
    vm = new Ctor().$mount()
  })

npm run unit上的错误跟踪:

ERROR LOG: '[Vue warn]: Error in render function: 
(found in <Root>)'
ERROR LOG: TypeError{stack: 'render@http://localhost:9876/base/specs/component.spec.js?adb355a285fbdae55582a83df658adee4846d0e1:77038:32
_render@http://localhost:9876/base/specs/component.spec.js?adb355a285fbdae55582a83df658adee4846d0e1:10690:26
updateComponent@http://localhost:9876/base/specs/component.spec.js?adb355a285fbdae55582a83df658adee4846d0e1:9239:28
get@http://localhost:9876/base/specs/component.spec.js?adb355a285fbdae55582a83df658adee4846d0e1:9550:29
Watcher@http://localhost:9876/base/specs/component.spec.js?adb355a285fbdae55582a83df658adee4846d0e1:9533:15
mountComponent@http://localhost:9876/base/specs/component.spec.js?adb355a285fbdae55582a83df658adee4846d0e1:9243:28
$mount@http://localhost:9876/base/specs/component.spec.js?adb355a285fbdae55582a83df658adee4846d0e1:14333:24
$mount@http://localhost:9876/base/specs/component.spec.js?adb355a285fbdae55582a83df658adee4846d0e1:16382:20
http://localhost:9876/base/specs/component.spec.js?adb355a285fbdae55582a83df658adee4846d0e1:46421:27

karma配置文件:

module.exports = function (config) {
  config.set({
    // to run in additional browsers:
    // 1. install corresponding karma launcher
    //    http://karma-runner.github.io/0.13/config/browsers.html
    // 2. add it to the `browsers` array below.
    browsers: ['PhantomJS'],
    frameworks: ['mocha', 'sinon-chai'],
    // chai config
    client: {
      chai: {
        includeStack: true
      }
    },
    reporters: ['spec', 'coverage'],
    files: [
      '../../node_modules/babel-polyfill/dist/polyfill.js',
      'specs/*.js'
    ],
    preprocessors: {
      'specs/*.js': ['webpack', 'sourcemap']
    },
    webpack: webpackConfig,
    webpackMiddleware: {
      noInfo: true,
    },
    coverageReporter: {
      dir: './coverage',
      reporters: [
        { type: 'lcov', subdir: '.' },
        { type: 'text-summary' },
      ]
    },
  });
};

感谢您提前提供任何帮助。

0 个答案:

没有答案