Skip to content

Instantly share code, notes, and snippets.

@przemkow
Last active July 29, 2021 10:52
Show Gist options
  • Save przemkow/e2a7a928a6c8444d602b29965acc0f41 to your computer and use it in GitHub Desktop.
Save przemkow/e2a7a928a6c8444d602b29965acc0f41 to your computer and use it in GitHub Desktop.
Jest transform svg with vue-jest@4
modules.export = {
...,
transform: {
".*\\.(svg)$": "<rootDir>/svg-transform.js",
},
}
{
"@vue/component-compiler-utils": "^3.2.0",
"vue-template-compiler": "^2.6.14",
...
}
const compilerUtils = require("@vue/component-compiler-utils");
const VueTemplateCompiler = require("vue-template-compiler");
/*
* Manually compile SVG to Vue component so jest can render imported SVG in the same way as it is rendered in live app.
* https://github.com/vuejs/component-compiler-utils#handling-the-output
*/
module.exports = {
process(source) {
const compiledTemplate = compilerUtils.compileTemplate({
source,
compiler: VueTemplateCompiler,
isFunctional: false,
});
return `${compiledTemplate.code}
module.exports = { render: render }`;
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment