Replies: 2 comments 2 replies
-
@daelmaak did you find a solution to this? I’m running into the same issue |
Beta Was this translation helpful? Give feedback.
1 reply
-
So I raised a similar issue on Discord and @sheremet-va was so kind to provide a solution that might be helpful in your case. I have modified it from .png to more mediaTypes. const mediaTypes = ['png', 'jpg', 'jpeg', 'gif', 'svg', 'webp', 'ico', 'bmp', 'avif', 'webp'];
defineConfig({
plugins: [
/**
* A custom plugin to convert media files to data URLs for jsdom resources: usable
*/
{
name: 'media-to-data-url',
enforce: 'pre',
load(id) {
for (const mediaType of mediaTypes) {
if (id.endsWith(`.${mediaType}`)) {
const src = readFileSync(id).toString('base64');
return `export default "data:image/${mediaType};base64,${src}"`;
}
}
},
},
]
}) Specifically, this converts the assets to base64 which allowed me personally to get jsdom running with |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Jest has a way to (not) process imported assets (.jpg, .png, ...) by setting what should be the resolved value. So it can resolve imported assets to eg. a mock object:
jest.config.js
As for Vite, I can't find an equivalent option to do that. The reason I need to do it is that in our Next.js codebase, we have some dynamic
require('../path/to/image')
s in our pages which Next.js resolves to an object which contains, among other things,src
,width
and other properties. But Vitest by default resolves such require differently, which in the test results intoSyntaxError: Invalid or unexpected token
(on the line of therequire
).So is there a way to make Vite transform assets in particular way so that it conforms to Next's image resolution mechanism?
Beta Was this translation helpful? Give feedback.
All reactions