Comments (15)
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:
- XML declaration,
<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.
from vue-svg-loader.
@joaopslins It took a bit of digging, but here's how I got this working for @vue/vue2-jest
and jest 28
:
Update svgTransform.js
to:
const VueTemplateCompiler = require('vue-template-compiler');
module.exports.process = (svgSource, filename) => {
const result = VueTemplateCompiler.compileToFunctions(
`${svgSource}`
);
return {
code: `module.exports = { render: ${result.render} }`,
};
}
jest.config.js stays the same:
module.exports = {
transform: {
'^.+\\.svg$': '<rootDir>/path/to/svgTransform.js',
},
};
And as @visualfanatic pointed out, make sure you run jest with the --no-cache
flag option i.e.
jest --no-cache
Otherwise you may not see any impact after updating svgTransform.js
. I didn't realize jest was caching the transforms at first when I was trying to debug and wasted a good few hours because of it.
For reference:
- https://github.com/vuejs/vue/tree/dev/packages/vue-template-compiler#compilercompiletofunctionstemplate
- https://jestjs.io/docs/28.0/code-transformation
from vue-svg-loader.
@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'
}
}
from vue-svg-loader.
Hi folks,
This solution is not working anymore after updating to vue-jest@4
, or @vue/vue2-jest
because this import fails require('vue-jest/lib/template-compiler');
, since there's no template-compiler
file anymore.
Does anyone figured out another workaround? Thanks!
from vue-svg-loader.
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('') + 2;
content = content.substring(svgElIndex);
}
const { render } = vueJest({
content,
attrs: {
functional: false
}
});
return `module.exports = { render: ${render} }`;
}
};
from vue-svg-loader.
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
- @fameches im trying to replicate your code but it seems there are some syntax errors and i dont understand what you're trying to get across
from vue-svg-loader.
from vue-svg-loader.
No problem at all 👍
from vue-svg-loader.
Just wanted to say that I also had this error, and your solution works like a charm with no modification 👍
Thanks!
from vue-svg-loader.
@koresar have you had a chance to test the solution above?
from vue-svg-loader.
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
from vue-svg-loader.
any update on this? Im running into the exact same error.
from vue-svg-loader.
Solution from @visualfanatic worked! However as I was using a later version of babel, I had to install babel bridge to make vue-jest compile properly.
npm i -D babel-core@^7.0.0-bridge.0
from vue-svg-loader.
As a sidenode, make sure to run --clearCache after you make this change... Since --no-cache
only skips the cache and doesn't reset it, you maybe experience stale issues even after fixing your svgTransform.js
if you don't reset it explicitly.
from vue-svg-loader.
@anthonylebrun Thank you! This worked well! I appreciate the help 🙇
from vue-svg-loader.
Related Issues (20)
- svgo options not working in webpack config
- [Question] Some objects in the SVG are being removed HOT 4
- Please create a new version to support svgo 2.x HOT 7
- SVG is missing parent's data-v attribute?
- Component is missing template or render function. HOT 1
- Syntax error on build HOT 4
- Relative module not found! HOT 4
- Module not found: Error: Can't resolve 'vue-loader-v16' HOT 1
- Security vulnerability - nth-check dependency HOT 1
- Syntax Error: NonErrorEmittedError: (Emitted value instead of an instance of Error) Error in parsing SVG: Non-whitespace before first tag. HOT 1
- Nuxt 3 support HOT 9
- Vuecli 5.x support HOT 7
- Loading svgs from different packages
- vue3 error HOT 5
- Is this project still being maintained? HOT 5
- Vue packages version mismatch
- nuxt3 can use? HOT 1
- [Vue warn]: Error in nextTick: "InvalidCharacterError: Failed to execute 'createElement' on 'Document' HOT 1
- Allow loading SVG images from HTML without breaking the current syntax (Nuxt) HOT 2
- Mark the component as Raw or use shallowRef
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vue-svg-loader.