lundi 9 décembre 2019

How to test a module using jest which imports an external library which is a constructor

I'm using a library, "pdfmake" and I want to write test cases using jest. I have a module pdf. Inside module pdf I'm exporting 2 functions. In both the exports, I'm using internal functions of "pdfmake" to generate pdfs.

Here is the code snippet: pdf.js

const PdfPrinter = require("pdfmake");
const fonts = require("./../shared/fonts");

const printer = new PdfPrinter(fonts);

const intiateDocCreation = docDefinition =>
  printer.createPdfKitDocument(docDefinition);

const finishDocCreation = (pdfDoc, pdfStream) => {
  pdfDoc.pipe(pdfStream);
  pdfDoc.end();
};
module.exports = {
  intiateDocCreation,
  finishDocCreation
};

I tried using

const PdfPrinter = require("pdfmake");
jest.mock("pdfmake", () => {
  return {
    createPdfKitDocument: jest.fn().mockImplementation(() => ({ a: "b" }))
  };
});

describe("test", () => {
  test("pdf", () => {
    const printer = new PdfPrinter(fonts);
    printer.createPdfKitDocument();
    expect(printer.createPdfKitDocument).toHaveBeenCalledTimes(1);
  });
});

Jest gives an error: TypeError: PdfPrinter is not a constructor

Aucun commentaire:

Enregistrer un commentaire