mocha-stylus-compiler
raw JSON → 1.0.1 verified Fri May 01 auth: no javascript maintenance
A Stylus compiler plugin for Mocha that allows requiring .styl files in tests, compiling them to CSS but ignoring the output (compiles to null). Version 1.0.1 is the latest and only stable release. It is a minimal tool for integrating Stylus preprocessing into Mocha test runs. Unlike alternative approaches (like using stylus directly or other Mocha compilers), this package specifically registers .styl files with Mocha's --compilers option, preventing parse errors when requiring Stylus files in tests. No updates since initial release; considered stable but unmaintained.
Common errors
error Error: Cannot find module 'mocha-stylus-compiler' ↓
cause Package not installed, or not in node_modules when Mocha runs (often due to global Mocha installation).
fix
Install locally:
npm install --save-dev mocha-stylus-compiler and use local Mocha: npx mocha --compilers styl:mocha-stylus-compiler error Error: require.extensions is not supported by webpack ↓
cause Webpack does not allow modifying require.extensions, which this package relies on. This package is intended for Node.js Mocha, not webpack.
fix
Use a webpack loader for Stylus files (e.g., stylus-loader) instead of this package.
error Error: Cannot find module 'stylus' ↓
cause The package depends on `stylus` as a peer dependency, but it may not be installed.
fix
Install stylus:
npm install --save-dev stylus Warnings
gotcha The package compiles Stylus to CSS but discards the output, so requiring a .styl file returns an empty object `{}`. This may be unexpected if you intend to actually use the compiled CSS. ↓
fix If you need to use the compiled CSS in tests, consider using a different approach (e.g., compile Stylus files separately and read the output).
deprecated Mocha v8+ deprecated the --compilers option in favor of --require with a custom compiler. The package may not work with newer Mocha versions. ↓
fix For Mocha v8+, use `--require mocha-stylus-compiler/register` if available, or switch to a compiler that supports the new API. Alternatively, downgrade Mocha to v7 or earlier.
gotcha The package only compiles .styl files with the extension `.styl`. If you use a different extension (e.g., .stylus), the compiler will not be applied. ↓
fix Rename your files to .styl or modify the compiler registration to match your extension.
Install
npm install mocha-stylus-compiler yarn add mocha-stylus-compiler pnpm add mocha-stylus-compiler Imports
- default wrong
import mochaStylus from 'mocha-stylus-compiler'correctrequire('mocha-stylus-compiler')
Quickstart
// Install mocha and mocha-stylus-compiler
// npm install --save-dev mocha mocha-stylus-compiler
// Create a test file test/example.test.js
const assert = require('assert');
const styles = require('./styles.styl'); // This will compile to an empty object
describe('Stylus', function() {
it('should compile without error', function() {
assert.equal(typeof styles, 'object');
});
});
// Run: npx mocha --compilers styl:mocha-stylus-compiler