question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to setup worker in unit test in Create-React-App?

See original GitHub issue

What are you trying to achieve? Please describe.

Run a test that loads a pdf from local file in a CRA app.

Describe solutions you’ve tried

I have tried putting the following at the top of my unit test file

import { pdfjs } from 'react-pdf';
pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.min.js`;

but i get the following error

setting up fake worker failed: "Cannot find module '//cdnjs.cloudflare.com/ajax/libs/pdf.js/2.5.207/pdf.worker.min.js' from '../../node_modules/pdfjs-dist/build/pdf.js'".

Environment

“react-scripts”: “^4.0.1”, “react-pdf”: “^5.1.0”, “react”: “^17.0.1”,

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
maksimfcommented, Mar 8, 2022
  "jest": {
    "moduleNameMapper": {
      "^react-pdf$": "react-pdf/dist/umd/entry.jest"
    }
  }

This solution has worked for me as well, the only thing I had to change though is this:

  "jest": {
    "moduleNameMapper": {
      "^react-pdf": "react-pdf/dist/umd/entry.jest"
    }
  },

I.e. removed $ cause I have:

import { Document, Page } from "react-pdf/dist/esm/entry.webpack";`)
3reactions
StephanBijzittercommented, Mar 18, 2021

Well the above did indeed work, to some extend at least, for me as well. But now I’m getting this issue:

    Worker was terminated
      at ensureNotTerminated (node_modules/pdfjs-dist/build/webpack:/pdfjs-dist/build/pdf.worker/src/core/worker.js:169:15)
      at onFailure (node_modules/pdfjs-dist/build/webpack:/pdfjs-dist/build/pdf.worker/src/core/worker.js:343:9)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Running Tests | Create React App
We recommend to put the test files (or __tests__ folders) next to the code they are testing so that relative imports appear shorter....
Read more >
How To Test a React App with Jest and React Testing Library
Step 1 — Setting up the Project. In this step, you will clone a sample project and launch the test suite. The sample...
Read more >
How To Test Your React Apps With The React Testing Library
Open a terminal and copy and run the below command. # start new react project and start the server npx create-react-app start-rtl &&...
Read more >
How to setup unit tests with Create React App, Mocha and ...
2. Install Mocha as a dev dependency, the test engine · 3. Install Chai-Enzyme · 4. Install · 5. Configure Babel · 6....
Read more >
Testing React Components with Testing Library and Mock ...
Testing React Components with Testing Library and Mock Service Worker · npm install msw. Once installed, we need to create some directories. ·...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found