Skip to content

Axios mocker

Erwan Rouchet requested to merge axios-history into master

Closes #292 (closed)

This adds a FakeAxios test helper: a wrapper around the MockAdapter from axios-mock-adapter. The implementation is pretty complex due to issues with extending the MockAdapter, but it adds two ways to ensure #291 (closed) never ever occurs again.

  • mock.reset() runs an assertion on all called mock handlers to ensure they were actually called. This would detect that POST /classifications/ is not called. This assertion is displayed pretty explicitly:

    2) store
         elementsv2
           actions
             "after each" hook for "creates an element with a classification and no parent":
    
        AssertionError: Some response mocks were not called
        + expected - actual
    
                 "zone": [null]
               }
               [undefined]
             ]
        +    [
        +      "/classifications/"
        +      [undefined]
        +      [undefined]
        +      200
        +      {
        +        "id": "classification2"
        +        "name": "The best classification"
        +      }
        +      [undefined]
        +    ]
           ]
           "put": []
         }
  • Requests in mock.history have their data attribute parsed back as JSON if the Content-Type is properly set, to make assertions on request bodies simpler.

  • mock.history.all is an Array regrouping all requests, without depending on their methods, in the order they were called. This allows to use a manner similar to the store.history to check for every single request at once. However, requests are rather complex with attributes we often do not really care about (such as transformRequest, transformResponse, timeout, validateStatus, xsrfCookieName, …), but some tests still need them. For this reason, I went with using _.pick to select the attributes to assert on:

    import { pick } from 'lodash'
    assert.deepStrictEqual(mock.history.all.map(obj => pick(obj, ['url', 'method', 'data'])), [
      { method: 'get', url: '/elements/', data: undefined },
      { method: 'post', url: '/corpus/', data: { name: 'something' } }
    ])

    This makes assertion error messages easy to read with diffs.

Edited by Erwan Rouchet

Merge request reports

Loading