7

在 Vue 上使用 Cypress 中的组件测试。我的项目组件使用vuetify 插件

目前,经过测试的组件使用 Vuetify 加载:

import DebuggingTemporaryComponent from "./DebuggingTemporaryComponent";
import {mount} from "@cypress/vue";
import vuetify from '../../resources/js/vuetify'

it('mounts the component with vuetify', () => {
    
    mount(DebuggingTemporaryComponent,{vuetify,})

    cy.contains('Hello World') ✅

}

但是,样式无法正常运行,因为 Vuetify 组件需要<v-app>在页面上至少包含一次。在组件测试中,这不会发生。

我需要按照React 等效文档中的建议自定义包装器。但是,每当我尝试创建自己的函数来执行此操作时,都会收到一个错误,指出相应的 webpack 加载器不存在。Vue 加载器在那里并且正在工作。

import {mount as cypressMount} from '@cypress/vue'

export function mount (component){
    return cypressMount(<v-app>component</v-app>, prepareComponent(props, options))
}

谁能帮助我下一步该去哪里?

4

2 回答 2

5

您可以在测试中构造一个简单的包装器,例如

要测试的组件 - Button.vue

<template>
  <v-btn color="red lighten-2" dark>
    Click Me
  </v-btn>
</template>

测试

import Button from "./Button";
import {mount} from "@cypress/vue";
import vuetify from '../plugins/vuetify'
import { VApp } from 'vuetify/lib/components'

const WrapperComp = {
  template: `
    <v-app>
      <Button />
    </v-app>
  `,
  components: {
    VApp,
    Button
  }
}

it('mounts the component with vuetify', () => {

  mount(WrapperComp, { vuetify })

  const lightRed = 'rgb(229, 115, 115)'
  cy.contains('button', 'Click Me')        // ✅
    .should('have.css', 'background-color', lightRed)  // fails if no <v-app> used above
})
于 2021-09-28T08:28:15.773 回答
-1

您会收到一个错误,因为您尝试使用Vue确实可以使用 JSX,但您需要配置其他构建插件。

不用 JSX 也可以通过使用render 函数来实现

import {mount} from "@cypress/vue";
import vuetify from '../../resources/js/vuetify'
import { VApp } from 'vuetify/lib/components'

function mountComponentWithVuetify(componentToMount, options = {}) 
{
  return mount({
    render(h) {
      return h(VApp, [h(componentToMount)])
    }
  },
  { 
    vuetify, 
    ...options,
  })
}

于 2021-09-22T18:18:34.180 回答