gpt4 book ai didi

javascript - 如何避免自定义指令的[Vue warn]?

转载 作者:行者123 更新时间:2023-12-02 21:01:21 24 4
gpt4 key购买 nike

我创建了一个自定义指令,它运行良好,但是当我对使用此自定义指令的组件运行 Mocha 测试时,我收到此警告消息 [Vue warn]: 无法解析指令:滚动文本,请告诉我如何解决这个问题

测试文件:

import { shallowMount } from "@vue/test-utils"
import { scrollText } from "z-common/services"
import ZSourcesList from "./ZSourcesList"

Vue.use(scrollText)

const stubs = [
"z-text-field",
"v-progress-circular",
"v-icon",
"z-btn"
]

describe("ZSourcesList.vue", () => {
const sources = []
for (let i = 0; i < 20; i++) {
sources.push({
field: "source",
// format numbers to get 2 diggit number with leading zero 1 -> 01
value: `cluster-${i.toLocaleString('en-US', { minimumIntegerDigits: 2, useGrouping: false })}`,
__typename: "SuggestV2Result"
})
}

it("displays 'No matching sources found' if there are no sources", () => {
const wrapper = shallowMount(ZSourcesList, {
mocks: {
$apollo: {
queries: {
suggestions: {
loading: false,
},
},
},
},
stubs,
sync: false,
data() {
return {
suggestions: [],
}
},
})

expect(wrapper.find(".notification .z-note")).to.exist
})
})

最佳答案

尝试在本地 vue 实例上注册自定义指令,然后挂载到该本地 vue 实例。

import { shallowMount, createLocalVue } from "@vue/test-utils" 
import { scrollText } from "z-common/services"
import ZSourcesList from "./ZSourcesList"

const localVue = createLocalVue()
localVue.use(scrollText) // Register the plugin to local vue

const stubs = [
"z-text-field",
"v-progress-circular",
"v-icon",
"z-btn"
]

describe("ZSourcesList.vue", () => {
const sources = []
for (let i = 0; i < 20; i++) {
sources.push({
field: "source",
// format numbers to get 2 diggit number with leading zero 1 -> 01
value: `cluster-${i.toLocaleString('en-US', { minimumIntegerDigits: 2, useGrouping: false })}`,
__typename: "SuggestV2Result"
})
}

it("displays 'No matching sources found' if there are no sources", () => {
const wrapper = shallowMount(ZSourcesList, {
mocks: {
$apollo: {
queries: {
suggestions: {
loading: false,
},
},
},
},
localVue, // Mount this component to localVue
stubs,
sync: false,
data() {
return {
suggestions: [],
}
},
})

expect(wrapper.find(".notification .z-note")).to.exist
})
})

在测试用例中使用本地 vue 实例而不是全局 vue 实例还可以防止污染全局 vue 实例,并有助于防止其他测试用例中的副作用。

关于javascript - 如何避免自定义指令的[Vue warn]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61346559/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com