title: How to test image upload (stream) with supertest and jest?
body: <p>I have an image upload endpoint in my API that accepts <code>application/octet-stream</code> requests and handles these streams. I'd like to write test coverage for this endpoint but cannot figure out how to use supertest to stream an image.</p>
  
  <p>Here's my code so far:</p>
  
  <pre><code>import request from 'supertest'
  
  const testImage = `${__dirname}/../../../assets/test_image.jpg`
  
  describe('Upload endpoint', () =&gt; {
  
    test('Successfully uploads jpg image', async () =&gt;
      request(app)
        .post(`${ROOT_URL}${endpoints.add_image.route}`)
        .set('Authorization', `Bearer ${process.env.testUserJWT}`)
        .set('content-type', 'application/octet-stream')
        .pipe(fs.createReadStream(testImage))
        .on('finish', (something) =&gt; {
          console.log(something)
        }))
  
  })
  </code></pre>
  
  <p>This code produces nothing, the <code>finish</code> event is never called, nothing is console logged, and this test suite actually passes as nothing is expected. I cannot chain a <code>.expect</code> onto this request, otherwise I get this runtime error:</p>
  
  <blockquote>
    <p>TypeError: (0 , _supertest2.default)(...).post(...).set(...).set(...).pipe(...).expect is not a function</p>
  </blockquote>
  
  <p>How is such a thing accomplished?</p>