From 656a55e87a5e134370523f30e7c554ad908f7ba6 Mon Sep 17 00:00:00 2001 From: Alexandre Hajjar Date: Wed, 9 Jun 2021 12:16:37 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20=20Make=20cypress=20record=5Fhttp?= =?UTF-8?q?=20deterministic=20(somehow)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mon-entreprise/english/gérer.js | 19 ++++++++++++------- mon-entreprise/cypress/support/commands.js | 1 + mon-entreprise/package.json | 1 + yarn.lock | 5 +++++ 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/mon-entreprise/cypress/integration/mon-entreprise/english/gérer.js b/mon-entreprise/cypress/integration/mon-entreprise/english/gérer.js index d903174a6..12ad73074 100644 --- a/mon-entreprise/cypress/integration/mon-entreprise/english/gérer.js +++ b/mon-entreprise/cypress/integration/mon-entreprise/english/gérer.js @@ -4,11 +4,13 @@ const FIXTURES_FOLDER = 'cypress/fixtures' const GERER_FIXTURES_FOLDER = `${FIXTURES_FOLDER}/gérer` const writeFixtures = Cypress.env('record_http') !== undefined const stubFixtures = !writeFixtures -const setInterceptResponses = (responses, hostnames) => { +const setInterceptResponses = (pendingRequests, responses, hostnames) => { if (writeFixtures) { cy.intercept('*', (req) => { if (!hostnames.includes(new URL(req.url).hostname)) return + pendingRequests.add(req.url) req.on('after:response', (res) => { + pendingRequests.delete(req.url) responses[res.url] = res.body }) }) @@ -29,11 +31,12 @@ const setInterceptResponses = (responses, hostnames) => { }) } } -const waitResponses = (responses) => { +const writeResponses = (pendingRequests, responses) => { if (writeFixtures) { - // No need to `cy.wait`, we anyway don't care about not-yet-received - // responses, as we don't care about responses not yet utilized in the - // test itself. Caveat: fixtures folder is undeterministic when recording. + // We need to wait on all catched requests to be fulfilled and recorded, + // otherwise the stubbed cy run might error when a request is not stubbed. + // Caveat: we assume request.url to be unique amongst recorded requests. + cy.waitUntil(() => pendingRequests.size === 0) Object.keys(responses).map((url) => { if (responses[url] === undefined) return cy.writeFile( @@ -47,15 +50,17 @@ const waitResponses = (responses) => { describe(`Manage page test (${ writeFixtures ? 'record mode' : 'stubbed mode' })`, function () { + let pendingRequests = new Set() let responses = {} const hostnamesToRecord = ['entreprise.data.gouv.fr', 'geo.api.gouv.fr'] beforeEach(() => { + pendingRequests = new Set() responses = {} - setInterceptResponses(responses, hostnamesToRecord) + setInterceptResponses(pendingRequests, responses, hostnamesToRecord) cy.visit(fr ? encodeURI('/gérer') : '/manage') }) afterEach(() => { - waitResponses(responses) + writeResponses(pendingRequests, responses) }) it('should not crash', function () { cy.contains(fr ? 'Gérer mon activité' : 'Manage my business') diff --git a/mon-entreprise/cypress/support/commands.js b/mon-entreprise/cypress/support/commands.js index e25527a4f..dc3166ff6 100644 --- a/mon-entreprise/cypress/support/commands.js +++ b/mon-entreprise/cypress/support/commands.js @@ -28,3 +28,4 @@ Cypress.Commands.add('iframe', { prevSubject: 'element' }, ($iframe) => { setTimeout(() => resolve($iframe.contents().find('body')), 6000) }) }) +import 'cypress-wait-until' diff --git a/mon-entreprise/package.json b/mon-entreprise/package.json index 28401687a..230a2d99e 100644 --- a/mon-entreprise/package.json +++ b/mon-entreprise/package.json @@ -46,6 +46,7 @@ "copy-webpack-plugin": "^4.5.2", "cypress": "^7.4.0", "cypress-plugin-tab": "^1.0.5", + "cypress-wait-until": "^1.7.1", "eslint-plugin-react": "^7.12.4", "html-webpack-plugin": "^3.2.0", "i18next-parser": "^3.3.0", diff --git a/yarn.lock b/yarn.lock index a63ae01e1..9b887f8da 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5122,6 +5122,11 @@ cypress-plugin-tab@^1.0.5: dependencies: ally.js "^1.4.1" +cypress-wait-until@^1.7.1: + version "1.7.1" + resolved "https://registry.yarnpkg.com/cypress-wait-until/-/cypress-wait-until-1.7.1.tgz#3789cd18affdbb848e3cfc1f918353c7ba1de6f8" + integrity sha512-8DL5IsBTbAxBjfYgCzdbohPq/bY+IKc63fxtso1C8RWhLnQkZbVESyaclNr76jyxfId6uyzX8+Xnt0ZwaXNtkA== + cypress@^7.4.0: version "7.4.0" resolved "https://registry.yarnpkg.com/cypress/-/cypress-7.4.0.tgz#679bfe75335b9a4873d44f0d989e9f0367f00665"