From 729c28c745cbf04bf920dfd5856b18637b1098f6 Mon Sep 17 00:00:00 2001 From: Erik Witkowski Date: Tue, 2 Nov 2021 19:31:31 +0100 Subject: [PATCH] add timeout test case to fetch api --- src/collector/LogCollectCypressFetch.js | 4 +- test/cypress/integration/fetchApi.spec.js | 63 +++++++++++++++++++++++ test/specs/commandsLogging.spec.js | 6 +++ 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/src/collector/LogCollectCypressFetch.js b/src/collector/LogCollectCypressFetch.js index ba7e097..ba3c593 100644 --- a/src/collector/LogCollectCypressFetch.js +++ b/src/collector/LogCollectCypressFetch.js @@ -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 ( diff --git a/test/cypress/integration/fetchApi.spec.js b/test/cypress/integration/fetchApi.spec.js index a37821a..f99dacd 100644 --- a/test/cypress/integration/fetchApi.spec.js +++ b/test/cypress/integration/fetchApi.spec.js @@ -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) => { diff --git a/test/specs/commandsLogging.spec.js b/test/specs/commandsLogging.spec.js index 339771a..064e887 100755 --- a/test/specs/commandsLogging.spec.js +++ b/test/specs/commandsLogging.spec.js @@ -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`,