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

How to use SVG component in Jest? #22

Closed
pontakornth opened this issue May 30, 2022 · 3 comments
Closed

How to use SVG component in Jest? #22

pontakornth opened this issue May 30, 2022 · 3 comments

Comments

@pontakornth
Copy link

I am trying to setup Jest unit test but Jest cannot transform .svg file. I don't know how to do it when it is imported as a Svelte component.

import Exclamation from '$lib/icons/exclamation.svg'

<Exclamation />
@pontakornth
Copy link
Author

I discovered a funny solution. I just need to map .svg to a Svelte component with an empty svg tag. Then, use moduleNameMapper.

jest.config.cjs

module.exports = {
// Unrelated config
	moduleNameMapper: {
		// Other config
		'^.+\\.svg$': '<rootDir>/src/lib/EmptyIcon.svelte'
	},
}

EmptyIcon.svelte

<svg />

I am not sure if I miss any edge case or something I should know so I don't close it for now.

@poppa
Copy link
Owner

poppa commented May 31, 2022

Thanks for your feedback. I haven't used it with Jest myself so I can't give you any pointers. Byt this is good info to know and add to the the README file.

I will take a look at it when I get time and also update the README with info on testing framework integrations.

@pontakornth
Copy link
Author

@poppa That's great. By the way, I discovered that ?url can be used with jest-transform-stub in both transform and moduleMapper. I am not sure which one fixed the issue but at least it works.

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

No branches or pull requests

2 participants