From fc0b7d89325c17b24a2afcfd23d336c807ca823b Mon Sep 17 00:00:00 2001 From: Daniele Pini Date: Fri, 17 Jun 2022 10:01:33 +0200 Subject: [PATCH 1/2] Fix retry on upcoming assertions --- component/advanced/ProductsList.spec.js | 27 +++++++++++++++++++++++++ cypress/integration/calculator.spec.js | 12 +++++++++++ src/reactHandler.js | 8 ++------ 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/component/advanced/ProductsList.spec.js b/component/advanced/ProductsList.spec.js index 83bb3e7..1e86725 100644 --- a/component/advanced/ProductsList.spec.js +++ b/component/advanced/ProductsList.spec.js @@ -153,5 +153,32 @@ describe('Selecting by React props and state', () => { props: { name: 'Second item' }, }).should('have.length', '1'); }); + + it('retries until timeout or upcoming assertions succeed', () => { + cy.stub(window, 'fetch') + .withArgs('http://myapi.com/products') + .resolves({ + json: cy.stub().resolves({ + products: [ + { id: 1, name: 'First item' }, + { id: 2, name: 'Second item' }, + ], + }), + }); + mount(); + + // to find DOM elements by React component constructor name, props, or state + cy.waitForReact(); + + const assertFn = cy.stub().returns(false); + assertFn.onCall(5).returns(true); + + cy.getReact('ProductsContainer', { options: { timeout: 1000 } }).should(() => { + // should retry 10 times + + const value = assertFn(); + expect(value).to.equal(true); // this should ultimately succeed at the 5th retry + }); + }); }); }); diff --git a/cypress/integration/calculator.spec.js b/cypress/integration/calculator.spec.js index 7e2e9e6..3cfa096 100644 --- a/cypress/integration/calculator.spec.js +++ b/cypress/integration/calculator.spec.js @@ -57,4 +57,16 @@ describe('It should validate cypress react selector', () => { .getProps('name') .should('eq', '5'); }); + + it('getReact should retry upcoming assertions', () => { + const assertFn = cy.stub().returns(false); + assertFn.onCall(5).returns(true); + + cy.getReact('t').should(() => { + // should retry 10 times + + const value = assertFn(); + expect(value).to.equal(true); // this should ultimately succeed at the 5th retry + }); + }); }); diff --git a/src/reactHandler.js b/src/reactHandler.js index 013bd7f..1c6ad04 100644 --- a/src/reactHandler.js +++ b/src/reactHandler.js @@ -145,9 +145,7 @@ exports.react = (subject, component, reactOpts = {}) => { }); }; - return resolveValue().then((value) => { - return value; - }); + return resolveValue(); }; /** @@ -273,9 +271,7 @@ exports.getReact = (subject, component, reactOpts = {}) => { }); }; - return resolveValue().then((value) => { - return value; - }); + return resolveValue(); }; /** From 4afe81e128c16b78ad7e3e8db21753b7895bfa7d Mon Sep 17 00:00:00 2001 From: Daniele Pini Date: Fri, 17 Jun 2022 10:08:28 +0200 Subject: [PATCH 2/2] Add forgotten specific timeout on test --- cypress/integration/calculator.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cypress/integration/calculator.spec.js b/cypress/integration/calculator.spec.js index 3cfa096..c479b37 100644 --- a/cypress/integration/calculator.spec.js +++ b/cypress/integration/calculator.spec.js @@ -62,7 +62,7 @@ describe('It should validate cypress react selector', () => { const assertFn = cy.stub().returns(false); assertFn.onCall(5).returns(true); - cy.getReact('t').should(() => { + cy.getReact('t', { options: { timeout: 1000 } }).should(() => { // should retry 10 times const value = assertFn();