Jest a minute… how to mock a class

Richard Bell
Level Up Coding
Published in
3 min readJan 16, 2024

--

Some code on a screen
Photo by Ferenc Almasi on Unsplash

Let’s get right into it. Our example involves the npm package ‘pdfkit’. The way pdfkit is used in code looks something like this:

import pdfkit from 'pdfkit';

const doc = new pdfkit();

doc.text('This is page one of my pdf');
doc.addPage();
doc.text('This is page two of my pdf');

This is a very simple example which will create a pdf with two pages with some text on each page…

--

--

Father to two beautiful baby girls | Husband to a generous wife | Lead Software Engineer | Hobby collector | Support me at ko-fi.com/richardtbell