Skip to content

Commit 075d60d

Browse files
Added Jest configuration in the FAQ
1 parent 6b0067b commit 075d60d

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

docs/faq.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,3 +314,40 @@ declare module '*.svg' {
314314
export default content;
315315
}
316316
```
317+
318+
## How to use this loader with Jest?
319+
320+
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. It is always good idea to always pass the SVG files through SVGO before putting them in a project [(for example using this great tool)](https://jakearchibald.github.io/svgomg/), so that the end result does not contain:
321+
322+
- XML declaration,
323+
- `<script>` tags,
324+
- `<style>` tags.
325+
326+
If your SVGs are prepared, create a transform file named for example `svgTransform.js` with:
327+
328+
``` js
329+
const vueJest = require('vue-jest/lib/template-compiler');
330+
331+
module.exports = {
332+
process(content) {
333+
const { render } = vueJest({
334+
content,
335+
attrs: {
336+
functional: false,
337+
},
338+
});
339+
340+
return `module.exports = { render: ${render} }`;
341+
},
342+
};
343+
```
344+
345+
And then modify your `jest.config.js` to use the transform file above (note that `<rootDir>` is injected by Jest):
346+
347+
``` js
348+
module.exports = {
349+
transform: {
350+
'^.+\\.svg$': '<rootDir>/path/to/svgTransform.js',
351+
},
352+
};
353+
```

0 commit comments

Comments
 (0)