When testing .js files that have Webpack CSS import './style.css' , such as import './style.css' , Mocha throws a syntax error (because it tries to import and parse the CSS file as JS). There is a solution for which has already been published on Stack Overflow , but it is only addressed if you are not already using the compiler with Mocha. I am using Babel 5. I have tried the following, but it seems that Mocha does not support passing multiple compilers:
// npm test script mocha ./src/**/*Test.js --compilers css:./scripts/mocha-webpack-compiler.js js:babel/register // scripts/mocha-webpack-compiler.js function noop() { return null; } require.extensions['.css'] = noop;
Is there a way to have multiple Mocha compilers or a better way to tell Mocha not to try to parse the Webpack CSS import?
EDIT:
I like the proposed @Giles B solution below; that was exactly what i needed. However, since I'm still on Babylon 5, I needed a few settings, as shown below:
mocha.opts
--require scripts/support/babelhook --require scripts/support/mocha-webpack-compiler
Scripts /babelhook.js
require('babel/register');
Scripts / mocha WebPack-compiler.js
function noop() { return null; } require.extensions['.css'] = noop;
mocha script
mocha ./src*Test.js
This works for me using babel and babel-core , as of version 5.8.23 .
javascript css webpack babeljs mocha
trevordmiller Nov 23 '15 at 21:33 2015-11-23 21:33
source share