There's a SVG (icon.svg) in my React component (foo), which is imported inline using svg-inline-react (component) and svg-inline-loader (Webpack loader).
import InlineSVG from 'svg-inline-react'
import icon from '!svg-inline!./icon.svg'
const foo = () => {
return (
<div>
<div className="bar" />
<InlineSVG src={icon} />
</div>
)
}
But jsdom doesn't seem to support SVG elements, so Mocha throws the following error (I'm not sure if that's the reason).
<InlineSVG />:rawprop works only whenwindow.DOMParserexists.
module.js:341
throw err;
^
Error: Cannot find module '!svg-inline!./icon.svg'
Mocha settings:
require('babel-register')();
const jsdom = require('jsdom').jsdom;
const exposedProperties = ['window', 'navigator', 'document'];
global.document = jsdom('');
global.window = document.defaultView;
Object.keys(document.defaultView).forEach((property) => {
if (typeof global[property] === 'undefined') {
exposedProperties.push(property);
global[property] = document.defaultView[property];
}
});
global.navigator = {
userAgent: 'node.js'
};
What can I do about that? Can I just ignore the <InlineSVG> component during my tests?
Are you using mount or shallow? If you're using mount does shallow work instead?
My test:
describe('<foo />', () => {
it('contains bar', () => {
expect(mount(<foo />)
.contains(<div className="bar" />)).to.equal(true)
})
})
Both shallow and mount returns the same error. It doesn't seem to be related with Enzyme.
"Error: Cannot find module '!svg-inline!./icon.svg'" seems unrelated to enzyme - perhaps the webpack loader isn't functioning correctly in your tests?
Yeah this seems to be due to a combination of issues with 1) jsdom not supporting SVG and possibly 2) your webpack loader configuration for your tests.
I'm going to close this since it's not an actionable issue for enzyme.
I guess it's the first one, as I said in my issue description, because it seems to be specific to SVG. Other loaders (including inline ones) are working correctly. The only impossible-to-test React components were the ones with inline SVG. Others are completely fine to test!
I was wondering how people solved it. Is there a way to ignore this SVG stuff? It doesn't need to be tested.
Anyway, thank you both for your attention!
@diessica you might want to look into a way to mock SVG files for tests. I'm not personally sure how to do that with Mocha (we mainly use Jest and its pretty easy there) so you may want ask in the Mocha gitter channel or on StackOverflow
Yeah, that's the way to go!
Thank you! 😀
For anyone still struggling with this issue: This has nothing to do with Enzyme. Check your Webpack and Mocha configuration.
module not found message).--compiler CLI option).For people still struggling with this issue: solution above isn't enough, so I wrote an article about it:
:fire: How to Exclude CSS, Images, Anything from Unit Tests
It works for any JS testing framework (Mocha, ava, Jest, _whatever_).
Really hope it helps!
Most helpful comment
For people still struggling with this issue: solution above isn't enough, so I wrote an article about it:
:fire: How to Exclude CSS, Images, Anything from Unit Tests
It works for any JS testing framework (Mocha, ava, Jest, _whatever_).
Really hope it helps!