Supertest: ENOENT on .attach

Created on 15 Jul 2015  路  2Comments  路  Source: visionmedia/supertest

To test an endpoint for uploading videos, I am using .attach(), but my test never passes due to what I am assuming is supertests inability to locate the video.

Here is my test case:

var app = require('./helpers/app');
var blog = require('../routes/blog');
var upload = require('../routes/files');
var should = require('should');
var expect = require('expect');
var request = require('supertest');
var bodyParser = require('body-parser');
...
//other unrelated tests here
...
describe("Adding a file to the local file system", function() {
    it("will successfully reach the endpoint", function(done) {
        request(app)
        .post('/uploads')
        .field('Content-Type', 'multipart/form-data')
        .field('file', 'filefield')
        .attach('file', './files/zack-video-test.mp4')
        .end(function (err, res) {
            if(err) {
                console.log(err);
            } else expect(res.status).to.equal(200);
            files.busboyFinished().should.be.true();
            it("will find the local file as defined", function() {
                expect(files.localFile()).toExist();
            });
            done();
        })

    });
});

When the test is run, I get this output:

Adding a file to the local file system
    1) will successfully reach the endpoint
POSTING TO /UPLOAD
request headers  { host: '127.0.0.1:56042',
  'accept-encoding': 'gzip, deflate',
  'user-agent': 'node-superagent/1.2.0',
  'content-type': 'multipart/form-data; boundary=--------------------------840493739829893270864048',
  connection: 'close',
  'transfer-encoding': 'chunked' }


  5 passing (84ms)
  1 failing

  1) Adding a file to the local file system will successfully reach the endpoint:
     Uncaught Error: ENOENT, open './files/zack-video-test.mp4'

I have already tried changing the path of the video around (files/, ./files/, ../tests/files/) but nothing seems to work.

Any ideas what I am doing wrong here?

Most helpful comment

The way paths are evaluated seems a bit weird. This works for me:
.attach('file', 'tests/files/zack-video-test.mp4')

All 2 comments

The way paths are evaluated seems a bit weird. This works for me:
.attach('file', 'tests/files/zack-video-test.mp4')

@grobgl yep. I meant to come back and close this issue. I realized that the path is relative to the app route, not the test file. This is working for me now. Thanks for checking on it for me.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nazreen picture nazreen  路  3Comments

Shingaz picture Shingaz  路  5Comments

peterjuras picture peterjuras  路  3Comments

mickaeltr picture mickaeltr  路  3Comments

schm1ty1 picture schm1ty1  路  4Comments