Search code examples
javascriptaxiosaxios-mock-adapter

It is possible to use a wildcard for the URL when mocking axios?


I would like to know if it is possible to mock certain URLs using wildcards, e.g. all URLs that start with /auth. Can I do something like /auth*?


Solution

  • Yes, per the docs you can use a regex as the URL. One of their examples is similar to your use case:

    Using variables in regex

    const usersUri = '/users';
    const url = new RegExp(`${usersUri}/*`);
    
    mock.onGet(url).reply(200, users);