0.1.9 • Published 10 years ago

module-fu v0.1.9

Weekly downloads
3
License
MIT
Repository
github
Last release
10 years ago

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 = require('module-fu');
  mf.setResolver(function(moduleName) { return require.resolve(moduleName); });
  var expect = require('chai').expect;

  describe('my-cool-module', function() {	
    it('rocks because I reload the module.', function() {
    	var info = mf.find('./my-cool-module.js');
    	expect(info.length).to.equal(0);

    	var mcm = mf.load('./my-cool-module.js');
    	info = mf.find('./my-cool-module.js');
    	expect(info.length).to.equal(1);
    	expect(d1.hello()).to.equal('hello world');

    	mf.remove('./my-cool-module.js');
    	info = mf.find('./my-cool-module.js');
    	expect(info.length).to.equal(0);
	});
  });

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 = require('module-fu');
  mf.setResolver(function(moduleName) { return require.resolve(moduleName); });
  var mcm = mf.load('./my-cool-module.js');

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.

0.1.9

10 years ago

0.1.8

10 years ago

0.1.5

10 years ago

0.1.4

10 years ago

0.1.3

10 years ago

0.1.2

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago