-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
95 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Test timeout | ||
|
||
The example shows how to stop a test if it takes too long to execute, similar to [Mocha's timeout](https://mochajs.org/#test-level) option. See [cypress/integration/spec.js](cypress/integration/spec.js) | ||
|
||
```js | ||
// this test fails after two seconds due to timeout | ||
it('does not finish long tests', () => { | ||
testTimeout(2 * 1000) | ||
cy.wait(10 * 1000) | ||
}) | ||
``` | ||
|
||
 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"fixturesFolder": false, | ||
"pluginsFile": false, | ||
"supportFile": false | ||
} |
59 changes: 59 additions & 0 deletions
59
examples/fundamentals__timeout/cypress/integration/spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/// <reference types="cypress" /> | ||
/* eslint-disable no-console */ | ||
|
||
const seconds = (n) => n * 1000 | ||
|
||
/** | ||
* Stops the current Cypress test if it takes longer than the provided timeout | ||
* @param {number} ms Test timeout in milliseconds | ||
* @example | ||
* // stop and fail the test if it runs for longer than 10 seconds | ||
* testTimeout(10 * 1000) | ||
*/ | ||
function testTimeout (ms) { | ||
// get the current test reference using | ||
// the cy.state() magic method | ||
const currentTest = cy.state('runnable') | ||
const startedAt = +new Date() | ||
|
||
setTimeout(() => { | ||
const testNow = cy.state('runnable') | ||
|
||
console.log('test started', currentTest) | ||
console.log('test now', testNow) | ||
console.log('test now state', testNow.state) | ||
|
||
if (currentTest !== testNow) { | ||
// different test already | ||
return | ||
} | ||
|
||
if (testNow.state) { | ||
// test has finished | ||
return | ||
} | ||
|
||
const timeNow = +new Date() | ||
|
||
console.log('elapsed %d limit %d', timeNow - startedAt, ms) | ||
if (timeNow - startedAt >= ms) { | ||
throw new Error(`Test ran longer than ${ms}ms`) | ||
} | ||
}, ms) | ||
} | ||
|
||
describe('Test timeout', () => { | ||
// this test runs fine, because it finishes | ||
// before the ten second limit expires | ||
it('allows short tests', () => { | ||
testTimeout(seconds(10)) | ||
cy.wait(seconds(5)) | ||
}) | ||
|
||
// this test fails after two seconds due to timeout | ||
// NOTE: enable to see test timeout in action | ||
it.skip('does not finish long tests', () => { | ||
testTimeout(seconds(2)) | ||
cy.wait(seconds(10)) | ||
}) | ||
}) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "test-timeout", | ||
"version": "1.0.0", | ||
"description": "Test throws an error if it runs over the time limit", | ||
"scripts": { | ||
"cypress:open": "../../node_modules/.bin/cypress open", | ||
"cypress:run": "../../node_modules/.bin/cypress run", | ||
"test:ci": "npm run cypress:run" | ||
} | ||
} |