Last active
July 29, 2021 10:52
-
-
Save przemkow/e2a7a928a6c8444d602b29965acc0f41 to your computer and use it in GitHub Desktop.
Jest transform svg with vue-jest@4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
modules.export = { | |
..., | |
transform: { | |
".*\\.(svg)$": "<rootDir>/svg-transform.js", | |
}, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"@vue/component-compiler-utils": "^3.2.0", | |
"vue-template-compiler": "^2.6.14", | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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