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 ignore queryString (params) when doing a mock

See original GitHub issue

tried:

mock.onGet( apiPath('/Facility') ).reply(config=>{
    console.log(config);
    return [
        200,newItem(1)
    ]
})
mock.onGet( apiPath('/Facility'),{ params: { facilityId: /\d+/ } } ).reply(config=>{
    console.log(config);
    return [
        200,newItem(1)
    ]
})

apiPath is just a function that returns a full apiUrl, whether it be dev, staging or production. any help would be appreciated

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:6
  • Comments:5

github_iconTop GitHub Comments

2reactions
BrendanBallcommented, Jul 9, 2018

I’m trying to mock a GET request without specifying the params that it should match on.

According to the example docs:

// Mock any GET request to /users
// arguments for reply are (status, data, headers)
mock.onGet('/users').reply(200, {
  users: [
    { id: 1, name: 'John Smith' }
  ]
});

You should be able to mock just a URL without specifying params and this should match on all GET requests with whatever query params they send in. This however is not the case. If a request sends in query params then this mock doesn’t match. Is this the same problem you’re having @r3wt ?

0reactions
AregShahbaziancommented, Jan 20, 2022

The solution for this problem is to use regex @r3wt in your case that would look like this:

mock.onGet( apiPath(/\/Facility/) ).reply(config=>{
    return [
        200,newItem(1)
    ]
})

This would match request to /Facility and also /Facility?x=1&y=2 etc

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to tell wiremock to ignore query strings parameters in ...
What I'm facing is that any query parameter appended in the request URL breaks my mock, so a call to http://mockurl/myurl?query=string gives me...
Read more >
How can I delete a query string parameter in JavaScript?
This doesn't work if you have two query strings and remove the first. The passed in url becomes invalid because the ? is...
Read more >
Ignoring Query String Parameters - SmartBear Support
To do this, enable the Ignore dynamic URL parameters property of your project and specify the list of parameters to ignore. For example,...
Read more >
Query parameters - Recipes - Mock Service Worker Docs
In order to access a query parameter of the intercepted request, use the searchParams property on the req.url instance.
Read more >
Best Practice: Caching Everything While Ignoring Query Strings
Enterprise customers can cache any type of static content, like HTML files, and ignore query strings in the resource URL by using Custom......
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