Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix async timeouts #5097

Merged
merged 19 commits into from
Sep 27, 2019
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/driver/src/cypress/cy.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -537,8 +537,8 @@ create = (specWindow, Cypress, Cookies, state, config, log) ->
## cleanup could be called during a 'stop' event which
## could happen in between a runnable because they are async
if state("runnable")
## make sure we don't ever time out this runnable now
timeouts.clearTimeout()
## make sure we reset the runnable's timeout now
state("runnable").resetTimeout()

## if a command fails then after each commands
## could also fail unless we clear this out
Expand Down
2 changes: 2 additions & 0 deletions packages/driver/src/cypress/runner.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,13 @@ testBeforeRunAsync = (test, Cypress) ->
fire("runner:test:before:run:async", test, Cypress)

runnableAfterRunAsync = (runnable, Cypress) ->
runnable.clearTimeout()
Promise.try ->
if not fired("runner:runnable:after:run:async", runnable)
fire("runner:runnable:after:run:async", runnable, Cypress)

testAfterRun = (test, Cypress) ->
test.clearTimeout()
if not fired(TEST_AFTER_RUN_EVENT, test)
setWallClockDuration(test)
fire(TEST_AFTER_RUN_EVENT, test, Cypress)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe "src/cy/commands/angular", ->
cy.ng("binding", "not-found")

it "cancels additional finds when aborted", (done) ->
cy.timeout(1000)
cy.stub(Cypress.runner, "stop")

retry = _.after 2, =>
Expand Down Expand Up @@ -108,6 +109,7 @@ describe "src/cy/commands/angular", ->
cy.ng("repeater", "not-found")

it "cancels additional finds when aborted", (done) ->
cy.timeout(1000)
cy.stub(Cypress.runner, "stop")

retry = _.after 2, =>
Expand Down Expand Up @@ -221,6 +223,7 @@ describe "src/cy/commands/angular", ->
done()

it "cancels additional finds when aborted", (done) ->
cy.timeout(1000)
cy.stub(Cypress.runner, "stop")

retry = _.after 2, =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ describe "src/cy/commands/navigation", ->
expect(win.foo).to.be.undefined

it "throws when reload times out", (done) ->
cy.timeout(1000)
locReload = cy.spy(Cypress.utils, "locReload")

cy
Expand Down Expand Up @@ -326,6 +327,7 @@ describe "src/cy/commands/navigation", ->
cy.go(0)

it "throws when go times out", (done) ->
cy.timeout(1000)
cy
.visit("/timeout?ms=100")
.visit("/fixtures/jquery.html")
Expand Down
84 changes: 84 additions & 0 deletions packages/driver/test/cypress/integration/cypress/runner_spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
/* eslint-disable mocha/handle-done-callback */

const pending = []

const throwMetaError = (mes) => {
const err = new Error(mes)

err.name = 'MetaError'
throw err
}

Cypress.on('test:after:run', (test) => {
if (test.state === 'pending') {
return pending.push(test)
Expand Down Expand Up @@ -32,4 +41,79 @@ describe('src/cypress/runner', () => {
expect(pending[1].title).to.eq('is pending 2')
})
})

context('async timeouts', () => {
kuceb marked this conversation as resolved.
Show resolved Hide resolved
let lastTimeout = null
const runner = Cypress.mocha.getRunner()

const throwAfter = (n) => {
return setTimeout(() => {
throwMetaError(`The test did not time out before ${n} millis`)
}, n)
}

const getTest = (r) => r && r.ctx.currentTest || r

beforeEach(() => {
lastTimeout = throwAfter(500)

runner.once('fail', (r) => {
const runnable = cy.state('runnable')
const test = getTest(runnable)

if (test.err.name === 'Uncaught MetaError') {
test.err.message = test.err.message.split('\nThis error originated from')[0]

return
}

expect(r.err.message).to.contain('Timed out after')

test.error = null
test.state = 'passed'

return false
})
})

afterEach(() => {
clearTimeout(lastTimeout)
})

it('can timeout async test', function (done) {
this.timeout(100)
})

it('can timeout async test after cypress command', function (done) {
this.timeout(100)
cy.wait(0)
})

it('does not timeout during cypress command', function (done) {
this.timeout(100)
cy.wait(200)
cy.then(() => done())
})

it('defaults to 4000 mocha timeout for tests', function () {
expect(this.timeout()).eq(4000)
})

describe('hooks can timeout and share timeout with test', function () {
beforeEach((done) => {
this.ctx.test.timeout(100)
})

it('should timeout on hook', () => {})
})

describe('hooks can timeout and share timeout with test after cypress command', function () {
beforeEach((done) => {
cy.wait(0)
this.ctx.test.timeout(100)
})

it('should timeout on hook', () => {})
})
})
})