question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow Dynamic Stubbing and Responses

See original GitHub issue

Current 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:

  1. 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
}
  1. 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:closed
  • Created 6 years ago
  • Reactions:104
  • Comments:41 (10 by maintainers)

github_iconTop GitHub Comments

52reactions
alexlee-devcommented, Mar 15, 2019

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.

35reactions
brian-manncommented, Nov 2, 2017

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found