Skip to content
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

Add more information for onNoMatch: throwException #310

Open
fanantoxa opened this issue Aug 3, 2021 · 1 comment
Open

Add more information for onNoMatch: throwException #310

fanantoxa opened this issue Aug 3, 2021 · 1 comment

Comments

@fanantoxa
Copy link

Domain knowlage

With last update there is a way to make axios-mock-adapter to throw an error when unhanded request happen.

const mock = new MockAdapter(axios, { onNoMatch: "throwException" });

So when when I make call I see error:

axios.get("/user/posts");

// Exception message on console:
//
// Could not find mock for: 
// {
//   "method": "get",
//   "url": "http://localhost/user/posts"
// }

Problem

There case when component makes a request to the same API endpoint but with different query param.
For example pagination.
I can setup specific mock for so it will mock only first request but not the second
For example:

const axios = require('axios')
const MockAdapter = require('axios-mock-adapter')

const mock = new MockAdapter(axios, { onNoMatch: 'throwException' })

// Mock GET request to /users when param `searchText` is 'John'
// arguments for reply are (status, data, headers)
mock.onGet('/users', { params: { searchText: 'John' } }).reply(200, {});


it('runs', async () => {
  await axios
    .get('/users', { params: { searchText: 'John' } })
    .then((response) => { console.log(response.data) })

  await axios
    .get('/users', { params: { searchText: 'Arnold' } })
    .then((response) => { console.log(response.data) })

  expect(true).toBe(true)
})

With this example I'll get error that looks like this:

Could not find mock for: 
    {
      "method": "get",
      "url": "/users"
    }

This is quite small amount of information for developer and it may take awhile to understand that the is another call to the same endpoint that mocked but with a bit different params.

Suggested solution

Add more information into the error.
I'd suggest params and request body (POST, PUT, etc requests may have slight difference in body that will lead to same problem)

@sarah-i-nbr
Copy link

I have the same problem with POST

How did you make it work @fanantoxa ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants