Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

217
Views
How can I mock a method which is called after then in jest?

I have the following code which downloads a pdf:

import html2pdf from 'html2pdf.js'
export const addFooter = (pdf) => {
  const totalPages = pdf.internal.getNumberOfPages()
  ...
}
export const downloadPDF = async (...) => {
  const pdfContent = document.getElementById("export")
  const opts = {...}
  await html2pdf().from(pdfContent).set(opts).toPdf().get('pdf').then(addFooter).save()
}

I would like to be able to mock the save method using jest.

Up until now I have:

jest.mock('html2pdf.js', () => () => ({
  from: () => ({
    set: () => ({
      toPdf: () => ({
        get: () => Promise.resolve({
          internal: {
            getNumberOfPages: () => {}
          }
        })
      })
    })
  })
}))

But running the tests give me:

TypeError: (0 , _html2pdf.default)(...).from(...).set(...).toPdf(...).get(...).then(...).save is not a function

Maybe someone have an idea how to mock the save method?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!