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.

How to create a mock per endpoint

See original GitHub issue

Hey!

Is there a way to mock a endpoint based on what fetch was called with?

Example:

fetch.mockResponseOnce(
  "http://api-lsdi.com/api/cart/1", 
  JSON.stringify(sampleResponse)
);

Basically the intention of this is to say mock this specific endpoint with this payload

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:6
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
yinzaracommented, Jul 28, 2020

fetch.mockResponse takes in either the response or a function that accepts the request and generates the response. So you can do exactly like you say by writing a switch statement in the function. Just see the docs or the TypeScript definition.

fetch.mockResponse(request => {
   if (request.url.startsWith('http://foo.bar')) {
     return /* a response */
   } else {
     return /* default response */
   }
})
1reaction
jefflaucommented, Jul 23, 2019

Currently there isn’t. I’m trying to work through a PR by @yinzara to get this done. You can check out the code here and see if it would fit your usecase:

https://github.com/jefflau/jest-fetch-mock/pull/123

Read more comments on GitHub >

github_iconTop Results From Across the Web

Create Rest Mock API in Seconds With These 9 Tools
Here, we will know some of the best tools to create REST mock API. ... In this platform, you can create, get, update,...
Read more >
Setting up mock servers | Postman Learning Center
Select Mock Servers in the sidebar, then select +. ... Select an existing collection, or create a new collection and add an initial...
Read more >
How To Create And Use Mock Apis In Minutes | Beeceptor
Mock API is a simple tool that lets you easily mock up APIs, generate custom data, and preform operations on it using RESTful...
Read more >
How to make a mock API server in JavaScript - Snyk
Development: Mock API servers temporarily remove the dependency between the frontend and backend teams, allowing them to work, develop, and test ...
Read more >
Docs - mockAPI
1. Create new mock project. New project form · 2. Add resource. New resource form. NOTE: Resource name maps to the endpoint url....
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 Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found