In a vite project of mine that runs without any errors, I wanted to integrate some tests with vitest.
When running the following command npm run test
and I have a test file open for a react component, I get the following error:
FAIL src/components/shared/SomeComponent.test.jsx [ src/components/shared/SomeComponent.test.jsx ]
Error: require() of ES Module C:...uinode_modulespdfjs-distbuildpdf.mjs not supported.
Instead change the require of C:...uinode_modulespdfjs-distbuildpdf.mjs to a dynamic import() which is available in all CommonJS modules.
❯ Object.<anonymous> node_modules/react-pdf/dist/cjs/index.js:30:28
The error is still there even if I go to vite.config.js
and add the following snippet:
plugins: [
react(),
eslintPlugin(),
svgr(),
commonjs({
filter(id) {
// Include `react-pdf` and `pdfjs-dist` for transformation
if (
id.includes('node_modules/react-pdf') ||
id.includes('node_modules/pdfjs-dist')
) {
return true;
}
return false;
},
}),
]
Why does this error even show up? My application runs with 0 problems.