I'm having a problem to mock a HTTP request from my Node.js code using nock.
To make the request I'm using the axios library:
axios.get(`https://api.spoonacular.com/recipes/${id}/information?apiKey=${process.env.MY_SPOONACULAR_KEY}`);
id is just an integer like 1131030.
I'm using nock this way:
nock('https://api.spoonacular.com')
.persist()
.log(console.log)
.get('/recipes\/\d*\/information$/')
.query(true)
.reply(200, {answer: 'any'});
I'm using query(true) to match any query string. The regex in the get is to match any possible id on the request.
However it is not working, and from the nock log I'm getting the following:
matching https://api.spoonacular.com:443/recipes/1131030/information to GET https://api.spoonacular.com:443/recipes/d*/information$/: false
You're passing a string, not a regex, to the .get()
method. You need to remove the quotes.
.get(/recipes\/\d*\/information$/)
Also, using .log()
is not preferred to when trying to figure out why Nock is not matching. Use DEBUG instead to get more information. https://github.com/nock/nock#debugging