Skip to content

Commit c02df13

Browse files
authored
Fix infinite retry on failed assertions (#342)
1 parent 88b29a2 commit c02df13

File tree

2 files changed

+60
-39
lines changed

2 files changed

+60
-39
lines changed

cypress/integration/calculator.spec.js

+12
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,16 @@ describe('It should validate cypress react selector', () => {
6969
expect(value).to.equal(true); // this should ultimately succeed at the 5th retry
7070
});
7171
});
72+
73+
it('getReact should time out after failing assertions', () => {
74+
cy.on('fail', (err) => {
75+
if (err.message !== 'Timed out retrying after 1000ms: expected false to equal true') {
76+
throw err;
77+
}
78+
});
79+
80+
cy.getReact('t', { options: { timeout: 1000 } }).should(() => {
81+
expect(false).to.equal(true);
82+
});
83+
});
7284
});

src/reactHandler.js

+48-39
Original file line numberDiff line numberDiff line change
@@ -114,33 +114,37 @@ exports.react = (subject, component, reactOpts = {}) => {
114114
};
115115

116116
const resolveValue = () => {
117+
const retry = () => {
118+
if (retries < 1) {
119+
cy.log(
120+
getComponentNotFoundMessage(
121+
component,
122+
reactOpts.props,
123+
reactOpts.state
124+
)
125+
);
126+
return;
127+
}
128+
129+
return cy
130+
.wait(retryInterval, {
131+
log: false,
132+
})
133+
.then(() => {
134+
retries--;
135+
return resolveValue();
136+
});
137+
};
138+
117139
return _nodes().then((value) => {
118140
if (!value) {
119-
if (retries < 1) {
120-
cy.log(
121-
getComponentNotFoundMessage(
122-
component,
123-
reactOpts.props,
124-
reactOpts.state
125-
)
126-
);
127-
return;
128-
}
129-
130-
return cy
131-
.wait(retryInterval, {
132-
log: false,
133-
})
134-
.then(() => {
135-
retries--;
136-
return resolveValue();
137-
});
141+
return retry();
138142
}
139143
if (!isPrimitive(value)) {
140144
value = Cypress.$(value);
141145
}
142146
return cy.verifyUpcomingAssertions(value, (reactOpts || {}).options, {
143-
onRetry: resolveValue,
147+
onRetry: retry,
144148
});
145149
});
146150
};
@@ -243,30 +247,35 @@ exports.getReact = (subject, component, reactOpts = {}) => {
243247
}
244248
);
245249
};
250+
246251
const resolveValue = () => {
252+
const retry = () => {
253+
if (retries < 1) {
254+
cy.log(
255+
getComponentNotFoundMessage(
256+
component,
257+
reactOpts.props,
258+
reactOpts.state
259+
)
260+
);
261+
return;
262+
}
263+
return cy
264+
.wait(retryInterval, {
265+
log: false,
266+
})
267+
.then(() => {
268+
retries--;
269+
return resolveValue();
270+
});
271+
};
272+
247273
return _nodes().then((value) => {
248274
if (!value) {
249-
if (retries < 1) {
250-
cy.log(
251-
getComponentNotFoundMessage(
252-
component,
253-
reactOpts.props,
254-
reactOpts.state
255-
)
256-
);
257-
return;
258-
}
259-
return cy
260-
.wait(retryInterval, {
261-
log: false,
262-
})
263-
.then(() => {
264-
retries--;
265-
return resolveValue();
266-
});
275+
return retry();
267276
}
268277
return cy.verifyUpcomingAssertions(value, (reactOpts || {}).options, {
269-
onRetry: resolveValue,
278+
onRetry: retry,
270279
});
271280
});
272281
};

0 commit comments

Comments
 (0)