English 中文(简体)
测试和模拟窗口。 未被称作近距离
原标题:testing and mocking window.close spy not being called

我正在使用假肢测试法进行测试,以便单位测试我的方法。 其中一个测试项目是,一个州应当把一个接近()的方法点上点击,这反过来又称作浏览器window.close():

const close = () => {
  console.log( close() called )
  window.close()
}

I have a unit test that mocks window.close but the test is not passing: expected "spy" to be called once, but got 0 times

我确信,随着我看到奥塞里的话,该构成部分的“接近”方法被称作“。 测试记录中的记录产出。

Here is the test method:

import { test, vi, expect } from "vitest";
import { mount } from "@vue/test-utils";

test( window.close is called when button is clicked , async () => {
  const wrapper = mount(MyComponent, {
    global: {
      plugins: [router]
    }
  })
  const btn = wrapper.find( #exit-button )
  await btn.trigger( click )
  window.close = vi.fn()
  expect(window.close).toHaveBeenCalledOnce()
})
问题回答

替换<代码>window.close可能不会产生消极的副作用,但一般来说,这种影响会受到抑制。 <>code>spyOn, 宁可采用一些方法,因为它能够恢复原来的实施。

A spy should be set before it is called, it should be:

  vi.spyOn(window,  close )
  const btn = wrapper.find( #exit-button )
  await btn.trigger( click )
  expect(window.close).toHaveBeenCalledOnce()




相关问题
Nuxt 3 useRoute import cannot find

I am using latest nuxt version and when I tried to use the useRoute method is triggers a "Cannot Find name useRoute" but it works so I would like to know what could I be missing My package ...

Vue array dependent select not display data

I have dependent selection like this. Selection image when i console log, they have data but not render data in selection data This is what i ve tried. for Product selection has default selected value ...

How to inject Bearer Token in Axios boot file

Quasar has a different setup using boot files and my current axios.ts looks liek this: import { boot } from quasar/wrappers ; import axios, { AxiosInstance } from axios ; declare module @vue/...

Vue router and Vite Base url conflict

I use Vue Router to develop my single page application and use Vite to package my project. I want to deploy all static files (such as index.js and index.css) under another CDN domain name (cdn.example....

Vue.js change class on mounted

I have a button element with a .hover property and I need the hover animation to also happen as soon as the page is mounted. How can I attribute a class to a button on the mounted function and then ...

Laravel +Vuejs CORS issue

I am facing a dilemma wit the CORS issue. I have application that is configured as follow: API: https://abc.mydomain.com (laravel) front end: https://subdomain.mydomain.com(VueJS) Both applications ...

热门标签