Skip to content
This repository was archived by the owner on Dec 12, 2020. It is now read-only.

Commit c4f5e10

Browse files
authored
feat: add experimental fetch polyfill support (#370)
for https://www.cypress.io/blog/2020/06/29/experimental-fetch-polyfill/
1 parent 547e0c2 commit c4f5e10

File tree

8 files changed

+99
-2
lines changed

8 files changed

+99
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,7 @@ Spec | Description
531531
[i18n](cypress/component/advanced/i18n) | Testing component that uses [Vue I18n](https://kazupon.github.io/vue-i18n/) plugin
532532
[mocking-axios](cypress/component/advanced/mocking-axios) | Mocking 3rd party CommonJS modules like `axios`
533533
[mocking-fetch](cypress/component/advanced/mocking-fetch) | Mocking `window.fetch` to stub responses and test the UI
534+
[fetch-polyfill](ypress/component/advanced/fetch-polyfill) | Using experimental `fetch` polyfill to spy on / stub those Ajax requests using regular Cypress network methods
534535
[mocking-components](cypress/component/advanced/mocking-components) | Mocking locally registered child components during tests
535536
[mocking-imports](cypress/component/advanced/mocking-imports) | Stub ES6 imports from the tests
536537
[render-functions](cypress/component/advanced/render-functions) | Mounting components with a [render function](https://www.tutorialandexample.com/vue-js-render-functions/)

cypress.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
"video": false,
55
"projectId": "134ej7",
66
"testFiles": "**/*spec.js",
7-
"experimentalComponentTesting": true
7+
"experimentalComponentTesting": true,
8+
"experimentalFetchPolyfill": true
89
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# fetch polyfill example
2+
3+
[Users.vue](Users.vue) is using `fetch` to retrieve the users from the server. We can use [experimental fetch polyfill](https://www.cypress.io/blog/2020/06/29/experimental-fetch-polyfill/) and spy on / stub network requests, see [Users.spec.js](Users.spec.js)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/// <reference types="cypress" />
2+
import { mount } from 'cypress-vue-unit-test'
3+
import Users from './Users.vue'
4+
5+
describe('Fetching users with polyfill', () => {
6+
it('renders real data', () => {
7+
// no mocking
8+
mount(Users)
9+
cy.get('.user').should('have.length', 3)
10+
})
11+
12+
it('can spy on the fetch requests', () => {
13+
cy.server()
14+
cy.route('/users?_limit=3').as('users')
15+
mount(Users)
16+
cy.wait('@users')
17+
.its('responseBody.length')
18+
.then((length) => {
19+
cy.get('.user').should('have.length', length)
20+
})
21+
})
22+
23+
it('shows loading UI while fetch is happening', () => {
24+
cy.server()
25+
cy.route({
26+
url: '/users?_limit=3',
27+
response: 'fixture:users',
28+
delay: 1000,
29+
})
30+
31+
mount(Users)
32+
cy.get('.loading').should('be.visible')
33+
cy.get('.loading').should('not.exist')
34+
35+
cy.get('.user').should('have.length', 2)
36+
})
37+
})
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<template>
2+
<div>
3+
<h1>Users</h1>
4+
<ul v-if="users && users.length">
5+
<li class="user" v-for="user of users" v-bind:key="user.id">
6+
<p><strong>{{user.id}}</strong> - {{user.name}}</p>
7+
</li>
8+
</ul>
9+
<p class="loading" v-else>Loading...</p>
10+
</div>
11+
</template>
12+
13+
<script>
14+
export default {
15+
data() {
16+
return {
17+
users: []
18+
}
19+
},
20+
21+
created() {
22+
fetch('https://jsonplaceholder.cypress.io/users?_limit=3')
23+
.then(response => response.json())
24+
.then(list => {
25+
this.users = list
26+
})
27+
}
28+
}
29+
</script>

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
"babel-plugin-istanbul": "6.0.0",
9494
"common-tags": "1.8.0",
9595
"debug": "4.1.1",
96-
"find-webpack": "2.0.0"
96+
"find-webpack": "2.0.0",
97+
"unfetch": "4.1.0"
9798
}
9899
}

src/support.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint-env mocha */
2+
import unfetch from 'unfetch'
23
require('@cypress/code-coverage/support')
34

45
/** Initialize an empty document with root element */
@@ -20,6 +21,25 @@ function renderTestingPlatform() {
2021
return cy.get('#cypress-jsdom', { log: false })
2122
}
2223

24+
/**
25+
* Replaces window.fetch with a polyfill based on XMLHttpRequest
26+
* that Cypress can spy on and stub
27+
* @see https://www.cypress.io/blog/2020/06/29/experimental-fetch-polyfill/
28+
*/
29+
function polyfillFetchIfNeeded() {
30+
// @ts-ignore
31+
if (Cypress.config('experimentalFetchPolyfill')) {
32+
// @ts-ignore
33+
if (!cy.state('fetchPolyfilled')) {
34+
delete window.fetch
35+
window.fetch = unfetch
36+
// @ts-ignore
37+
cy.state('fetchPolyfilled', true)
38+
}
39+
}
40+
}
41+
2342
beforeEach(() => {
2443
renderTestingPlatform()
44+
polyfillFetchIfNeeded()
2545
})

0 commit comments

Comments
 (0)