Full stack developer building things to make life a little easier. Huge fan of JavaScript, React, Node.js, and testing my code. • twitter.com/ZakLaughton • zaklaughton.dev
Sure! Mocking the post request is exactly the same as mocking the get request, except you'll want to mock the post method:
axios.post.mockResolvedValue({data:[{userId:1,id:1,title:'My First Album'},{userId:1,id:2,title:'Album: The Sequel'}]});
In this case, you'll want the mocked value to be whatever you get back from the real post response. I've found console.log()-ing the response to a real post request a good way to get a response to copy into the code.
If you want the mock to return a dynamic value based on the input, you could instead use axios.post.mockImplementation(...) This will allow you to create a custom function to build a response based on the input given to axios.post(). You can see the full documentation for mockImplementation()here.
I hope this helps!
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Sure! Mocking the post request is exactly the same as mocking the get request, except you'll want to mock the
post
method:In this case, you'll want the mocked value to be whatever you get back from the real post response. I've found
console.log()
-ing the response to a real post request a good way to get a response to copy into the code.If you want the mock to return a dynamic value based on the input, you could instead use
axios.post.mockImplementation(...)
This will allow you to create a custom function to build a response based on the input given toaxios.post()
. You can see the full documentation formockImplementation()
here.I hope this helps!