2016-11-11 38 views
1

我已经使用此结构创建了vue组件。单元测试Vue组件导轨应用程序

Vue.component('my-component', { 
    template: '<div>A custom component!</div>' 
}) 

我想对此进行测试使用因缘和茉莉或茉莉轨宝石。我无法弄清楚如何测试组件。在文档中的所有示例中,他们使用requirejs模块的测试方式。我使用创建组件的全局组件方式。

这些是来自文档的示例。

<template> 
    <span>{{ message }}</span> 
</template> 
<script> 
    export default { 
    data() { 
     return { 
     message: 'hello!' 
     } 
    }, 
    created() { 
     this.message = 'bye!' 
    } 
    } 
</script> 

// Import Vue and the component being tested 
import Vue from 'vue' 
import MyComponent from 'path/to/MyComponent.vue' 

// Here are some Jasmine 2.0 tests, though you can 
// use any test runner/assertion library combo you prefer 

describe('MyComponent',() => { 
    // Inspect the raw component options 
    it('has a created hook',() => { 
    expect(typeof MyComponent.created).toBe('function') 
    }) 
    // Evaluate the results of functions in 
    // the raw component options 
    it('sets the correct default data',() => { 
    expect(typeof MyComponent.data).toBe('function') 
    const defaultData = MyComponent.data() 
    expect(defaultData.message).toBe('hello!') 
    }) 
    // Inspect the component instance on mount 
    it('correctly sets the message when created',() => { 
    const vm = new Vue(MyComponent).$mount() 
    expect(vm.message).toBe('bye!') 
    }) 
    // Mount an instance and inspect the render output 
    it('renders the correct message',() => { 
    const Ctor = Vue.extend(MyComponent) 
    const vm = new Ctor().$mount() 
    expect(vm.$el.textContent).toBe('bye!') 
    }) 
}) 

回答

0

import foo from 'bar';var foo = require('bar');做同样的事情,使得可作为当前文件的变量foo。在测试示例中,MyComponent是导入的vue组件实例,也可以在当前文件中使用​​实现。所以,如果你的测试是在同一个文件,只需要使用MyComponent变量,如果没有,你需要导出MyComponent先用module.exports = MyComponentexport default MyComponent。这些出口假设MyComponent是要导出的唯一的事情,如果你要导出多个变量,检出文档:http://wiki.commonjs.org/wiki/Modules/1.1https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/export