Code Monkey home page Code Monkey logo

Comments (15)

damianstasik avatar damianstasik commented on June 28, 2024 19

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.

anthonylebrun avatar anthonylebrun commented on June 28, 2024 10

@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:

from vue-svg-loader.

sarahdayan avatar sarahdayan commented on June 28, 2024 7

@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.

joaopslins avatar joaopslins commented on June 28, 2024 3

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.

fameches avatar fameches commented on June 28, 2024 2

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

Screenshot-20191009142507-708x279

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.

kylegoines avatar kylegoines commented on June 28, 2024 2

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.

koresar avatar koresar commented on June 28, 2024 1

from vue-svg-loader.

damianstasik avatar damianstasik commented on June 28, 2024 1

No problem at all 👍

from vue-svg-loader.

joelworsham avatar joelworsham commented on June 28, 2024 1

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.

damianstasik avatar damianstasik commented on June 28, 2024

@koresar have you had a chance to test the solution above?

from vue-svg-loader.

engmagdy87 avatar engmagdy87 commented on June 28, 2024

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

Screenshot-20191009142507-708x279

from vue-svg-loader.

hendrikras avatar hendrikras commented on June 28, 2024

any update on this? Im running into the exact same error.

from vue-svg-loader.

anandbhaskaran avatar anandbhaskaran commented on June 28, 2024

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.

anthonylebrun avatar anthonylebrun commented on June 28, 2024

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.

joaopslins avatar joaopslins commented on June 28, 2024

@anthonylebrun Thank you! This worked well! I appreciate the help 🙇

from vue-svg-loader.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.