Skip to content

Commit

Permalink
add timeout test case to fetch api
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik-Outreach committed Nov 2, 2021
1 parent 8359d55 commit 729c28c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/collector/LogCollectCypressFetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ module.exports = class LogCollectCypressFetch {
if (statusCode) {
log += `\nStatus: ${statusCode}`;
}
if (options.err && options.err.message.match(/abort/)) {
log += ' - ABORTED';
if (options.err && options.err.message) {
log += ' - ' + options.err.message;
}

if (
Expand Down
63 changes: 63 additions & 0 deletions test/cypress/integration/fetchApi.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,70 @@ describe('Fetch Api', () => {
cy.wait('@putComment');

cy.get('.breaking-get', {timeout: 1});
})

context('Timeout', () => {

it('forceNetworkError ', () => {
cy.visit('/commands/network-requests');

cy.intercept(
{
method: 'PUT',
url: 'comments/*',

},
{
forceNetworkError: true,
}
).as('putComment');

cy.window().then((w) => {
fetch('/comments/10', {
method: 'PUT',
body: 'test',
});
});

cy.wait('@putComment');

cy.get('.breaking-get', {timeout: 1});
});

// Currently Cypress can't handle fetch Abort properly. It produces an unhandled entry, while the request remains "pending":
// cy:command ✘ uncaught exception AbortError: The user aborted a request.
it('timeout using AbortController', () => {
cy.visit('/commands/network-requests');

cy.intercept(
{
method: 'PUT',
url: 'comments/*',

},
{
delay: 500,
}
).as('putComment');

cy.window().then((w) => {

const controller = new AbortController();
setTimeout(() => controller.abort(), 100);

fetch('/comments/10', {
method: 'PUT',
body: 'test',
signal: controller.signal
});
});

cy.wait('@putComment');

cy.get('.breaking-get', {timeout: 1});
});
});


context('Real Fetch Requests', () => {
const testRealFetchRequest = (options) => {
Expand Down
6 changes: 6 additions & 0 deletions test/specs/commandsLogging.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ describe('Commands logging.', () => {
expect(stdout).to.contain(`Status: 404\n`);
expect(stdout).to.contain(`Response body: {\n${PADDING} "error": "Test message."\n${PADDING}}\n`);

// timeouts / abort
expect(cleanStdout).to.contain(
`(putComment) STUBBED PUT https://example.cypress.io/comments/10 - forceNetworkError called`,
'network failed request contains failure message'
);

// test real fetch requests
expect(cleanStdout).to.contain(
`cy:fetch ${ICONS.route} GET https://jsonplaceholder.cypress.io/comments/1\n${PADDING} Status: 200\n`,
Expand Down

0 comments on commit 729c28c

Please sign in to comment.