Search code examples
javascriptaxiosmoxios

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
        })
    })
})

Solution

  • 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.