Copy plugin in Vue Cypress component test
P粉725827686
P粉725827686 2023-08-30 19:44:36
0
1
554
<p>I'm using Cypress to component test my Vue application. Following the code examples at https://docs.cypress.io/guides/component-testing/vue/examples#Replicating-Plugins will produce several errors, as listed below: </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>I'm using TypeScript.Can someone provide me with a working code snippet? My code is as follows: </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>Does anyone know how to fix this? How do I call this mount function when using it in tests? </p>
P粉725827686
P粉725827686

reply all(1)
P粉561323975

I solved this problem using Vue Test Utils syntax. In /support/component.ts:

import { mount } from "cypress/vue";

Cypress.Commands.add("mount", (component) => {
  return mount(component, {
    global: {
      plugins: [createPinia(), i18n],
    },
  });
});
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template