6

Environment:

NodeJS 8.1.2
axios 0.16.2
axios-mock-adapter 1.9.0

A test POST API call utilising JSONPlaceholder as follow:

const expect = require('chai').expect
const MockAdapter = require('axios-mock-adapter')

// Bootstrapping

const PlaceholderApp = {
  createComment: function (author, email, message) {
    const options = {
      method: 'post',
      url: 'https://jsonplaceholder.typicode.com/comments',
      data: {
        name: author,
        email: email,
        body: message,
      }
    }
    return axios(options)
  }
}

// Mock Adapter

const mockHttpClient = new MockAdapter(axios, { delayResponse: 50 })
// mockHttpClient.onPost(/(\/comments)/i, { name: 'author A', email: 'authorA@test.com', body: 'test comment' }).reply(526) // WORKS!
mockHttpClient.onPost(/(\/comments)/i, { email: 'authorA@test.com' }).reply(527) //This won't work. Would like to have something like this to work tho...
mockHttpClient.onAny().passThrough()

// Test cases

describe('PlaceholderApp.createComment', () => {
  it("should fail due to mock...", (resolve) => {
    PlaceholderApp.createComment('author A', 'authorA@test.com', 'test comment')
      .then((res) => {
        resolve()
      })
      .catch((err) => {
        resolve(err)
      })
  })
})

I would like to know if there's a way to be able to match partial of POST data?

Trav L
  • 14,732
  • 6
  • 30
  • 39
  • This is actually related to my [other question](https://stackoverflow.com/questions/46637254/is-it-possible-to-apply-passthrough-within-a-mock-reply-using-axios-mock-adapt) that was meant to be a workaround to this hurdle, but has problem of its own... – Trav L Oct 09 '17 at 02:47
  • What do you mean by _"match partial of `POST` data"_? What are you trying to achieve? – guest271314 Oct 09 '17 at 02:48
  • I'm able to to have my mock applied to "exact" match. In my example: `{ name: 'author A', email: 'authorA@test.com', body: 'test comment' }`. The question is, am I able to apply wildcards and say "apply mock on all POST requests that has `email == 'authorA@test.com'`? – Trav L Oct 09 '17 at 03:25

2 Answers2

5

You can catch all POST requests to a specific URL and then manually match your conditions within the reply callback and passThrough if conditions don't meet, we can passThrough within the reply callback by passing the call to the originalAdapter like answered in your other question.

mockHttpClient.onPost(/(\/comments)/i).reply((config) => {
  const data = JSON.parse(config.data);

  if (data.email == 'authorA@test.com') {
    return [200, 'response'];
  } else {
    // passThrough
    return mockHttpClient.originalAdapter(config);
  }
})

NOTE : you can have multiple full match data to the same URL if provided data is different, but for our own partial match implementation you can't add another request to the same URL and method, you will have to add the logic to match all wanted cases to one request.

Mohamed Gharib
  • 2,387
  • 1
  • 15
  • 16
1

Since version v1.18.0 (22 Mar 2020) asymmetricMatch is supported.

mockHttpClient.onPost(/(\/comments)/i, {
  asymmetricMatch: (actual) => actual.email === 'authorA@test.com'
}).reply(527)
Martin
  • 5,714
  • 2
  • 21
  • 41