在VUE组件测试中附加资源

发布于 2025-02-10 06:31:06 字数 686 浏览 1 评论 0原文

我正在我的VUE项目上运行组件测试,遵循文档开始柏树组件测试(VUE 2/3)

import { mount } from '@cypress/vue'
import HelloWorld from './HelloWorld.vue'

describe('HelloWorld', () => {
  it('renders a message', () => {
    const msg = 'Hello Cypress Component Testing!'
    mount(HelloWorld, {
      propsData: {
        msg
      }
    })

    cy.get('h1').should('have.text', msg)
  })
})

我如何包括通常加载到index.html的资源,因为组件测试中没有基本页面。

I'm running component tests on my Vue project, have followed the documentation Getting Started with Cypress Component Testing (Vue 2/3).

import { mount } from '@cypress/vue'
import HelloWorld from './HelloWorld.vue'

describe('HelloWorld', () => {
  it('renders a message', () => {
    const msg = 'Hello Cypress Component Testing!'
    mount(HelloWorld, {
      propsData: {
        msg
      }
    })

    cy.get('h1').should('have.text', msg)
  })
})

How do I include resources that are normally loaded to index.html, as there is no base page in component tests.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

农村范ル 2025-02-17 06:31:06

例如

const div = document.createElement('div')
div.id = 'root'
document.body.appendChild(div)

const linkElem = document.createElement('link');
linkElem.setAttribute('rel', 'stylesheet');
linkElem.setAttribute('href', 'https://cdnjs.cloudflare.com/ajax/libs/MaterialDesign-Webfont/6.7.96/css/materialdesignicons.min.css');
document.head.appendChild(linkElem)

mount(HelloWorld, {
  propsData: {
    msg
  },
  attachTo: '#root'
})

Create your resource link on document and attach your component to a child element of the same document, for example

const div = document.createElement('div')
div.id = 'root'
document.body.appendChild(div)

const linkElem = document.createElement('link');
linkElem.setAttribute('rel', 'stylesheet');
linkElem.setAttribute('href', 'https://cdnjs.cloudflare.com/ajax/libs/MaterialDesign-Webfont/6.7.96/css/materialdesignicons.min.css');
document.head.appendChild(linkElem)

mount(HelloWorld, {
  propsData: {
    msg
  },
  attachTo: '#root'
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文