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 all 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
3 changes: 0 additions & 3 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
2 changes: 1 addition & 1 deletion lib/index.js
Expand Up @@ -13,4 +13,4 @@ function rewire(filename) {

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
18 changes: 18 additions & 0 deletions 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 @@ -17,12 +18,28 @@ function load(targetModule) {
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;
9 changes: 8 additions & 1 deletion 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 Down Expand Up @@ -54,7 +55,13 @@ function internalRewire(parentModulePath, targetPath) {
}

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

if (targetPath.match(/\.coffee$/)) {
moduleEnv.load(targetModule);
} else {
moduleEnv.compile(targetModule, src, targetPath);
}


return targetModule.exports;
}
Expand Down