-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs(net-stubbing): deprecate cy.route(), remove experimentalNetworkStubbing #3323
Changes from 10 commits
440ce3c
c8bef8d
431e0c7
1deb947
839ae8b
0093454
1930af3
077eb7f
92a0380
0e45d8f
b588846
39028a3
86f24cb
896ff7d
0e7301a
cbd4daa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
{% note danger %} | ||
🚨 Please be aware that `cy.route()` and `cy.server()` only currently supports intercepting XMLHttpRequests. **Requests using the Fetch API and other types of network requests like page loads and `<script>` tags will not be intercepted by `cy.route()` and `cy.server()`.** You can polyfill `window.fetch` to spy on and stub requests using `cy.route()` and `cy.server()` by enabling the [experimental](https://on.cypress.io/experimental) feature `experimentalFetchPolyfill`. See {% issue 95 %} for more details and temporary workarounds. | ||
{% note warning %} | ||
⚠️ `cy.route()` and `cy.server()` only support intercepting XMLHttpRequests. Requests using the Fetch API and other types of network requests like page loads and `<script>` tags will not be intercepted by `cy.route()` and `cy.server()`. | ||
|
||
Cypress also has a new [`cy.http()`](/api/commands/http.html) command that supports requests using the Fetch API and other types of network requests like page loads. For more information, check out the [`cy.http()` documentation](/api/commands/http.html).{% endnote %} | ||
**To support requests using the Fetch API you can use one of the solutions below:** | ||
|
||
- Use [`cy.http()`](/api/commands/http.html) which supports requests using the Fetch API and other types of network requests like page loads. See [`cy.http()`](/api/commands/http.html). | ||
- Polyfill `window.fetch` to spy on and stub requests using `cy.route()` and `cy.server()` by enabling [`experimentalFetchPolyfill`](https://on.cypress.io/experimental). See {% issue 95 %} for more details and temporary workarounds. | ||
{% endnote %} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{% note warning %} | ||
⚠️ **`cy.server()` and `cy.route()` are deprecated in Cypress 6.0.0**. In a future release, support for `cy.server()` and `cy.route()` will be moved to a plugin. Consider using [`cy.http()`](/api/commands/http.html) instead. | ||
{% endnote %} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -137,7 +137,7 @@ cy.fixture('audio/sound.mp3', 'base64').then((mp3) => { | |
|
||
```javascript | ||
cy.fixture('users').then((json) => { | ||
cy.route('GET', '/users/**', json) | ||
cy.http('GET', '/users/**', json) | ||
}) | ||
``` | ||
|
||
|
@@ -147,51 +147,32 @@ cy.fixture('users').then((json) => { | |
{% url 'Check out our example recipe using `cy.fixture()` to bootstrap data for our application.' recipes#Server-Communication %} | ||
{% endnote %} | ||
|
||
### Using an alias to access a fixture | ||
|
||
You can make use of aliasing, {% url `.as()` as %}, instead of working directly with the yielded data. | ||
|
||
Using an alias provides the benefit of terseness and readability. It also makes it easier to access the data later in your tests. | ||
|
||
```javascript | ||
cy.fixture('users').as('usersJSON') | ||
cy.route('GET', '/users/**', '@usersJSON') | ||
|
||
// ...later on... | ||
|
||
cy.get('#email').then(() => { | ||
// we have access to this.usersJSON since it was aliased | ||
this.usersJSON | ||
}) | ||
``` | ||
|
||
### Modifying fixture data before using it | ||
|
||
You can modify fixture data directly before passing it along to a route. | ||
|
||
```javascript | ||
cy.fixture('user').then((user) => { | ||
user.firstName = 'Jane' | ||
cy.route('GET', '/users/1', user).as('getUser') | ||
cy.http('GET', '/users/1', user).as('getUser') | ||
}) | ||
|
||
cy.visit('/users') | ||
cy.wait('@getUser').then((xhr) => { | ||
expect(xhr.requestBody.firstName).to.eq('Jane') | ||
cy.wait('@getUser').then(({ request }) => { | ||
expect(request.body.firstName).to.eq('Jane') | ||
Comment on lines
+161
to
+162
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This returns undefined, when trying to drill into the request.body.prop because it('cy.http() - route responses to matching requests', () => {
cy.visit('https://example.cypress.io/commands/network-requests')
cy.http('GET', '**/comments/*').as('getComment')
cy.get('.network-btn').click()
cy.wait('@getComment').then(({ response }) => {
expect(response.body.name).to.eq('id labore ex et quam laborum')
})
}) |
||
}) | ||
``` | ||
|
||
# Notes | ||
|
||
## Shortcuts | ||
|
||
### Using `fixture` or `fx` shortcuts | ||
### Using the `fixture` `StaticResponse` property | ||
|
||
Fixtures can also be referenced directly without using the `.fixture()` command by using the special keywords: `fixture:` or `fx:` within {% url `cy.route()` route %}. | ||
Fixtures can also be referenced directly without using the `.fixture()` command by using the special property `fixture` on the {% url `cy.http()` http %} `StaticResponse` object. | ||
|
||
```javascript | ||
cy.route('GET', '/users/**', 'fixture:users') // this works | ||
cy.route('GET', '/users/**', 'fx:users') // this also works | ||
cy.http('GET', '/users/**', { fixture: 'users' }) | ||
``` | ||
|
||
## Validation | ||
|
@@ -265,7 +246,7 @@ describe('User page', () => { | |
|
||
# See also | ||
|
||
- {% url `cy.route()` route %} | ||
- {% url `cy.http()` http %} | ||
- {% url `.then()` then %} | ||
- {% url 'Recipe: Bootstrapping App Test Data' recipes#Server-Communication %} | ||
- {% url 'Fixtures' https://github.com/cypress-io/testing-workshop-cypress#fixtures %} section of the Cypress Testing Workshop |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -203,13 +203,12 @@ cy.window().its('evilProp').should('not.exist') | |
***Get `responseBody` of aliased route*** | ||
|
||
```javascript | ||
cy.server() | ||
cy.route(/comments/, 'fixture:comments.json').as('getComments') | ||
cy.http(/comments/, { fixture: 'comments.json' }).as('getComments') | ||
cy.get('#fetch-comments').click() | ||
cy.wait('@getComments').its('responseBody').should('deep.eq', [ | ||
cy.wait('@getComments').its('response.body').should('deep.eq', JSON.stringify([ | ||
{ id: 1, comment: 'hi' }, | ||
{ id: 2, comment: 'there' } | ||
]) | ||
])) | ||
Comment on lines
+208
to
+211
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This example also doesn't work (same as it('cy.http() - route responses to matching requests', () => {
cy.visit('https://example.cypress.io/commands/network-requests')
cy.http('GET', '**/comments/*').as('getComment')
cy.get('.network-btn').click()
cy.wait('@getComment').its('response.body').should('deep.eq', JSON.stringify([
{
postId: 1,
id: 1,
name: 'id labore ex et quam laborum',
email: '[email protected]',
body: 'laudantium enim quasi est quidem magnam voluptate ipsam eos\ntempora quo necessitatibus\ndolor quam autem quasi\nreiciendis et nam sapiente accusantium',
},
])) |
||
``` | ||
|
||
The commands above will display in the Command Log as: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This example doesn't work, so is not the equivalent of the previous version. The assertion seems to fail now because there are newlines in the body. (I'm still reviewing but this is one example of a change needed that I've run into)