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

javascript - Handle WebPack CSS imports when testing with Mocha - Stack Overflow

programmeradmin0浏览0评论

With WebPack you can import styles in your code like this: import './PageSpinner.styl'; But when you try to test this code with Mocha, your tests will be crashed with SyntaxError because engine tries to handle styles like JS code.

How can I test code like this with Mocha?

With WebPack you can import styles in your code like this: import './PageSpinner.styl'; But when you try to test this code with Mocha, your tests will be crashed with SyntaxError because engine tries to handle styles like JS code.

How can I test code like this with Mocha?

Share Improve this question asked Sep 20, 2015 at 19:34 andre487andre487 1,3992 gold badges18 silver badges30 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 27

I had the same problem lately and the solution was through Mocha compilers.

create a file, let's call it 'css-null-compiler.js' and it has:

function noop() {
  return null;
}

require.extensions['.styl'] = noop;
// you can add whatever you wanna handle
require.extensions['.scss'] = noop;
require.extensions['.png'] = noop;
// ..etc

when you run mocha from the command line, pass this file as a compiler

mocha /your/test.spec.js --compilers css:css-null-compiler.js

This can be done with the ignore-styles package.

Install the package and then require when running mocha.

e.g.

mocha --require babel-register --require ignore-styles
发布评论

评论列表(0)

  1. 暂无评论