Skip to content

Commit

Permalink
Merge 291fb1b into fff5037
Browse files Browse the repository at this point in the history
  • Loading branch information
killmenot committed Sep 15, 2017
2 parents fff5037 + 291fb1b commit ac608ca
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -5,7 +5,7 @@ rewire
[![](https://img.shields.io/npm/v/rewire.svg)](https://www.npmjs.com/package/rewire)
[![](https://img.shields.io/npm/dm/rewire.svg)](https://www.npmjs.com/package/rewire)
[![Dependency Status](https://david-dm.org/jhnns/rewire.svg)](https://david-dm.org/jhnns/rewire)
[![Build Status](https://travis-ci.org/jhnns/rewire.svg?branch=master)](https://travis-ci.org/rewire/jhnns)
[![Build Status](https://travis-ci.org/jhnns/rewire.svg?branch=master)](https://travis-ci.org/jhnns/rewire)
[![Coverage Status](https://img.shields.io/coveralls/jhnns/rewire.svg)](https://coveralls.io/r/jhnns/rewire?branch=master)

rewire adds a special setter and getter to modules so you can modify their behaviour for better unit testing. You may
Expand Down Expand Up @@ -140,7 +140,7 @@ Limitations
-----------

**Using `const`**<br>
It's not possible to rewire `const` (see [#79](https://github.com/jhnns/rewire/issues/79)). This can probably be solved with [proxies](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Proxy) someday but requires further research.
It's not possible to rewire `const` (see [#79](https://github.com/jhnns/rewire/issues/79)). This can probably be solved with [proxies](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Proxy) someday but requires further research.

**Transpilers**<br>
Some transpilers, like babel, rename variables in order to emulate certain language features. Rewire will not work in these cases (see [#62](https://github.com/jhnns/rewire/issues/62)). A possible solution might be switching to [babel-plugin-rewire](https://github.com/speedskater/babel-plugin-rewire).
Expand Down
7 changes: 4 additions & 3 deletions lib/index.js
Expand Up @@ -5,12 +5,13 @@ var rewireModule = require("./rewire.js");
* call myModule.__set__(name, value) and myModule.__get__(name) to manipulate private variables.
*
* @param {!String} filename Path to the module that shall be rewired. Use it exactly like require().
* @param {Object} options Options.
* @return {*} the rewired module
*/
function rewire(filename) {
return rewireModule(module.parent, filename);
function rewire(filename, options) {
return rewireModule(module.parent, filename, options);
}

module.exports = rewire;

delete require.cache[__filename]; // deleting self from module cache so the parent module is always up to date
delete require.cache[__filename]; // deleting self from module cache so the parent module is always up to date
6 changes: 4 additions & 2 deletions lib/rewire.js
Expand Up @@ -8,7 +8,7 @@ var Module = require("module"),
/**
* Does actual rewiring the module. For further documentation @see index.js
*/
function internalRewire(parentModulePath, targetPath) {
function internalRewire(parentModulePath, targetPath, options) {
var targetModule,
prelude,
appendix,
Expand All @@ -19,6 +19,8 @@ function internalRewire(parentModulePath, targetPath) {
throw new TypeError("Filename must be a string");
}

options = options || {}

// Resolve full filename relative to the parent module
targetPath = Module._resolveFilename(targetPath, parentModulePath);

Expand All @@ -34,7 +36,7 @@ function internalRewire(parentModulePath, targetPath) {
targetModule = new Module(targetPath, parentModulePath);

// We prepend a list of all globals declared with var so they can be overridden (without changing original globals)
prelude = getImportGlobalsSrc();
prelude = getImportGlobalsSrc(options.ignore);

// Wrap module src inside IIFE so that function declarations do not clash with global variables
// @see https://github.com/jhnns/rewire/issues/56
Expand Down

0 comments on commit ac608ca

Please sign in to comment.