Skip to content

Commit

Permalink
fix _global.setTimeout being undefined when restoring the clock (cypr…
Browse files Browse the repository at this point in the history
…ess-io#2875)

fixes cypress-io#2850 

this fixes an edge case where the application under test changed the
clock at a later time and when we restored the clock, the global
function became undefined due to deleting the property off of window
  • Loading branch information
brian-mann authored Dec 3, 2018
1 parent e7b8683 commit 2f97cb3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/driver/src/cypress/clock.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ create = (win, now, methods) ->
clock.tick(ms)

restore = ->
_.each clock.methods, (method) ->
try
## before restoring the clock, we need to
## reset the hadOwnProperty in case a
## the application code eradicated the
## overridden clock method at a later time.
## this is a property that lolex using internally
## when restoring the global methods.
## https://github.com/cypress-io/cypress/issues/2850
fn = clock[method]
if fn and fn.hadOwnProperty and win[method]
win[method].hadOwnProperty = true

clock.uninstall()

bind = (win) ->
Expand Down
37 changes: 37 additions & 0 deletions packages/driver/test/cypress/integration/issues/2850_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// https://github.com/cypress-io/cypress/issues/2850
describe('issue #2850: invoking cy.clock() before two visits', () => {
it('works the first time', () => {
cy.clock()
cy.visit('/fixtures/generic.html')
cy.window().then((win) => {
// override the setTimeout function now
win.setTimeout = () => {}
})
})

it('works the second time', () => {
cy.clock()
cy.visit('/fixtures/generic.html')
})

it('works the third time', () => {
cy.clock().then((clock) => {
cy.visit('/fixtures/generic.html')
cy.window().then((win) => {
// override the setTimeout function now
win.setTimeout = () => { }

// manually restore the clock
clock.restore()
})
cy.clock().then((clock2) => {
clock2.restore()
})
})
})

it('works the forth time', () => {
cy.clock()
cy.visit('/fixtures/generic.html')
})
})

0 comments on commit 2f97cb3

Please sign in to comment.