jeudi 29 août 2019

Jest mock pdfmake and fs.createWriteStream

I am very new to Jest and unit testing in general. I am trying to generate test for a piece of code that essentially uses pdfmake and fs.createWriteStream in order to create and write to a pdf file

As I am reading tutorials - and getting confused a bit as it's info I am not used to - I have tried to put together couple of things. An fs.js module to try and get the writestream mocked. About the pdfmake I am not very sure - could i perhaps skip that and assume I have a target file and therefore mock just the stream creation?

describe('createWriteStream', () => {
  const MOCK_FILE_INFO = {
    '/path/to/file1.pdf': 'console.log("file1 pdf contents");'
  };

Something like the above?

And then just have a result as success for the fs.createWriteStream?

The original code looks something like:

var pdfDoc = new pdfPrinter({
        Roboto: {
            normal: new Buffer(
                require('pdfmake/build/vfs_fonts.js').pdfMake.vfs['Roboto-Regular.ttf'],
                'base64'
            )
        }
    }).createPdfKitDocument(docDefinition);
    pdfDoc.pipe(fs.createWriteStream(path));
    pdfDoc.end();

I understand is not a lot of info but I guess if someone is very aware of unit testing might know how to best shape this. Thanks

Aucun commentaire:

Enregistrer un commentaire