Skip to content

Commit b9f5ecb

Browse files
gabberseppjennifer-shehanebahmutov
committed
add example: how to stub console.log (#346)
* add example: how to stub console.log * Update Readme to include console example * update circle.yml add console build and missing window stubbing build * remove extra cypress files that are unnecessary to show example * replace stub with spy (it is not stubbing but only spying on call) - formatting from eslint * update test * rename file * add screenshot * Delete snapshots.js * Update README.md Co-Authored-By: Gleb Bahmutov <gleb.bahmutov@gmail.com> Co-authored-by: Jennifer Shehane <jennifer@cypress.io> Co-authored-by: Gleb Bahmutov <gleb.bahmutov@gmail.com>
1 parent 086855f commit b9f5ecb

File tree

9 files changed

+181
-83
lines changed

9 files changed

+181
-83
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ Recipe | Description
7272
[Stubbing `window.fetch`](./examples/stubbing-spying__window-fetch) | Use `cy.stub()` to control fetch requests
7373
[Stubbing methods called on `window`](./examples/stubbing-spying__window) | Use `cy.stub()` for methods called on `window`
7474
[Stubbing Google Analytics](./examples/stubbing-spying__google-analytics) | Use `cy.stub()` to test Google Analytics calls
75+
[Spying and stubbing methods on `console` object](./examples/stubbing-spying__console) | Use `cy.spy()` and `cy.stub()` on `console.log`
7576

7677
## Unit Testing
7778

circle.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ jobs:
148148
<<: *defaults
149149
stubbing-spying__google-analytics:
150150
<<: *defaults
151+
stubbing-spying__window:
152+
<<: *defaults
153+
stubbing-spying__console:
154+
<<: *defaults
151155
testing-dom__drag-drop:
152156
<<: *defaults
153157
testing-dom__form-interactions:
@@ -272,6 +276,12 @@ all_jobs: &all_jobs
272276
- stubbing-spying__window-fetch:
273277
requires:
274278
- build
279+
- stubbing-spying__console:
280+
requires:
281+
- build
282+
- stubbing-spying__window:
283+
requires:
284+
- build
275285
- stubbing-spying__google-analytics:
276286
requires:
277287
- build
@@ -347,6 +357,8 @@ all_jobs: &all_jobs
347357
- stubbing-spying__functions
348358
- stubbing-spying__window-fetch
349359
- stubbing-spying__google-analytics
360+
- stubbing-spying__window
361+
- stubbing-spying__console
350362
- testing-dom__drag-drop
351363
- testing-dom__form-interactions
352364
- testing-dom__hover-hidden-elements
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Stubbing Console
2+
3+
The spec file [cypress/integration/spec.js](cypress/integration/spec.js) shows how to spy on and stub methods of the `console` object.
4+
5+
![Test example](images/console-example.png)
6+
7+
## Learn more
8+
9+
- [`cy.spy()`](https://on.cypress.io/spy)
10+
- [`cy.stub()`](https://on.cypress.io/stub)
11+
- [`cy.visit()`](https://on.cypress.io/visit)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"pluginsFile": false,
3+
"supportFile": false,
4+
"fixturesFolder": false
5+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/// <reference types="cypress" />
2+
context('Console', () => {
3+
describe('spying on console.log', function () {
4+
beforeEach(function () {
5+
cy.visit('/index.html', {
6+
onBeforeLoad (win) {
7+
cy.spy(win.console, 'log').as('consoleLog')
8+
},
9+
})
10+
})
11+
12+
it('calls console.log with expected text', function () {
13+
cy.get('#console-log').click()
14+
cy.get('@consoleLog').should('be.calledWith', 'Hello World!')
15+
})
16+
})
17+
18+
describe('stubbing console.log', function () {
19+
let parameter
20+
21+
beforeEach(() => {
22+
cy.visit('/index.html', {
23+
onBeforeLoad (win) {
24+
cy.stub(win.console, 'log', (x) => {
25+
parameter = x
26+
})
27+
},
28+
})
29+
})
30+
31+
it('waits until stub has been executed and we get a value', function () {
32+
cy.get('#console-log').click()
33+
// We need to wait until the application calls "console.log"
34+
// and our local closure variable "parameter" gets a value.
35+
// Using "should(cb)" we force retrying the callback
36+
// until all assertions inside pass, see:
37+
// https://on.cypress.io/retry-ability
38+
// https://on.cypress.io/should#Function
39+
cy.should(() => {
40+
expect(parameter).to.equal('Hello World!')
41+
})
42+
})
43+
})
44+
})
Loading
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Stubbing Console</title>
6+
</head>
7+
<body>
8+
<h1>My Application</h1>
9+
<ul>
10+
<li>
11+
<button id="console-log">Log something</button>
12+
</li>
13+
</ul>
14+
15+
<script>
16+
var btn = document.getElementById('console-log')
17+
18+
btn.addEventListener('click', function(event) {
19+
// let's introduce an async behavior just for fun
20+
setTimeout(() => {
21+
console.log("Hello World!")
22+
}, 500)
23+
})
24+
</script>
25+
</body>
26+
</html>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "stubbing-window",
3+
"version": "1.0.0",
4+
"description": "",
5+
"scripts": {
6+
"start": "echo",
7+
"cypress:run": "../../node_modules/.bin/cypress run",
8+
"cypress:open": "../../node_modules/.bin/cypress open"
9+
}
10+
}

0 commit comments

Comments
 (0)