Jest's preprocessor for css, sass, less, stylus modules generated with Webpack
Usage no npm install needed!
<script type="module">
import jestCssModulesTransform from 'https://cdn.skypack.dev/jest-css-modules-transform';
</script>
README
jest-css-modules-transform
Preprocessor css modules for Jest test framework
This preprocessor converts css files in modules like Webpack.
If we have css files
.class1, .class2, .class3 {
display: block;
}
Webpack will transfrom it to object.
{
class1: 'class1', //value may be different. It depends of localIndentName property
class2: 'class2',
class3: 'class3',
}
In testing you need to mock all css modules to aviod exception. But use pure object {} for mocking is bad idea, because it makes wrong classNames in components. This preprocessor makes correct modules as if Webpack did.
It supports pure CSS, SCSS, SASS, STYLUS and LESS.
For PostCSS
For .css, .pcss, .postcss files used postcss.config.js from root folder(if file exist of course).
For tests you can rewrite your existed postcss.config.js. See options.
Options
You can save preproccessor options in file jest-css-modules-transform-config.js in root of your project(Where is the file package.json).
You can pass options for your preprocessors.
Pass urls for prepend(before file content) to transformed files.
Useful in a situation where the module uses variables or mixins without explicit import.
For example you have vars in file, but without implicit import. You can prepend file with sass variables before convert module.
injectIntoDOM
Type: boolean
Default: false
Inject the generated CSS into the head of the document.
This option could be useful for visual regression testing, where the output dom has the styles applied to it.
sassModuleName
Type: string
Default: null
The default nodejs module for sass/scss files is sass. If not found in node_modules, then node-sass. You can define any another module.
It works with any preprocessors(Sass, Less, Styles, PostCSS, pure CSS)
Custom config path
By default option plugin's config file is jest-css-modules-transform-config.js from root. For specify custom path just add env JEST_CSS_MODULES_TRANSFORM_CONFIG. For example:
JEST_CSS_MODULES_TRANSFORM_CONFIG=custom-config-path npm test
or
JEST_CSS_MODULES_TRANSFORM_CONFIG=custom-config-path yarn test