Home  >  Q&A  >  body text

Copy plugin in Vue Cypress component test

<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粉725827686P粉725827686415 days ago468

reply all(1)I'll reply

  • P粉561323975

    P粉5613239752023-08-31 10:16:55

    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],
        },
      });
    });

    reply
    0
  • Cancelreply