How to unit test with a file upload in mocha

Here is an example of how you would do it with supertest module.

var should = require('should'),
    supertest = require('supertest');
var request = supertest('localhost:3000');

describe('upload', function() {
    it('a file', function(done) {
       request.post('/your/endpoint')
              .field('extra_info', '{"in":"case you want to send json along with your file"}')
              .attach('image', 'path/to/file.jpg')
              .end(function(err, res) {
                  res.should.have.status(200); // 'success' status
                  done();
              });
    });
});

Leave a Comment