Problem with use of Axios mock adapter GET

810 Views Asked by At

I try to use Axios mock adapter to simulate GET to Web API, but it does not work.

The api url looks like something like that :

`/api/forms/${guid}`

I try using Regex, but doesn't work (probably something with the d+):

mock.onGet('/\/api\/forms\/\d+/').reply((config) => {
    // the actual id can be grabbed from config.url
    console.log(config);
    return [200, {}];
});

This work:

mock.onGet('/users').reply((config) => {
    // the actual id can be grabbed from config.url
    console.log(config);
    return [200, {}];
});
1

There are 1 best solutions below

1
On BEST ANSWER

Regex should not be quoted in JavaScript.

Try this:

mock.onGet(/\/api\/forms\/\d+/).reply((config) => {