6

Having trouble using Nock.js to mock an HTTP Post Request with csv file attachment.

With superagent this is possible:

authenticatedSession
      .post('/foo')
      .set('Content-Type', 'multipart/form-data')
      .attach('csv', barCsv)
      .expect(200)

With axios, the request would look like:

const config = { headers:
    {
      'Content-Type': 'multipart/form-data'
    } };

    const formData = new FormData();
    formData.set('fooCsv', file);

 axios.post(`${API_URL}/foo`, formData, config)

But with nock, the below request has yet to hit.

nock(API_URL)
   .matchHeader('content-type', 'multipart/form-data')
   .post('/foo', formData)
   .reply(200, successfulResponse);

The only "helpful" error is this: Error: Data after transformation must be a string, an ArrayBuffer, a Buffer, or a Stream.

How can the axios and/or nock request be reformatted? How can the appropriate formData be matched in .post with nock?

ken
  • 3,650
  • 1
  • 30
  • 43

0 Answers0