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.
Cypress uses different
windowin 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.calledOnceBut there may be another complication, because
cy.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 of
cy.clock()and see if the call information is attached.let clock; beforeEach(() => { clock = cy.clock(new Date()) })Or specify
clearIntervalshould not be proxiedbeforeEach(() => { cy.clock(new Date(), ['Date', 'setInterval']) // 仅代理Date和setInterval })