Module Fu
A small library providing utility methods to manage modules, particularly loading and uncaching (removing from cache). Handy for unit testing where having freshly loaded modules is wanted.
Installation
npm install module-fu --save-dev
This example assumes you'll be using it in development only.
Example
var mf = ; mf; var expect = expect; ;
Tests
npm test
API
setResolver(resolverFn)
Description
Sets the function used to resolve module references. Usually needs setting from where this module is loaded.
Parameters
- resolverFn - A function that performs the equivalent of require.resolve()
Example
var mf = ; mf; var mcm = mf;
remove(moduleName)
Description
Removes a module from the cache
Parameters
- moduleName - the name of the module to remove from the cache.
find(moduleName[, callback])
Description
Searches the cache for references to a module.
Parameters
- moduleName - the name of the module to search for.
- callback - optional callback. If specified it will be called for each occurrence found.
Returns
If no callback is specified => an array of results found. If a callback is specified => undefined.
load(moduleName[, importName])
Description
Loads a module and optionally retrieves an import. Designed to give nice messages when the module or import is not found so mistakes in writing tests are picked up quickly.
Parameters
- moduleName - the name of the module to load.
- importName - Optional name of the import to get and return for the module.
Returns
If an importName is specified => The module exported property with the same name. If no importName is specified => The module is returned.
reload(moduleName[, importName])
Description
Reloads a module and optionally retrieves an import. Similar to load but first removes the module from the cache.
Parameters
- moduleName - the name of the module to reload.
- importName - Optional name of the import to get and return for the module.
Returns
If an importName is specified => The module exported property with the same name. If no importName is specified => The module is returned.
Credits
Adapted from this answer to a StackOverflow question.