0.0.0 • Published 4 years ago

rewired v0.0.0

Weekly downloads
2
License
ISC
Repository
-
Last release
4 years ago

rewired

Rewires part of the exposed interface of a CommonJS module for mocking purposes.

NPM Version Build Status Code Status

Stupid simple module inspired by rewire to easily mock the exposed interface of a module when it's not explicitly defined inside the scope of the module (i.e. only in the exports).

install

npm i -D rewired

usage

Lets say you have flux web API utility that fetches data from a server and when successful, calls a server action creator to dispatch it to a store.

/**
 * @module DataServerAction
 */

var dispatcher = require('./dispatcher');

module.exports = {
	// NOT explicitly defined inside the scope of the module, only in the exports.
	receiveData: function (data) {
		dispatcher.dispatch({
			type: 'RECEIVE_DATA',
			payload: data
		});
	}
};
/**
 * @module WebApiUtil
 */
var DataServerAction = require('./DataServerAction');
var $ = require('jquery');

module.exports = {
	fetch: function (url) {
		$.ajax({
			type: 'GET',
			url: url
		})

		.done(DataServerAction.receiveData)

		.fail(function (xhr, status, err) {
			console.error('Request Error: ', err);
		});
	}
};

If you want to temporarily override the original implementation of the server action creator for testing purposes and when finished restore it, use rewired.

/**
 * Web API Util Tests.
 */

var api = require('../web-api-util');
var DataServerAction = require('../DataServerAction');
var rewired = require('rewired');

describe('Web API Util', function () {
	context('fetch()', function () {
		before(function (done) {
			this.revert = rewired(DataServerAction, 'receiveData', function (data) {
				console.log('Overridden the "original" function!');

				this.data = data;
				done();
			}.bind(this));

			api.fetch('https://www.api.com/v1/data');
		});

		it('calls DataServerAction.receiveData(data)', function () {
			expect(this.data).to.exist;
		});

		after(function () {
			this.revert();
		});
	});
});

This principle can be applied with any CommonJS module (node.js or browserify), with any properties.

The example above uses mocha to describe the test scenario.

interface

rewired exposes the following properties:

rewired(targetModule, targetKey, override)

Rewires a targetModule property identified by the targetKey with an override.

Returns a method to revert back to the original implementation.

var superModule = require('./super-module'); // exposes a 'port' property
var rewired = require('rewired');

console.log(superModule.port); // 9999

// Rewire with a mock implementation.
var revert = rewired(superModule, 'port', 8080);

console.log(superModule.port); // 8080

// Revert back to the original implementation;
revert();

console.log(superModule.port); // 9999

testing

npm test

code coverage

npm run cover

istanbul and coveralls are used to generate a code coverage report.

linting

jshint is used to lint the code.

The code will be linted when running npm test by means of the 'pretest' hook.

0.0.0

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago