最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Requireimport svg during mocha tests - Stack Overflow

programmeradmin1浏览0评论

I have a project being built with webpack. This allows me to import .svg files to create React components.

When running tests I have been attempting to avoid using webpack to avoid tying the mocha version to a webpack plugin. Unfortunately, when the .svg imports are hit, they fail to be found. We are also using css modules, and they allowed me to use the css-modules-require-hook to work around the importing of css files.

Is there a technique I could use to accomplish the same thing with SVGs?

I have a project being built with webpack. This allows me to import .svg files to create React components.

When running tests I have been attempting to avoid using webpack to avoid tying the mocha version to a webpack plugin. Unfortunately, when the .svg imports are hit, they fail to be found. We are also using css modules, and they allowed me to use the css-modules-require-hook to work around the importing of css files.

Is there a technique I could use to accomplish the same thing with SVGs?

Share Improve this question edited Jun 5, 2017 at 18:19 Shepmaster 431k111 gold badges1.2k silver badges1.5k bronze badges asked May 16, 2016 at 17:14 MattMatt 2,8152 gold badges30 silver badges48 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 15

I saw this solved by using require.extensions (in node this is deprecated, but should never go away) to force importing these types of assets to result in an no-op. Starting mocha with the --require flag lets us configure the runtime environment for our tests so starting mocha like this:

NODE_PATH=./app mocha -w --compilers js:babel-core/register --require ./app/lib/testHelper.js --require ./app/lib/testNullCompiler.js 'app/**/*.spec.@(js|jsx)' --watch-extensions js,jsx

where testNullCompiler.js is:

const noop = () => 1;

require.extensions['.css'] = noop;
require.extensions['.scss'] = noop;
require.extensions['.png'] = noop;
require.extensions['.jpg'] = noop;
require.extensions['.jpeg'] = noop;
require.extensions['.gif'] = noop;
require.extensions['.svg'] = noop;

This will cause all the above types of files to return the noop function instead of attempting to load the actual file.

My code is using the es6 import syntax but I'm assuming that babel is converting that to require under the covers which enables this technique to work.

Yes, I just find a way to mock .svg module for mocha here.

Mocha can use require hooks to deal with universal javascript import, such as babel-register for .js.

You can use require-hacker to add hooks to extensions of modules.

You can use a null react component mock for other component loaders such as react-svg-loader to load .svg as a react component.

here is the example:

import requireHacker from 'require-hacker';

const fakeComponentString = `
  require('react').createClass({
    render() {
      return null;
    }
  })
`;

// for fake component
requireHacker.hook('svg', path => `module.exports = ${fakeComponentString}`);
发布评论

评论列表(0)

  1. 暂无评论