0

I am trying to cover the lines and functions for code coverage of the simple multer file in Node.js but I am not getting where I am going wrong.

Here is the file:

fileUpload.js

const express = require("express");
const multer = require("multer");
const path = require("path");

const storage = multer.diskStorage({
  destination: (req, file, callBack) => {
    callBack(null, "uploads");
  },
  filename: (req, file, callBack) => {
    callBack(null, `${file.originalname}`);
  },
});

const upload = multer({
  storage: storage,
  fileFilter: (req, file, cb) => {
    if (file.mimetype == "image/png" || file.mimetype == "image/jpg" || file.mimetype == "text/plain") {
      cb(null, true);
    } else {
      cb(null, false);
      return cb(new Error('Only .png, .jpg, .txt and .jpeg format allowed!'));
    }
  }
})
module.exports = {
  upload,
  express
};

fileUpload.test.js

const multer = require('multer')
const fileStorage = require("../file-storage");

jest.mock('multer', () => {
  const multer = () => ({
    diskStorage: () => {
      return {
        destination: (req, res, callBack) => {
          callBack(null, 'uploads')
        },
        filename: (req, res, callBack) => {
          req.body = {
            userName: 'testUser'
          }
          req.files = [{
            originalname: 'sample.name',
            mimetype: 'sample.type',
            path: 'sample.url',
            buffer: Buffer.from('whatever'), // this is required since `formData` needs access to the buffer
          }, ]
          return callBack()
        }
      }

    },
  })
  multer.diskStorage = () => jest.fn()
  return multer

})

describe('65317652', () => {
  const upload = fileStorage.upload
  it('should pass', () => {
    const storage = upload.diskStorage();
    const uploadData = multer({
      storage: storage
    });
    expect(uploadData).toBeTruthy();
  });
});

I have searched plenty of blogs but didn't find any proper solution.

enter image description here

How to cover those lines?

vinuta
  • 423
  • 9
  • 29
  • Those lines are invoked _by multer_, as callbacks. In your case you've mocked multer out (with a weirdly intricate test double that seems to repeat the actual implementation you've written...), so if you want those functions to get called you'll have to do so directly. – jonrsharpe Sep 09 '22 at 13:43
  • but i have seen lot of example and they did in the same way, can you please provide the code if possible thanks – vinuta Sep 09 '22 at 13:46
  • No, I want you to think more carefully about what your code is actually doing, so you can both test it and actually understand it. Here's an example you can adapt to your specific use case: https://stackoverflow.com/q/55958626/3001761. – jonrsharpe Sep 09 '22 at 13:53

1 Answers1

0

That might not be the best way to test your module which uses multer (I don't personally know this library). However you could manage to reach your code in the following way.

  1. Get rid of your mock since you want to cover the code from your file-storage.js not the code from your mock.
  2. Perform all calls in order to reach the code you want to cover.

Here is how you could reach the 'destination' code from you file-storage.js:

const multer = require('multer')
const fileStorage = require("../file-storage");

// 1 - Get rid of mock since this is not what you want to cover
//
// jest.mock('multer', () => {
//     const multer = () => ({
//         diskStorage: () => {
//             return {
//                 destination: (req, res, callBack) => {
//                     callBack(null, 'uploads')
//                 },
//                 filename: (req, res, callBack) => {
//                     req.body = {
//                         userName: 'testUser'
//                     }
//                     req.files = [{
//                         originalname: 'sample.name',
//                         mimetype: 'sample.type',
//                         path: 'sample.url',
//                         buffer: Buffer.from('whatever'), // this is required since `formData` needs access to the buffer
//                     },]
//                     return callBack()
//                 }
//             }

//         },
//     })
//     multer.diskStorage = () => jest.fn()
//     return multer

// })

describe('65317652', () => {
    const upload = fileStorage.upload
    it('should pass', () => {
        const storage = upload;
        const multerRes = multer({
            storage: storage
        });
        
        // 2 - Perform all calls in order to reach the code you want to cover.
        //
        expect(multerRes).toBeTruthy();
        const diskStorageRes = multerRes.storage.storage
        expect(diskStorageRes).toBeTruthy();
        const getDestinationRes = diskStorageRes.getDestination()
        expect(getDestinationRes).toBeTruthy();

    });

});

There seem to be other more appropriate ways to test multer. See https://stackoverflow.com/a/58474936/6210975 for example.

Greg7000
  • 297
  • 3
  • 15