How would you test file upload ? #2250
-
Hello, How would you test an endpoint using I want to do something similar to this Stack Overflow answer: const fs = require('fs');
const MockExpressRequest = require('mock-express-request');
const FormData = require('form-data');
const form = new FormData();
form.append('my_file',
fs.createReadStream(path.join(__dirname, 'fixtures', 'file-upload-test.txt'))
);
const request = new MockExpressRequest({
method: 'POST',
host: 'localhost',
url: '/upload',
headers: form.getHeaders()
});
form.pipe(request);
doUpload(request, response, next); But I don't want to add dependencies just for this test case. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hello @JonathanCabezas , Upload functionality relies on For testing the upload functionality you'd need a system test:
|
Beta Was this translation helpful? Give feedback.
Hello @JonathanCabezas ,
Upload functionality relies on
express-fileupload
body parser which is not covered by the unit testing helpertestEndpoint()
.You can use
testEndpoint
with{ requestProps: { method: "POST", body: data }}
wheredata
is already a parsed object, but that would be the test of the endpoint handler, not the parser, like the upload did already happen.For testing the upload functionality you'd need a system test:
express-zod-api/tests/system/example.spec.ts
Line 19 in 6e23748
express-zod-api/tests/system/exa…