WebPack CSS import processing when testing with Mocha

Using WebPack, you can import styles into your code as follows: import './PageSpinner.styl'; But when you try to test this code using Mocha, your tests will be broken using SyntaxError, because the engine is trying to handle styles such as JS code.

How can I test such code with Mocha?

+10
javascript webpack mocha
Sep 20 '15 at 19:34
source share
2 answers

Recently, I had the same problem and the solution was implemented using Mocha compilers.

create a file, name 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 
+27
Sep 29 '15 at 15:57
source share

This can be done using ignore-styles .

Install the package and then run mocha.

eg.

 mocha --require babel-register --require ignore-styles 
+3
Nov 29 '16 at 23:26
source share



All Articles