Allow Dynamic Stubbing and Responses
See original GitHub issueCurrent behavior:
Currently, all mocked responses need to know the response data upfront.
cy
.server()
.route("/users", [{id: 1, name: "Kyle"}])
The stubbed route can also include dynamic routes, but the response will always be static upon the route being initialized.
cy
.server()
.route(/api\.server\.com, SingleFixture)
Expected behavior:
- The ability to setup fixture responses dynamically. Perhaps using some sort of controller function.
cy
.server()
.route(/api\.server\.com/, FixtureController)
function FixtureController(request, response) {
if (request.url.contains('users') {
response.body = [{id: 1, name: "Pat"}]
return request
}
if (request.url.contains('login') {
response.body = { token: '123abc' }
return request
}
- The ability to match all routes, but conditionally allow some responses to be stubbed, and others to request real data. In the above, perhaps if
response
wasn’t set, the request would continue to the server.
Test code:
function FixtureController(request, response) {
if (request.url === 'api.server.com/users') {
response.body = { hello: "world" }
return response
}
}
describe(`Dynamic Stubbing`, () => {
beforeEach(() => {
cy.server().route(/api\.server\.com/, FixtureController)
})
it('should stub a route dynamically', () => {
cy.request('http://api.server.com/users').then(function(response) {
expect(response.body).to.have.property("hello", "world")
})
})
})
- Operating System: OSX
- Cypress Version: 0.19.2
- Browser/Browser Version: Chrome Latest
Issue Analytics
- State:
- Created 6 years ago
- Reactions:104
- Comments:41 (10 by maintainers)
Top Results From Across the Web
How to use stub multiple API requests dynamically in Cypress
We have added a new command to mock request to our endpoint dynamically on-demand using xhook (library to intercept and modify XHR request...
Read more >Dynamic stub response - Google Groups
I want to create a imposter in such a way that I can dynamically change the ID ... which allows you to tokenize...
Read more >Returning stubbed HTTP responses to specific requests
A core feature of WireMock is the ability to return canned HTTP responses for requests matching criteria. These are described in detail in...
Read more >Dynamic XHR responses recording & stubbing with Cypress
One powerful feature in Cypress is the ability to stub XHR responses. This means that when your app fetches data from an API,...
Read more >intercept - Cypress Documentation
spying, dynamic stubbing, request modification, etc. cy.intercept(url, ... you'll have access to the entire request-response session, enabling you to modify ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
So we are almost 2 years after this problem was initially brought up. I see people have written some pseudo fixes for different problems, but none of them seem like a catch all (at least for XHR).
Does anyone from Cypress have an update on this? @brian-mann ?
Love Cypress, and I’d like to be able to use it more dynamically.
@ianwalter we are not likely going to change or update any of the existing route / wait API’s because ultimately the whole thing needs to be rewritten and implemented in a completely different fashion.
Here’s an issue describing it: https://github.com/cypress-io/cypress/issues/687
When we rewrite those API’s we will take this use case into account.