5

I am making request with axios.js library and part of my integration test suite are expectations on data send in body of POST request, done by axios, to see if it's ok. During development I'he used nock.js and put expectations inside .post nock method, like this:

it('test signature should be present' , done => {
  ...
  nock...
    .post('', body => {
      const requestBody = JSON.parse(body);
      expect(requestBody.config.signature).toEqual('TEST SIGNATURE');
      done();
    ...
  })
...
});

but need also to have integration tests that doesn't use nock with command

export NOCK_OFF='true' && jest

thus need a method to get request data and write expectations for it.

How this can be achieved?

P.S. I can imagine adding some custom axios interceptor in each test case before making HTTP call and removing it after test assertions, but it seems a lot of code for such thing. Or having some test helper that will store in its state data from the response and assert some things against it.

P.P.S. Possible variant is to spawn some HTTP Proxy and somehow catch requests and do assertions on them. But this is engineering overkill.

P.P.S. There may be a need for this testing flow though, but this is different question.

zmii
  • 3,268
  • 2
  • 29
  • 53

0 Answers0