在 Vue Cypress 元件測試中複製插件
P粉725827686
2023-08-30 19:44:36
<p>我正在使用 Cypress 來元件測試我的 Vue 應用程式。遵循 https://docs.cypress.io/guides/component-testing/vue/examples#Replicating-Plugins 中的程式碼範例會產生幾個錯誤,如下所列:</p>
<pre class="brush:php;toolbar:false;">Argument of type '(this: Context, component:
ComponentOptionsWithObjectProps<Readonly<ComponentPropsOptions<Data>>, unknown, {},
ComputedOptions, Record<string, Function>, ... 7 more ..., { ...; } | {}>, options?:
MountingOptions<...> | undefined) => Chainable<...>' is not assignable to parameter of type
'CommandFn<"mount">'.
Type 'Chainable<{ wrapper: VueWrapper<ComponentPublicInstance<{ [x: number]: unknown; } & {
readonly length?: number | Prop<unknown, unknown> | null | undefined; readonly concat?:
Prop<unknown, unknown> | { (...items: ConcatArray<string>[]): string[]; (...items: (string |
ConcatArray<...>)[]): string[]; } | null | undefin...' is not assignable to type 'void |
Chainable<{ wrapper:
VueWrapper<ComponentPublicInstance<ExtractPropTypes<Readonly<ComponentPropsOptions<Data>>>,
unknown, {}, ComputedOptions, ... 6 more ..., {}>>; component: ComponentPublicInstance<...>;
}>'.
Property 'global' does not exist on type '[options?: MountingOptions<{ [x: number]: unknown; }
& { readonly length?: number | Prop<unknown, unknown> | null | undefined; readonly concat?:
Prop<unknown, unknown> | { (...items: ConcatArray<string>[]): string[]; (...items: (string |
ConcatArray<...>)[]): string[]; } | null | undefined; ... 12 more ...; toLocale...'.
Cannot find name 'Vapp'.
No overload matches this call.
The last overload gave the following error.</pre>
<p>我正在使用 TypeScript。有人可以提供我一個工作程式碼片段嗎?我的程式碼如下:</p>
<pre class="brush:php;toolbar:false;">import { createPinia } from "pinia";
import i18n from "../../src/locales/i18n";
import { mount } from "cypress/vue";
import { h } from "vue";
declare global {
namespace Cypress {
interface Chainable {
mount: typeof mount;
}
}
}
Cypress.Commands.add("mount", (component, ...args) => {
args.global = args.global || {};
args.global.plugins = args.global.plugins || [];
args.global.plugins.push(createPinia());
args.global.plugins.push(i18n);
return mount(() => {
return h(Vapp, {}, component)
}, ...args);
});</pre>
<p>有人知道如何解決這個問題嗎?在測試中使用時我如何呼叫這個掛載函數? </p>
我使用 Vue Test Utils 語法解決了這個問題。在 /support/component.ts 中: