【发布时间】:2019-09-18 07:19:44
【问题描述】:
我构建了一个简单的 Vue 组件来包装 Trix 编辑器。我正在尝试为其编写测试,但 Trix 似乎没有正确安装,并且没有像在浏览器中那样生成工具栏元素。我正在使用 Jest 测试运行器。
TrixEdit.vue
<template>
<div ref="trix">
<trix-editor></trix-editor>
</div>
</template>
<script>
import 'trix'
export default {
mounted() {
let el = this.$refs.trix.getElementsByTagName('trix-editor')[0]
// HACK: change the URL field in the link dialog to allow non-urls
let toolbar = this.$refs.trix.getElementsByTagName('trix-toolbar')[0]
toolbar.querySelector('[type=url]').type = 'text'
// insert content
el.editor.insertHTML(this.value)
el.addEventListener('trix-change', e => {
this.$emit('input', e.target.innerHTML)
})
}
}
</script>
TrixEdit.spec.js
import { mount, shallowMount, createLocalVue } from '@vue/test-utils'
import TrixEdit from '@/components/TrixEdit.vue'
const localVue = createLocalVue()
localVue.config.ignoredElements = ['trix-editor']
describe('TrixEdit', () => {
describe('value prop', () => {
it('renders text when value is set', () => {
const wrapper = mount(TrixEdit, {
localVue,
propsData: {
value: 'This is a test'
}
})
expect(wrapper.emitted().input).toEqual('This is a test')
})
})
})
expect() 失败并出现以下错误
Expected value to equal:
"This is a test"
Received:
undefined
at Object.toEqual (tests/unit/TrixEdit.spec.js:19:39)
为什么 Trix 在我的测试中没有初始化?
【问题讨论】:
-
你能分享有问题的回购吗?
-
@hackape 不幸的是它是私人的。
-
mocha 单元测试在 nodejs 运行时运行,而
trix仅在浏览器运行时运行。这就是它无法在单元测试中初始化的原因。 -
@hackape 这是开玩笑,不是摩卡。
-
好吧,mocha 还是 jest,问题都一样,运行时的区别
标签: unit-testing vue.js vuejs2 jestjs vue-component