使用 Vueify 使用 VueJS 单文件组件运行 Karma 测试



我正在尝试用Karma运行我的单元测试。我有calendar.tests.js文件看起来很像这样:

import { handleSelectDay } from '../components/Calendar/index.vue'
describe('Calendar', () => {
  describe('handleSelectDay', () => {
    const data = {};
    describe('updates data', () => {
      it('should set the selectedDay to a new id', () => {
        handleSelectDay('id');
        expect(data.daySelected).to.equal('id');
      });
    });
  });
});

当我使用 Karma 运行此测试时,我得到以下结果:TypeError: (0 , _index.handleSelectDay) is not a function is not a function

我的 karma.conf.js 看起来像:

module.exports = function (config) {
  config.set({
    frameworks: ['browserify', 'mocha'],
    files: ['static/js/apps/FutureHours/test/calender.tests.js'],
    preprocessors: {
      'static/js/apps/**/test/*.js': ['browserify']
    },
    browsers: ['Chrome'],
    logLevel: 'LOG_DEBUG',
    plugins: [
      'karma-mocha',
      'karma-browserify',
      'karma-chrome-launcher',
      'karma-spec-reporter'
    ],
    browserify: {
      debug: true,
      transform: [['babelify', { presets: ['env'] }], 'vueify']
    }
  })
}

我怎样才能让 Karma 很好地配合 VueJS 单文件组件?

这样做的问题是您不能从.vue组件进行命名导出。相反,组件中使用的任何方法都必须通过单元测试中的组件对象进行访问。任何在组件methods之外使用的函数,都应该存在于它们自己的ES模块中,以使单元测试更容易。

最新更新