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'));
});
}
testEnvironment
is set to "node"
, succeeds.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.
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 https://github.com/mozilla/pdf.js/blob/0d391daccc2f4e4b9c91268e719bd10fe63a49ae/src/display/api.js#L129-L130
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!
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
*/
For me none of the above solutions are working. Im using vue-cli with jest tests
Most helpful comment
I've managed to deal with it! For those stuck like me, do NOT set
pdfjs.GlobalWorkerOptions.workerSrc
orpdfjs.GlobalWorkerOptions.workerPort
. Instead, use pdf.worker.entry.js file - simply import it and you're done!