Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable mocking of const #117

Merged
merged 7 commits into from Nov 11, 2017
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -4,6 +4,7 @@ node_js:
- "0.12"
- "4"
- "5"
- "6"

script:
- npm test
Expand Down
10 changes: 6 additions & 4 deletions README.md
Expand Up @@ -139,9 +139,6 @@ myModule.__with__({
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.

**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 Expand Up @@ -182,10 +179,15 @@ Please be aware that you can't rewire `eval()` or the global object itself.
API
---

### rewire(filename: String): rewiredModule
### rewire(filename: String, [options]): rewiredModule

Returns a rewired version of the module found at `filename`. Use `rewire()` exactly like `require()`.

#### Options
| Property | Default | Description |
|----------|---------|-------------|
| convertConst | false | Set to true to convert all `const` variables of the required module to `let`. This way you can mock const variables. **Caution**: Setting this to true can lead to inaccurate tests.

### rewiredModule.&#95;&#95;set&#95;&#95;(name: String, value: *): Function

Sets the internal variable `name` to the given `value`. Returns a function which can be called to revert the change.
Expand Down
6 changes: 3 additions & 3 deletions lib/index.js
Expand Up @@ -7,10 +7,10 @@ var rewireModule = require("./rewire.js");
* @param {!String} filename Path to the module that shall be rewired. Use it exactly like require().
* @return {*} the rewired module
*/
function rewire(filename) {
return rewireModule(module.parent, filename);
function rewire(filename, opts) {
return rewireModule(module.parent, filename, opts);
}

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
20 changes: 19 additions & 1 deletion lib/moduleEnv.js
Expand Up @@ -2,6 +2,7 @@

var Module = require("module"),
fs = require("fs"),
babelCore = require("babel-core"),
coffee;

// caching original wrapper
Expand All @@ -11,18 +12,34 @@ var moduleWrapper0 = Module.wrapper[0],
nodeRequire,
currentModule;

function load(targetModule) {
function load(targetModule, isTransform) {
nodeRequire = targetModule.require;
targetModule.require = requireProxy;
currentModule = targetModule;

registerExtensions();

targetModule.load(targetModule.id);

// This is only necessary if nothing has been required within the module
reset();
}

function compile(targetModule, src, filename) {
nodeRequire = targetModule.require;
targetModule.require = requireProxy;
targetModule.filename = filename;
currentModule = targetModule;

var convertedSrc = babelCore.transform(stripBOM(src), {
plugins: ["transform-es2015-constants"]
});

targetModule._compile(convertedSrc.code, filename);

reset();
}

function reset() {
Module.wrapper[0] = moduleWrapper0;
Module.wrapper[1] = moduleWrapper1;
Expand Down Expand Up @@ -91,4 +108,5 @@ try {
}

exports.load = load;
exports.compile = compile;
exports.inject = inject;
16 changes: 13 additions & 3 deletions lib/rewire.js
@@ -1,5 +1,6 @@
var Module = require("module"),
fs = require("fs"),
path = require("path"),
getImportGlobalsSrc = require("./getImportGlobalsSrc.js"),
getDefinePropertySrc = require("./getDefinePropertySrc.js"),
detectStrictMode = require("./detectStrictMode.js"),
Expand All @@ -8,11 +9,15 @@ var Module = require("module"),
/**
* Does actual rewiring the module. For further documentation @see index.js
*/
function internalRewire(parentModulePath, targetPath) {
function internalRewire(parentModulePath, targetPath, opts) {
var targetModule,
prelude,
appendix,
src;
src,
isTransform;

opts = typeof opts === "object" ? opts : {};
isTransform = !!opts.convertConst;

// Checking params
if (typeof targetPath !== "string") {
Expand Down Expand Up @@ -54,7 +59,12 @@ function internalRewire(parentModulePath, targetPath) {
}

moduleEnv.inject(prelude, appendix);
moduleEnv.load(targetModule);

if(isTransform) {
moduleEnv.compile(targetModule, src, targetPath);
} else {
moduleEnv.load(targetModule);
}

return targetModule.exports;
}
Expand Down