Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getDocument hangs in Jest/JSDom environment #9579

Closed
wojtekmaj opened this issue Mar 18, 2018 · 5 comments
Closed

getDocument hangs in Jest/JSDom environment #9579

wojtekmaj opened this issue Mar 18, 2018 · 5 comments
Labels

Comments

@wojtekmaj
Copy link
Contributor

wojtekmaj commented Mar 18, 2018

On PDF.js 2.0.385 and later, running the following simplest example:

pdf.getDocument({ data: arrayBufferData })
  .then(() => console.log('Success'))
  .catch() => console.log('Fail'));

runs fine on Node.js, obviously.

Running the same piece in Jest, as in this example (simplified, please ignore the fact the tests should be async):

describe('Test', () => {
  it('does something', () => {
    pdf.getDocument({ data: arrayBufferData })
      .then(() => console.log('Success'))
      .catch() => console.log('Fail'));
  });
}
  • if testEnvironment is set to "node", succeeds.
  • if testEnvironment is set to "jsdom" (default), hangs and never reaches .then().

On PDF.js 2.0.305 and earlier this is not an issue, because you can easily disable workers manually.

Now that there's no way of doing that via config as of #9385, all projects using PDF.js and Jest/JSDom will be unable to test anything PDF.js-related. Switching testEnvironment to "node" is usually not an option as this will cause virtually everything else on front-end to fail.

@Snuffleupagus
Copy link
Collaborator

As mentioned in both ISSUE_TEMPLATE.md and CONTRIBUTING.md:
For issues with custom implementations, you need to provide access to a complete, and small, runnable example for the issue to be actionable. And please don't assume that people are familiar with various JS frameworks, in this case "Jest".

Without any context or code, the following questions comes to mind:
Are workers supported, and if so have you tried to set the workerSrc option and just run tests with workers enabled?
Have you tried using the workerPort option to load the worker file? Keep in mind that it's also possible to pass a worker to getDocument, see

pdf.js/src/display/api.js

Lines 129 to 130 in 0d391da

* @property {PDFWorker} worker - (optional) The worker that will be used for
* the loading and parsing of the PDF data.

@wojtekmaj
Copy link
Contributor Author

I've managed to deal with it! For those stuck like me, do NOT set pdfjs.GlobalWorkerOptions.workerSrc or pdfjs.GlobalWorkerOptions.workerPort. Instead, use pdf.worker.entry.js file - simply import it and you're done!

@p-mcgowan
Copy link

The above solution didn't work for me, instead I had to configure jest to use node instead of jsdom (enabled by default)

Adding this to the top of my spec file solved it

/**
 * @jest-environment node
 */

@sem4phor
Copy link

sem4phor commented Jul 12, 2019

For me none of the above solutions are working. Im using vue-cli with jest tests

@zzzachzzz
Copy link

This issue is a bit older, and I ran into different problems when running in Jest, though I got it working with a couple solutions:

  1. The browser/environment lacks native support for critical functionality used by the PDF.js library (e.g. `ReadableStream`); please use a `legacy`-build instead.

Solution: Install a polyfill for ReadableStream, and import it before your test: import 'web-streams-polyfill/es2018';

  1. TypeError: _pdfWorker.default is not a constructor

Solution: Conditionally configure the PdfjsWorker:

if (process.env.NODE_ENV !== 'test') {
  pdfjs.GlobalWorkerOptions.workerPort = new PdfjsWorker();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants