Unable to get jest to load a module (pdf.js) in a component test

Viewed 569

I am building tests using jest that makes use of the pdf.js node library, but keep getting the following error due to jest not picking up the library properly

TypeError: Cannot set property 'workerSrc' of undefined

Here is pdfParser.js:

import * as pdfJs from 'pdfjs-dist/legacy/build/pdf'

import pdfjsWorker from 'pdfjs-dist/build/pdf.worker.entry'

pdfJs.GlobalWorkerOptions.workerSrc = pdfjsWorker

export const readPdf = async theFile => {
 ... (*it doesn't even get this far)
}

Here is my test (pdfParser.test.js):

import { readPdf } from '../../../../src/utils/pdfParser.js'

describe('PDF Parser', () => {
  it('returns error when no file submitted', () => {
    expect(1).toEqual(1)
  })
})

I've tried setting the following in package.json

  "jest": {
    "moduleNameMapper": {
      "pdfjs-dist": "<rootDir>/node_modules/pdfjs-dist/legacy/build/pdf.js"
    },
    "moduleDirectories": [
      ".",
      "src",
      "src/util",
      "node_modules"      
    ]

What am I doing wrong?

0 Answers
Related