cy.spy that monitors the window.clearInterval function
P粉023326773
P粉023326773 2023-08-17 12:16:34
0
1
393

In an application I'm developing, I use setInterval and clearInterval. I want to monitor the clearInterval method to see if it is called. Here's what I'm actually doing:

beforeEach(() => { cy.clock(new Date()) }) it('Test scenario' => { const fn = cy.spy(document.defaultView, 'clearInterval') //Also tried fn = cy.spy(window, 'clearInterval') ... ... ... //The clearInterval function is called when entering the `then` section, but the stub reports that it has not been called yet cy.tick(30000).then(() => { expect(fn).to.have.been.calledOnce }) }) afterEach(() => { cy.clock().invoke('restore') })

The expect assertion in the snippet above fails, I expect it to pass. Is my logic for initializing the spy valid|correct? Any help with the above issue is greatly appreciated.

P粉023326773
P粉023326773

reply all (1)
P粉745412116

Cypress uses differentwindowin tests and applications.cy.spy(window...is using the test window, but to monitor the application window, you need to use thecy.window()command.

let spy; cy.window().then(appWindow => { spy = cy.spy(appWindow, 'clearInterval') }) ... later expect(spy).to.have.been.calledOnce

But there may be another complication, becausecy.clock()putsclearInterval()in the proxy so that the application's timing function can be controlled, so You may not be able to monitor it.

If you find that it still doesn't work properly, please get the return value ofcy.clock()and see if the call information is attached.

let clock; beforeEach(() => { clock = cy.clock(new Date()) })

Or specifyclearIntervalshould not be proxied

beforeEach(() => { cy.clock(new Date(), ['Date', 'setInterval']) // 仅代理Date和setInterval })
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!