Skip to content

Instantly share code, notes, and snippets.

@ryanseddon
Created October 9, 2015 05:44
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ryanseddon/e76fd16af2f8f4f4bed8 to your computer and use it in GitHub Desktop.
Save ryanseddon/e76fd16af2f8f4f4bed8 to your computer and use it in GitHub Desktop.
Mocha compiler for css-module support in tests using sass
mocha --compilers js:babel/register,js:./test/css-modules-compiler.js --recursive -w
var hook = require('css-modules-require-hook');
var sass = require('node-sass');
hook({
extensions: ['.scss'],
preprocessCss: function (css) {
var result = sass.renderSync({
data: css
});
return result.css;
}
});
@ryanseddon
Copy link
Author

Using this will actually render the classes in your react components

screen shot 2015-10-09 at 16 30 10

@tomduncalf
Copy link

Amazing, thanks so much for posting this, just what I needed!

@stipsan
Copy link

stipsan commented Mar 20, 2016

Hey, if you're doing relative scss imports in your files (like I did) you may run into problems.

It's easy to fix though:

var hook = require('css-modules-require-hook');
var sass = require('node-sass');
var path = require('path');

hook({
  extensions: ['.scss'],
  preprocessCss: function (css, filepath) {
    var result =  sass.renderSync({
      data: css,
      includePaths: [ path.resolve(filepath, '..') ]
    });

    return result.css;
  }
});

@MathieuLorber
Copy link

Thank you ! And thanks for the imports trick

@pahund
Copy link

pahund commented Apr 28, 2016

Thank you @ryanseddon and @stipsan, this helped me a lot! :-)

@potapovDim
Copy link

node_modules/node-sass/lib/index.js:420
throw util._extend(new Error(), JSON.parse(result.error));
^

Error: Undefined variable: "$font-logo".

@rpoitras
Copy link

@stipsan and @ryanseddon thank you very much for taking the time to share!

@jhohlfeld
Copy link

As of babel 6 it's rather mocha --compilers js:babel-core/register,js:./test/css-modules-compiler.js

babel-core instead of just 'babel`

@thetallweeks
Copy link

Has anyone had issues with empty css files throwing?

throw new Error('No input specified: provide a file name or a source string to process');

It's coming from an external dependency so I can't remove the file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment