Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching axios POST request with moxios

Is it possible to use moxios to mock a reply to a POST request that will match not just by URL but also by the POST body? Inspecting the body afterwards would work for me too.

This is what I am doing now. As far as I know there are no method specific stubbing methods:

describe('createCode', function () {
    it('should create new code', function () {
        moxios.stubRequest(process.env.API_URL + '/games/GM01/codes', {
            status: 200
        })
    })
})
like image 956
MartinTeeVarga Avatar asked Apr 17 '17 11:04

MartinTeeVarga


People also ask

How to make post requests with Axios?

POST Requests with Axios Sep 17, 2019 The easiest way to make a POST request with Axios is the axios.post () function. The first parameter to axios.post () is the URL, and the 2nd is the HTTP request body.

What is Axios and how do I use it?

Axios offers methods for all the HTTP verbs, which are less popular but still used: and a method to get the HTTP headers of a request, discarding the body: Another really powerful feature of Axios that cannot be understated is the ability to execute multiple requests in parallel, simply provide an array argument to axios.all.

How to use Axios with npm or yarn?

Run the following command to install the axios with npm or yarn CLI. 2. POST request with a JSON body using axios Let’s use the following syntax for the POST request. We have used the post method of the axios and attached the JSON body with the request. 3. POST request with HTTP header We can use the third parameter to pass the HTTP headers.

What is the 2nd parameter in Axios post?

The first parameter to axios.post () is the URL, and the 2nd is the HTTP request body. By default, if the 2nd parameter to axios.post () is an object, Axios serializes the object to JSON using the JSON.stringify () function .


1 Answers

There is a way to inspect the last axios request using moxios:

let request = moxios.requests.mostRecent()
expect(request.config.method).to.equal('post')
expect(JSON.parse(request.config.data)).to.deep.equal(code)

The config object is what's being passed in axios, data is the body of the request.

like image 94
MartinTeeVarga Avatar answered Oct 12 '22 14:10

MartinTeeVarga