Skip to content

Commit 6f9e679

Browse files
authored
feat: add example how spy retries following assertions (#204)
1 parent 2ce848d commit 6f9e679

File tree

2 files changed

+60
-11
lines changed

2 files changed

+60
-11
lines changed

app/commands/spies-stubs-clocks.html

+27-5
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ <h1>Spies, Stubs &amp; Clocks</h1>
7474
<div class="col-xs-12">
7575
<h4><a href="https://on.cypress.io/spy">cy.spy()</a></h4>
7676
<p>To wrap a method in a spy, use the <a href="https://on.cypress.io/spy"><code>cy.spy()</code></a> command.</p>
77-
<pre><code class="javascript">let obj = {
77+
<pre><code class="javascript">const obj = {
7878
foo () {},
7979
}
8080

81-
let spy = cy.spy(obj, 'foo').as('anyArgs')
81+
const spy = cy.spy(obj, 'foo').as('anyArgs')
8282

8383
obj.foo()
8484

@@ -87,14 +87,36 @@ <h4><a href="https://on.cypress.io/spy">cy.spy()</a></h4>
8787

8888
<div class="col-xs-12"><hr></div>
8989

90+
<div class="col-xs-12">
91+
<p><a href="https://on.cypress.io/spy"><code>cy.spy()</code></a> retries until the
92+
assertions that follow it pass.</p>
93+
<pre><code class="javascript">const obj = {
94+
foo () {},
95+
}
96+
97+
cy.spy(obj, 'foo').as('foo')
98+
99+
setTimeout(() => {
100+
obj.foo()
101+
}, 500)
102+
103+
setTimeout(() => {
104+
obj.foo()
105+
}, 2500)
106+
107+
cy.get('@foo').should('have.been.calledTwice')</code></pre>
108+
</div>
109+
110+
<div class="col-xs-12"><hr></div>
111+
90112
<div class="col-xs-12">
91113
<h4><a href="https://on.cypress.io/stub">cy.stub()</a></h4>
92114
<p>To create a stub and/or replace a function with a stub, use the <a href="https://on.cypress.io/stub"><code>cy.stub()</code></a> command.</p>
93115
<pre><code class="javascript">let obj = {
94116
foo () {},
95117
}
96118

97-
let stub = cy.stub(obj, 'foo').as('foo')
119+
const stub = cy.stub(obj, 'foo').as('foo')
98120

99121
obj.foo('foo', 'bar')
100122

@@ -108,7 +130,7 @@ <h4><a href="https://on.cypress.io/clock">cy.clock()</a></h4>
108130
<p>To control time in the browser, use the <a href="https://on.cypress.io/clock"><code>cy.clock()</code></a> command.</p>
109131
<pre><code class="javascript">// create the date in UTC so its always the same
110132
// no matter what local timezone the browser is running in
111-
let now = new Date(Date.UTC(2017, 2, 14)).getTime()
133+
const now = new Date(Date.UTC(2017, 2, 14)).getTime()
112134

113135
cy.clock(now)
114136
cy.visit('http://localhost:8080/commands/spies-stubs-clocks')
@@ -130,7 +152,7 @@ <h4><a href="https://on.cypress.io/tick">cy.tick()</a></h4>
130152
<p>To move time in the browser, use the <a href="https://on.cypress.io/tick"><code>cy.tick()</code></a> command.</p>
131153
<pre><code class="javascript">// create the date in UTC so its always the same
132154
// no matter what local timezone the browser is running in
133-
let now = new Date(Date.UTC(2017, 2, 14)).getTime()
155+
const now = new Date(Date.UTC(2017, 2, 14)).getTime()
134156

135157
cy.clock(now)
136158
cy.visit('http://localhost:8080/commands/spies-stubs-clocks')

cypress/integration/examples/spies_stubs_clocks.spec.js

+33-6
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,49 @@ context('Spies, Stubs, and Clock', () => {
55
// https://on.cypress.io/spy
66
cy.visit('http://localhost:8080/commands/spies-stubs-clocks')
77

8-
let obj = {
8+
const obj = {
99
foo () {},
1010
}
1111

12-
let spy = cy.spy(obj, 'foo').as('anyArgs')
12+
const spy = cy.spy(obj, 'foo').as('anyArgs')
1313

1414
obj.foo()
1515

1616
expect(spy).to.be.called
1717
})
1818

19+
it('cy.spy() retries until assertions pass', () => {
20+
cy.visit('http://localhost:8080/commands/spies-stubs-clocks')
21+
22+
const obj = {
23+
/**
24+
* Prints the argument passed
25+
* @param x {any}
26+
*/
27+
foo (x) {
28+
/* eslint-disable-next-line no-console */
29+
console.log('obj.foo called with', x)
30+
},
31+
}
32+
33+
cy.spy(obj, 'foo').as('foo')
34+
35+
setTimeout(() => {
36+
obj.foo('first')
37+
}, 500)
38+
39+
setTimeout(() => {
40+
obj.foo('second')
41+
}, 2500)
42+
43+
cy.get('@foo').should('have.been.calledTwice')
44+
})
45+
1946
it('cy.stub() - create a stub and/or replace a function with stub', () => {
2047
// https://on.cypress.io/stub
2148
cy.visit('http://localhost:8080/commands/spies-stubs-clocks')
2249

23-
let obj = {
50+
const obj = {
2451
/**
2552
* prints both arguments to the console
2653
* @param a {string}
@@ -32,7 +59,7 @@ context('Spies, Stubs, and Clock', () => {
3259
},
3360
}
3461

35-
let stub = cy.stub(obj, 'foo').as('foo')
62+
const stub = cy.stub(obj, 'foo').as('foo')
3663

3764
obj.foo('foo', 'bar')
3865

@@ -44,7 +71,7 @@ context('Spies, Stubs, and Clock', () => {
4471

4572
// create the date in UTC so its always the same
4673
// no matter what local timezone the browser is running in
47-
let now = new Date(Date.UTC(2017, 2, 14)).getTime()
74+
const now = new Date(Date.UTC(2017, 2, 14)).getTime()
4875

4976
cy.clock(now)
5077
cy.visit('http://localhost:8080/commands/spies-stubs-clocks')
@@ -57,7 +84,7 @@ context('Spies, Stubs, and Clock', () => {
5784

5885
// create the date in UTC so its always the same
5986
// no matter what local timezone the browser is running in
60-
let now = new Date(Date.UTC(2017, 2, 14)).getTime()
87+
const now = new Date(Date.UTC(2017, 2, 14)).getTime()
6188

6289
cy.clock(now)
6390
cy.visit('http://localhost:8080/commands/spies-stubs-clocks')

0 commit comments

Comments
 (0)