'How to test file upload service response - javascript
I hope you are well, thank you in advance. I need help to test the code of my service POST file upload I have tried everything and does not give me, I attach code of the service and the tests that I try to do.
The functionality is simply a file uploader that sends it to a POST service, the service should respond this: RESPONDE 200:
{
"file_name": "12582_c7282234-4ad1-4f02-afde-a33f69401458.pdf",
"original_file_name": "CONSTANCIA DEVOLUCION DINERO.pdf",
"type": "application/pdf",
"size": 182978,
"creation_date": "2022-02-24T20:24:59.372916"
}
Code:
import * as fs from 'fs';
import FormData from 'form-data';
upload(files: any) {
console.log('llega esto', files);
return new Promise((resolve, reject) => {
const formData = new FormData();
const readFile = fs.createReadStream(files.file[0].path);
readFile.on('open', () => {
formData.append('file', readFile, files.file[0].originalFilename);
const url = `${this.baseUrl}/attachments`;
const headers = {
'x-caller-id': '12582',
'Content-Type': 'application/json',
...formData.getHeaders(),
};
console.log('antes de requets');
return this.request
.post(url, {
data: formData,
headers: headers,
context: this.context,
})
.then((response: Response<any>) => {
resolve(response);
})
.catch((error: Error) => {
reject(error);
});
});
});
}
CODE TEST POSITIVE
test('you should not send the file', () => {
const responseUpload = {
file_name: 'file.pdf',
original_file_name: 'file.pdf',
type: 'application/pdf',
size: 0.0000076294,
creation_date: '2022-02-24T20:24:59.372916',
};
const pathFile = 'tests/unit/services/utils/mockFile.pdf';
fs.writeFile(pathFile, 'New file test', function (err) {
if (err) throw err;
});
const files = {
file: [
{
fieldName: 'file',
originalFilename: 'file.pdf',
path: 'tests/unit/services/utils/mockFile.pdf',
headers: [Object],
size: 8,
},
],
};
return new Promise((resolve) => {
const readFile = fs.createReadStream(files.file[0].path);
const formData = new FormData();
readFile.on('open', () => {
formData.append('file', readFile, files.file[0].originalFilename);
new Service(req).upload(files).then((response) => {
resolve(response);
expect(response).toBe(responseUpload);
});
});
fs.unlink(pathFile, () => ({}));
});
});
});
I get a 404 or that it cannot read the file
TEST NEGATIVE
const pathFile = 'tests/unit/services/utils/mockFile.pdf';
fs.writeFile(pathFile, 'New file test', function(err) {
if (err) throw err;
});
const files = {
file: [
{
path: 'mockFile.pdf',
originalFilename: 'mockFile.pdf',
size: 0.0000076294,
},
],
};
new Service(req).upload(files).catch((error) => {
expect(error.status).toBe(404);
});
fs.unlink(pathFile, () => ({}));
error test negative
**FAIL services tests/unit/services/upload/index.spec.ts
● Test suite failed to run**
Jest worker encountered 4 child process exceptions, exceeding retry limit
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
