使用 Vue 和 Cypress 使用 Slots 发布测试组件

Fra*_*ros 3 vue.js cypress vuejs3

我是组件测试和赛普拉斯的新手。我一直按照官方文档和示例对我的项目进行一些基本的组件测试。最终,我偶然发现了一个案例,我想测试我编写的一个简单组件,该组件接受单个槽(default),并且由于它与存储库中提供的示例非常相似@cypress/vue,所以我冒昧地复制了代码并将其调整为我的喜好。

然而,虽然第一个测试通过并安装没有问题,但当我尝试安装正在测试的组件并将其传递给默认插槽时,我收到类型错误Cannot convert undefined or null to object。从那时起,我浏览了 Vue 和 Vue-Testing 示例,但我似乎没有弄清楚在调用 mount 函数时我做错了什么。下面是我的代码片段,供那些可以在这方面帮助我的人参考。

基本按钮

<template>
  <a role="button" class="btn btn-primary">
    <slot />
  </a>
</template>
Run Code Online (Sandbox Code Playgroud)

BaseButton.spec.ts

import BaseButton from '@/components/BaseButton.vue'
import { mount } from '@cypress/vue'

describe('BaseButton', () => {
  context('when slots are not passed', () => {
    it('renders nothing', () => {
      mount(BaseButton)
      cy.get('a').should('have.text', '')
    })
  })

  context('when slots are passed', () => {
    it('renders slots', () => {
      mount(BaseButton, {
        slots: {
          default: 'Test Button',
        },
      })

      cy.get('a').should('have.text', 'Test Button')
    })

    it('renders scopedSlots', () => {
      mount(BaseButton, {
        slots: {
          default: '<template #default="props"><p>Yay! {{props.content}}</p></template>',
        },
      })

      cy.contains('Yay! Scoped content!').should('be.visible')
      cy.contains('Nothing used the Scoped content!').should('not.exist')
    })
  })
})
Run Code Online (Sandbox Code Playgroud)

这是我基于代码的官方示例。

icu*_*ube 6

您可以尝试以下操作:

mount(BaseButton, {
   slots: {
      default: {
          render: () => "Test Button"
      }
   }
})
Run Code Online (Sandbox Code Playgroud)