Since this is one of the top results when searching for "axios mock adaptor with token in path", I'll just point out that the GitHub README for axios mock adaptor has the solution. https://github.com/ctimmerm/axios-mock-adapter
You can pass a Regex to .onGet, so for your case -
const pathRegex = new Regexp(`${apiUrl}\/invoice\/*`);
mock
.onGet(pathRegex).reply
...etc.etc.
That should pick up your calls to /invoice/${id}
npm
npmjs.com › package › axios-mock-adapter
axios-mock-adapter - npm
October 9, 2024 - mock.onPost("/foo").reply(function ... id can be grabbed from config.url return [200, {}]; }); ... const usersUri = "/users"; const url = new RegExp(`${usersUri}/*`); mock.onGet(url).reply(200, users);...
» npm install axios-mock-adapter
Published Oct 09, 2024
Version 2.1.0
Author Colin Timmermans
GitHub
github.com › ctimmerm › axios-mock-adapter › issues › 180
Using regex in config parameters · Issue #180 · ctimmerm/axios-mock-adapter
November 30, 2018 - Using the library, there is a possibility to have Regex in URL. But what about the Regex in config params? For example, I could use something like this: mockAdapter .onPost(/^\/api\/es\/search/, { size: /d+/, // Any number I want query: { 'query' } }) .reply(200, {});
Author EugeneHerasymchuk
Stack Overflow
stackoverflow.com › questions › 51424462 › regex-in-axios-mock-api-adapter-dont-work
Regex in Axios mock api adapter don't work
July 19, 2018 - The problem is that even when calling the api with the id in the url, the adpater triggered is always the last one and not the first one with the regex to catch the source id. ... Try /dmd\.mocked\/api\/sources$/ as the second one.
GitHub
github.com › ctimmerm › axios-mock-adapter › issues › 54
Missleading regex example · Issue #54 · ctimmerm/axios-mock-adapter
May 17, 2017 - When using a regex expression, requests don't get caught by the adapter in some cases · This works : const axios = axios.create({ baseURL: 'https://apiurl.com' }) axios.get('/foo/123').then(function (data) { console.log(data) }) mock.onGet(/\/foo\/\d+/).reply(function(config) { return [200, {}]; }) This doesn't : const axios = axios.create({ baseURL: 'https://apiurl.com/' }) axios.get('/foo/123').then(function (data) { console.log(data) }) mock.onGet(/\/foo\/\d+/).reply(function(config) { return [200, {}]; }) It is due to the trailing slash in the baseURL.
GitHub
github.com › ctimmerm › axios-mock-adapter
GitHub - ctimmerm/axios-mock-adapter: Axios adapter that allows to easily mock requests
mock.onPost("/foo").reply(function ... id can be grabbed from config.url return [200, {}]; }); ... const usersUri = "/users"; const url = new RegExp(`${usersUri}/*`); mock.onGet(url).reply(200, users);...
Starred by 3.5K users
Forked by 255 users
Languages JavaScript 96.2% | TypeScript 3.8% | JavaScript 96.2% | TypeScript 3.8%
UNPKG
unpkg.com › browse › axios-mock-adapter@1.18.1 › README.md
axios-mock-adapter
Mock a low level network error ... that returns an axios request, essentially mocking a redirect ```js mock.onPost("/foo").reply(function (config) { return axios.get("/bar"); }); ``` Using a regex ```js mock.onGet(/\/users\/\d+/).reply(function (config) { // the actual ...
Top answer 1 of 3
7
Since this is one of the top results when searching for "axios mock adaptor with token in path", I'll just point out that the GitHub README for axios mock adaptor has the solution. https://github.com/ctimmerm/axios-mock-adapter
You can pass a Regex to .onGet, so for your case -
const pathRegex = new Regexp(`${apiUrl}\/invoice\/*`);
mock
.onGet(pathRegex).reply
...etc.etc.
That should pick up your calls to /invoice/${id}
2 of 3
5
For anyone who also wants to get the js object from dynamic query string of the url
mock.onGet(/api\/test\/?.*/).reply((config) => {
console.log(config.url, parseQueryString(config.url));
return [202, []];
});
function parseQueryString(url: string) {
const queryString = url.replace(/.*\?/, '');
if (queryString === url || !queryString) {
return null;
}
const urlParams = new URLSearchParams(queryString);
const result = {};
urlParams.forEach((val, key) => {
if (result.hasOwnProperty(key)) {
result[key] = [result[key], val];
} else {
result[key] = val;
}
});
return result;
}
Result
axios.get("api/test");
// api/test
// null
axios.get("api/test?foo=1&bar=two");
// api/test?foo=1&bar=two
// {foo: "1", bar: "two"}
axios.get("api/test?foo=FOO&bar=two&baz=100");
// api/test?foo=FOO&bar=two&baz=100
// {foo: "FOO", bar: "two", baz: "100"}
axios.get("api/test?foo=FOO&bar=two&foo=loo");
// api/test?foo=FOO&bar=two&foo=loo
// {foo: ["FOO", "loo"], bar: "two"}
Live Demo
Stack Overflow
stackoverflow.com › questions › tagged › axios-mock-adapter
Recently Active 'axios-mock-adapter' Questions - Stack Overflow
I am trying to use axios-mock-adapter to mock the response I get when calling the Spotify API to request an access token. Here is the function that does just that...
Tabnine
tabnine.com › home page › code › javascript › axios-mock-adapter
axios-mock-adapter JavaScript and Node.js code examples | Tabnine
before(() => { server = new MockAdapter(axios); server.onGet('/api/traces/searchableKeys').reply(200, stubSearchableKeys); server.onGet('/api/alerts/root-service/unhealthyCount').reply(200, 0); server.onGet('/api/services').reply(200, stubServices); server.onGet('/api/operations?serviceName=root-service').reply(200, stubOperations); server.onGet(/^\/api\/traces\?/g).reply(200, stubTraces); server.onGet(/^\/api\/traces\/timeline\?/g).reply(200, []); });
GitHub
github.com › ctimmerm › axios-mock-adapter › blob › master › types › index.d.ts
axios-mock-adapter/types/index.d.ts at master · ctimmerm/axios-mock-adapter
Axios adapter that allows to easily mock requests. Contribute to ctimmerm/axios-mock-adapter development by creating an account on GitHub.
Author ctimmerm
GitHub
github.com › ctimmerm › axios-mock-adapter › issues › 18
How to match against get params? · Issue #18 · ctimmerm/axios-mock-adapter
August 31, 2016 - mock.onGet('/api', { name: 'Bob' }).reply(200, 'Hello') mock.onGet('/api?name=Bob').reply(200, 'Hello')
Author chipit24
Blogger
ggtcf.blogspot.com › 2019 › 03 › how-to-work-with-path-parameters-in.html
How to work with path parameters in Axios Mock adapter2019 Community Moderator ElectionMock axios with axios-mock-adapter get undefined respHow to test post calls in axios with jest and axios-mock-adapterThe reason of usage axios-mock-adapterHow to mocking a http request with route params using axios-mock-adapterUse axios and axios-mock-adapterPassing path parameters in axiosWrite test axios-mock-adapter with axios.create()Regex in Axios mock api adapter don't workMock axios using axios interceptorsaxios-mo
March 13, 2019 - 2019 Community Moderator ElectionMock axios with axios-mock-adapter get undefined respHow to test post calls in axios with jest and axios-mock-adapterThe reason of usage axios-mock-adapterHow to mocking a http request with route params using axios-mock-adapterUse axios and axios-mock-adapterPassing path parameters in axiosWrite test axios-mock-adapter with axios.create()Regex in Axios mock api adapter don't workMock axios using axios interceptorsaxios-mock-adapter doesn't seem to be completing with promises
CloudDefense.ai
clouddefense.ai › code › javascript › example › axios-mock-adapter
Top 10 Examples of axios-mock-adapter code in Javascript
beforeEach((done) => { loadFixtures(FIXTURE_PATH); mockData = getJSONFixture(FIXTURE_PATH); // put the fixture in DOM as the component expects document.body.innerHTML = `<div id="js-issuable-app"></div>`; document.getElementById('js-issuable-app').dataset.initial = JSON.stringify(mockData); mock = new MockAdapter(axios); mock.onGet(`${API_ENDPOINT}?per_page=100`).reply(200, mockData, { 'x-total': 2 }); wrapper = mount(localVue.extend(RelatedMergeRequests), { localVue, store: createStore(), propsData: { endpoint: API_ENDPOINT, projectNamespace: 'gitlab-org', projectPath: 'gitlab-ce', }, }); setImmediate(done); });
Npm
npm.io › package › axios-mock-adapter
Axios-mock-adapter NPM | npm.io
mock.onPost("/foo").reply(function (config) { return axios.get("/bar"); }); ... mock.onGet(/\/users\/\d+/).reply(function (config) { // the actual id can be grabbed from config.url return [200, {}]; }); ... const usersUri = "/users"; const url = new RegExp(`${usersUri}/*`); mock.onGet(url)...