i am new to nodejs testing with mocha and chai.right now I am having issue while testing a API route handler with mocha. my route handler code is
exports.imageUpload = (req, res, next) => {
Upload(req,res, async () => {
//get the image files and its original urls (form-data)
let files = req.files['files[]'];
const originalUrls = req.body.orgUrl;
// check the input parameters
if(files == undefined || originalUrls == undefined){
res.status(400).send({status:'failed', message:"input field cannot be undefined"})
}
if(files.length > 0 && originalUrls.length > 0){
//array of promises
let promises = files.map(async(file,index)=>{
//create a image document for each file
let imageDoc = new ImageModel({
croppedImageUrl : file.path,
originalImageUrl: (typeof originalUrls === 'string') ? originalUrls : originalUrls[index],
status: 'New'
});
// return promises to the promises array
return await imageDoc.save();
});
// resolve the promises
try{
const response = await Promise.all(promises);
res.status(200).send({status: 'success', res: response});
}catch(error){
res.status(500).send({status:"failed", error: error})
}
}else{
res.status(400).send({status:'failed', message:"input error"})
}
})
}
the Upload function is just a multer utility to store the imagefile and my test code is
describe('POST /content/image_upload', () => {
it('should return status 200', async() => {
const response = await chai.request(app).post('/content/image_upload')
.set('Content-Type', 'application/form-data')
.attach('files[]',
fs.readFileSync(path.join(__dirname,'./asset/listEvent.png')), 'asset')
.field('orgUrl', 'https://images.unsplash.com/photo-1556830805-7cec0906aee6?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1534&q=80')
expect(response.error).to.be.false;
expect(response.status).to.be.equal(200);
expect(response.body.status).to.be.equal('success');
response.body.res.forEach(item => {
expect(item).to.have.property('croppedImageUrl');
expect(item).to.have.property('originalImageUrl');
expect(item).to.have.property('status');
expect(item).to.have.property('_id');
})
})
})
and the output shown after running this code is
1) POST /content/image_upload
should return status 200:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/home/techv/content_capture/server/ContentServiceLib/api/test/image_upload.test.js)
Aucun commentaire:
Enregistrer un commentaire