The MyComponent is importing an SVG file, and then using it in the code. I cannot unit test such component because vue-svg-loader is not configured in my Jest.
import { shallowMount } from "@vue/test-utils";
import MyComponent from "../MyComponent";
describe("MyComponent.vue", () => {
it("should put new date if is was passed via props", async () => {
const wrapper = shallowMount(MyComponent, {
propsData: {
value: "2018-01-05"
}
});
The unit test throws... Cannot render the SVG.
How to add vue-svg-loader to the Jest configuration?
There is one major issue when it comes to integrating vue-svg-loader with Jest, and it is async behaviour. Jest's transforms are synchronous, webpack loaders can be both. That means we cannot use SVGO to process the SVG files, which can be bad in some cases. I suggest you and everybody to always pass the SVG files through SVGO before putting them in a project (for example using this great tool), so that the end result does not contain:
<script> tags,<style> tags.If your SVGs are prepared, create a transform file named for example svgTransform.js with:
const vueJest = require('vue-jest/lib/template-compiler');
module.exports = {
process(content) {
const { render } = vueJest({
content,
attrs: {
functional: false,
},
});
return `module.exports = { render: ${render} }`;
},
};
And then modify your jest.config.js to use the transform file above (note that <rootDir> is injected by Jest):
module.exports = {
transform: {
'^.+\\.svg$': '<rootDir>/path/to/svgTransform.js',
},
};
To test if everything works you need to start the tests with the --no-cache flag. Let me know if that works for you.
@koresar have you had a chance to test the solution above?
Hello.
Sorry mate. I had no chance.
We've decided to just rename the file extension from .svg to .vue and wrap
it in the tag. As the result no additional configuration needed
anywhere.
Cheers
On Wed., 25 Jul. 2018, 20:50 Damian Stasik, notifications@github.com
wrote:
@koresar https://github.com/koresar have you had a chance to test the
solution above?—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/visualfanatic/vue-svg-loader/issues/38#issuecomment-407714406,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABjCL1pdCsnd8I9q5aMSLeYmWxK9iWNdks5uKE2LgaJpZM4VfZIZ
.
No problem at all 👍
Just wanted to say that I also had this error, and your solution works like a charm with no modification 👍
Thanks!
@koresar If that didn't work for you, check whether svg is present in another regular expression in other transform properties.
If you're using Vue CLI, it probably generated a line for all assets (including SVGs) that gets transformed with jest-transform-stub. If this line contains svg, just remove it, it should work.
module.exports = {
// ...
transform: {
// ...
'.+\\.(css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$': 'jest-transform-stub',
'^.+\\.svg$': '<rootDir>/svgTransform.js'
}
}
I follow the instruction of adding loader to jest
https://vue-svg-loader.js.org/faq.html#how-to-use-this-loader-with-jest
but this error appears

any update on this? Im running into the exact same error.
I follow the instruction of adding loader to jest
https://vue-svg-loader.js.org/faq.html#how-to-use-this-loader-with-jestbut this error appears
if your svg files have ?xml ? you'll get the error. So need just remove it:
const vueJest = require('vue-jest/lib/template-compiler');
module.exports = {
process (content) {
// remove . Should be one root element or get an error
if (content.startsWith('
const svgElIndex = content.indexOf('?>') + 2;
content = content.substring(svgElIndex);
}
const { render } = vueJest({
content,
attrs: {
functional: false
}
});
return `module.exports = { render: ${render} }`;
}
};
if your svg files have ?xml ? you'll get the error. So need just remove it:
i removed my ?xml and still am encountering the above issue
Most helpful comment
There is one major issue when it comes to integrating
vue-svg-loaderwith Jest, and it is async behaviour. Jest's transforms are synchronous, webpack loaders can be both. That means we cannot use SVGO to process the SVG files, which can be bad in some cases. I suggest you and everybody to always pass the SVG files through SVGO before putting them in a project (for example using this great tool), so that the end result does not contain:<script>tags,<style>tags.If your SVGs are prepared, create a transform file named for example
svgTransform.jswith:And then modify your
jest.config.jsto use the transform file above (note that<rootDir>is injected by Jest):To test if everything works you need to start the tests with the
--no-cacheflag. Let me know if that works for you.