babel/register style hook to ignore style imports when running in Node. This
is for projects that use something like Webpack to enable CSS imports in
example) you'll see errors like this:
SyntaxError: /Users/brandon/code/my-project/src/components/my-component/style.sass: Unexpected token (1:0) > 1 | .title | ^ 2 | font-family: serif 3 | font-size: 10em 4 |
To resolve this, require
ignore-styles with your mocha tests:
mocha --require ignore-styles
See DEFAULT_EXTENSIONS for the full list of extensions ignored, and send a pull request if you need more.
Note: This is not for use inside Webpack. If you want to ignore extensions in Webpack you'll want to use a loader like ignore-loader. This is for use in Node outside of your normal Webpack build.
$ npm install --save-dev ignore-styles
To use this with multiple Mocha requires:
mocha --require babel-register --require ignore-styles
You can also use it just like
To customize the extensions used:
To customize the extensions in ES5:
By default, a no-op handler is used that doesn't actually do anything. If you'd like to substitute your own custom handler to do fancy things, pass it as a second argument:
The first argument to
register is the list of extensions to handle. Leaving it
undefined, as above, uses the default list. The handler function receives two arguments,
Why is this useful? One example is when using something like react-css-modules. You need the style imports to actually return something so that you can test the components, or the wrapper component will throw an error. Use this to provide test class names.
Another use case would be to simply return the filename of an image so that it can be verified in unit tests:
const _ = ;const path = ;
If the filename ends in '.png' or '.jpg', then the basename of the file is returned as the value of the module on import.
The MIT License (MIT)
Copyright (c) 2015 Brainspace Corporation