Registry / testing / mocha-image-compiler

mocha-image-compiler

JSON →
library1.0.0jsnpmunverified

Mocha compiler that enables webpack-like importing of images (PNG, JPG, GIF, SVG) in Node.js test environments. Current version 1.0.0 is stable and rarely updated. It works by replacing image require calls with a simple path string, making it ideal for testing components that import images. Unlike webpack loaders, this is a lightweight, no-config solution specifically designed for Mocha's --compilers flag. Requires Mocha as a peer dependency.

npm install mocha-image-compiler
INSTALL
IMPORT
SIG · MOCHA-IMAGE-COMPIL
M
mocha-image-compiler
testingjavascriptv1.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

mocha-image-compiler
require('mocha-image-compiler') (CommonJS) or import 'mocha-image-compiler' (ESM wrapper); used via --compilers flag, not directly imported in test files
Used as a Mocha compiler, not imported in test code. The 'require' pattern is typical for Mocha config, but direct usage inside tests is not needed.

Installs mocha and mocha-image-compiler, creates a test file that imports a PNG image, and runs mocha with the compiler to replace image require with a path string.

npm install --save-dev mocha mocha-image-compiler echo "var logo = require('./logo.png'); console.log(logo);" > test.js echo '{}' > package.json mocha --compilers .:mocha-image-compiler test.js
Debug
Known issues
gotchaOnly works with Mocha's --compilers flag; does not support webpack or other bundlers.
fix
Use only in Mocha test environment; for bundlers use appropriate loaders (e.g., file-loader).
affects: >=1.0.0
gotchaReturns the file path as a string, not the actual image data or a module. This may differ from webpack behavior where require returns a URL or data URI.
fix
Assume require('./image.png') returns a string path; do not expect a Buffer or URL object.
affects: >=1.0.0
gotchaRequires Mocha to be installed separately; not included as a dependency.
fix
Install mocha alongside mocha-image-compiler: npm install --save-dev mocha
affects: >=1.0.0
gotchaDoes not support dynamic require() calls; only static require paths are transformed.
fix
Use only static string paths in require(); do not use variables or expressions.
affects: >=1.0.0
gotchaMay conflict with other Mocha compilers if not ordered correctly (e.g., with babel/register).
fix
Specify compilers in correct order: mocha --compilers js:babel/register,.:mocha-image-compiler
affects: >=1.0.0
Errors
Common errors & fixes
Error: Cannot find module 'mocha-image-compiler'
Package not installed globally or missing in node_modules.
fix
Run npm install --save-dev mocha-image-compiler in your project.
Error: compiler not found: mocha-image-compiler
Incorrect compiler syntax in --compilers flag.
fix
Use --compilers .:mocha-image-compiler (dot colon package-name). Ensure the dot matches the file extension (e.g., . for any extension, or .js:...).
ReferenceError: require is not defined in ES module scope
Mocha is running with ESM ('type': 'module' in package.json) but using require() syntax.
fix
Add --experimental-modules or use import statements. Alternatively, set 'type': 'commonjs' or use .cjs extension.
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies
mocharequiredRuns as a Mocha compiler; Mocha is required as a peer dependency.
Agent activity
2 hits · last 30 days
node
2
Resources
mocha-image-compiler — npm install mocha-image-compiler · libregistry