title: Mock a multipart/form-data Express.JS request object
body: <p>I would like to unit test an Express middleware function which, in turn, uses <a href="https://github.com/felixge/node-formidable" rel="noreferrer">node-formidable</a> to process a multipart file upload.</p>
  
  <p>Here’s a contrived example:</p>
  
  <pre><code>function doUpload(req, res, next) {
      const form = new formidable.IncomingForm();
      form.uploadDir = this.mediaDir;
      form.keepExtensions = true;
      form.type = 'multipart';
      form.multiples = true;
  
      form.parse(req, function(err, fields, files) {
          res.writeHead(200, {'content-type': 'text/plain'});
          res.write('received upload:\n\n');
          res.end(util.inspect({fields: fields, files: files}));
      });
  }
  </code></pre>
  
  <p>This code works for me when tested with Chrome and a running Express app.</p>
  
  <p>I want my test code to look like the following where I am mocking the request object, but I can't figure out how to mock the request object with the form data. The formidable callback is not firing:</p>
  
  <pre><code>it(‘handles uploads’, (done) =&gt; {
      const mockReq = http.request({
          host: 'example.org',
      });
      mockReq.url = ‘/upload’;
  
      const res = jasmine.createSpyObj('response', [ 'json', 'send', 'status', 'render', 'header', ‘redirect’, ‘end’, ‘write;]);
      const next = jasmine.createSpy('next');
  
      //*how to emulate a request with a multipart file upload)
  
      doUpload(req, res, next);
      res.write.and.callFake(done);
  });
  </code></pre>
  
  <p>I’ve tried using the <a href="https://github.com/form-data/form-data" rel="noreferrer">form-data</a> library to create a FormData object and pipe it to the request, but no luck, I’m not sure if I’m on the right track or way off. Something like this:</p>
  
  <pre><code>var form = new FormData();
  
  const buff = fs.readFileSync(path.join(__dirname, '../fixtures/logo.png'));
  
  form.append('file', buff, {
      filename: 'logo.jpg',
      contentType: 'image/png',
      knownLength: buff.length
  });
  form.append('name', 'Logo');
  
  req.headers = form.getHeaders();
  
  form.pipe(req);
  doUpload(req, res, next);
  </code></pre>