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.

getDocument hangs in Jest/JSDom environment

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

13reactions
wojtekmajcommented, Apr 2, 2018

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!

3reactions
p-mcgowancommented, Jan 25, 2019

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
 */
Read more comments on GitHub >

github_iconTop Results From Across the Web

JavaScript - can't get jsdom document in Jest test
I have a module that provides some convenience functions for DOM manipulation that I'm trying to test with Jest and ...
Read more >
Troubleshooting - Jest
Use this guide to resolve issues with Jest. Tests are Failing and You Don't Know Why​. Try using the debugging support built into...
Read more >
Why Jest freezes after tests run? (Jest hangs indefinitely)
If you noticed that Jest (npm / yarn) test hang at the end of the test execution while you run tests in parallel...
Read more >
Use Jest write unit testing for DOM manipulation
so we must ready DOM for test case of addTodo ! As mention document of Jest: Jest ships with jsdom which simulates a...
Read more >
Changelog.md - jest-environment-jsdom-fourteen - GitLab
Fixed HTML serialization (e.g. via innerHTML ) breaking after setting certain properties to non-string values. Fixed how disabling an element ...
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