我遇到了一些问题,试图弄清楚为什么我非常简单的测试用例总是失败.我正在努力学习Vue中的测试是如何工作的,并且有一个非常简单的测试,其中element.focus()
是触发器onMount(() => /* see implementation ?*/)
.
以下是我的组件:
const myInputRef = ref<HTMLInputElement | null>(null);
onMounted(() => {
if (myInputRef.value) {
myInputRef.value.focus()
}
})
</script>
<template>
<input ref="myInputRef" name="my-input" type="text">
<label for="my-input">input:</label>
</template>
我的测试用例是:
describe('App', () => {
it('should auto focus input element', async () => {
const wrapper = mount(App);
const inputElement = wrapper.find('input');
expect(inputElement.exists()).toBeTruthy()
await nextTick()
expect(document.activeElement).toBe(inputElement.element)
})
})
结果是,expect(inputElement.exists()).toBeTruthy()
的判断结果很好,但
expect(document.activeElement).toBe(inputElement.element)
的结果是:
AssertionError: expected <body></body> to be <input name="my-input" …(1)></input> // Object.is equality
我有什么不明白的吗?