Node.js express-Unit Testing of a function-the function reads a csv file and store its data in MySQL database
My function that is to be tested. I want to test uploadAuthors function using Jest
```
const uploadAuthors = async (req, res) => {
try {
if (req.file == undefined) {
return res.status(400).send("Please upload a CSV file!");
}
let authors = [];
let path = __basedir + "/resources/static/assets/uploads/" + req.file.filename;
fs.createReadStream(path)
.pipe(csv.parse({ headers: true }))
.on("error", (error) => {
throw error.message;
})
.on("data", (row) => {
authors.push(row);
})
.on("end", () => {
Authors.bulkCreate(authors)
.then(() => {
res.status(200).send({
message:
"Uploaded the file successfully: " + req.file.originalname,
});
})
.catch((error) => {
res.status(500).send({
message: "Fail to import data into database!",
error: error.message,
});
});
});
} catch (error) {
console.log(error);
res.status(500).send({
message: "Could not upload the file: " + req.file.originalname,
});
}
};```
Here's the csv file data
email,firstname,lastname
null-walter@echocat.org,Paul,Walter
And here's my Unit Test file's code:
```
const { uploadAuthors } = require('../controllers/csv')
global.__basedir = __dirname + "/Raft-Labs-Test/..";
beforeEach(() => {
//code to connect to database
});
test('Uploading data to authors table', async () => {
let resultData = {"message": "Uploaded the file successfully: test.csv"}
let req = {file: test.csv}
let res = {}
let data = await uploadAuthors(req,res)
expect(data).toEqual(resultData)
})```
I want to write test using jest for the above function but just cannot do it without having a req.file (In the test's code test.csv file inside req object is just a variable and not the actual file) which is fine to have using Postman but i don't know how to do it without Postman and through Jest.