Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/twbs/rewire.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjeroenvalcke <valcke_jeroen@hotmail.com>2017-11-13 19:43:47 +0300
committerjeroenvalcke <valcke_jeroen@hotmail.com>2017-11-13 19:43:47 +0300
commit0a77533c6b7a41606975303be2327c250cd77d19 (patch)
tree58b7025a7f49f19d8015720601410c857eeaf495
parentc176d025b57ff616d2a63989a685fe70651e33d3 (diff)
parentcce2de832c99e1f1491c9766bc6a2f1b4d0a3bd2 (diff)
Merge remote-tracking branch 'upstream/master'
-rw-r--r--.travis.yml6
-rw-r--r--CHANGELOG.md4
-rw-r--r--README.md15
-rw-r--r--lib/index.js4
-rw-r--r--lib/moduleEnv.js49
-rw-r--r--lib/rewire.js23
-rw-r--r--package-lock.json310
-rw-r--r--package.json10
-rw-r--r--test/getImportGlobalsSrc.test.js15
-rw-r--r--test/rewire.test.js4
-rw-r--r--testLib/constModule.js (renamed from testLib/ES2015Module.js)7
-rw-r--r--testLib/sharedTestCases.js59
-rw-r--r--testLib/throwError.js5
13 files changed, 188 insertions, 323 deletions
diff --git a/.travis.yml b/.travis.yml
index 929d5ed..5f98a89 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,9 +1,9 @@
language: node_js
node_js:
- - "0.10"
- - "0.12"
- "4"
- - "5"
+ - "6"
+ - "8"
+ - "9"
script:
- npm test
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 35db73c..a856b08 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,6 +1,10 @@
Changelog
---------
+### 3.0.0
+- **Breaking:** Remove support for node versions below 4
+- Add support for `const` [#79](https://github.com/jhnns/rewire/issues/79) [#95](https://github.com/jhnns/rewire/issues/95) [#117](https://github.com/jhnns/rewire/pull/117) [#118](https://github.com/jhnns/rewire/pull/118)
+
### 2.5.2
- Fix cluttering of `require.extensions` even if CoffeeScript is not installed [#98](https://github.com/jhnns/rewire/pull/98)
diff --git a/README.md b/README.md
index 03b2866..c8a589d 100644
--- a/README.md
+++ b/README.md
@@ -14,9 +14,7 @@ rewire adds a special setter and getter to modules so you can modify their behav
- inspect private variables
- override variables within the module.
-rewire does **not** load the file and eval the contents to emulate node's require mechanism. In fact it uses node's own require to load the module. Thus your module behaves exactly the same in your test environment as under regular circumstances (except your modifications).
-
-**Please note:** The current version of rewire is not compatible with `const` or [babel](http://babeljs.io/). See [Limitations](https://github.com/jhnns/rewire#limitations).
+**Please note:** The current version of rewire is only compatible with CommonJS modules. See [Limitations](https://github.com/jhnns/rewire#limitations).
<br>
@@ -139,8 +137,8 @@ myModule.__with__({
Limitations
-----------
-**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).
+**Babel's ES module emulation**<br>
+During the transpilation step from ESM to CJS modules, Babel renames internal variables. Rewire will not work in these cases (see [#62](https://github.com/jhnns/rewire/issues/62)). Other Babel transforms, however, should be fine. Another solution might be switching to [babel-plugin-rewire](https://github.com/speedskater/babel-plugin-rewire).
**Variables inside functions**<br>
Variables inside functions can not be changed by rewire. This is constrained by the language.
@@ -179,15 +177,10 @@ Please be aware that you can't rewire `eval()` or the global object itself.
API
---
-### rewire(filename: String, [options]): rewiredModule
+### rewire(filename: String): 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.
diff --git a/lib/index.js b/lib/index.js
index 765de10..0edcd2c 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -7,8 +7,8 @@ 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, opts) {
- return rewireModule(module.parent, filename, opts);
+function rewire(filename) {
+ return rewireModule(module.parent, filename);
}
module.exports = rewire;
diff --git a/lib/moduleEnv.js b/lib/moduleEnv.js
index 29c407c..789ea2f 100644
--- a/lib/moduleEnv.js
+++ b/lib/moduleEnv.js
@@ -3,47 +3,32 @@
var Module = require("module"),
fs = require("fs"),
babelCore = require("babel-core"),
+ // Requiring the babel plugin here because otherwise it will be lazy-loaded by Babel during rewire()
+ transformBlockScoping = require("babel-plugin-transform-es2015-block-scoping"),
coffee;
- // caching original wrapper
var moduleWrapper0 = Module.wrapper[0],
moduleWrapper1 = Module.wrapper[1],
originalExtensions = {},
+ matchCoffeeExt = /\.coffee$/,
nodeRequire,
currentModule;
-function load(targetModule, isTransform) {
+function load(targetModule) {
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;
- restoreExtensions();
}
function inject(prelude, appendix) {
@@ -64,23 +49,48 @@ function requireProxy(path) {
}
function registerExtensions() {
+ var originalJsExtension = require.extensions[".js"];
var originalCoffeeExtension = require.extensions[".coffee"];
+ if (originalJsExtension) {
+ originalExtensions.js = originalJsExtension;
+ }
if (originalCoffeeExtension) {
originalExtensions.coffee = originalCoffeeExtension;
}
+ require.extensions[".js"] = jsExtension;
require.extensions[".coffee"] = coffeeExtension;
}
function restoreExtensions() {
+ if ("js" in originalExtensions) {
+ require.extensions[".js"] = originalExtensions.js;
+ }
if ("coffee" in originalExtensions) {
require.extensions[".coffee"] = originalExtensions.coffee;
}
}
+function jsExtension(module, filename) {
+ var _compile = module._compile;
+
+ module._compile = function (content, filename) {
+ content = babelCore.transform(content, {
+ plugins: ["transform-es2015-block-scoping"],
+ retainLines: true,
+ filename: filename
+ }).code;
+ _compile.call(module, content, filename);
+ };
+
+ restoreExtensions();
+ originalExtensions.js(module, filename);
+}
+
function coffeeExtension(module, filename) {
var content = stripBOM(fs.readFileSync(filename, "utf8"));
+ restoreExtensions();
content = coffee.compile(content, {
filename: filename,
bare: true
@@ -108,5 +118,4 @@ try {
}
exports.load = load;
-exports.compile = compile;
exports.inject = inject;
diff --git a/lib/rewire.js b/lib/rewire.js
index 1d240d4..e678f37 100644
--- a/lib/rewire.js
+++ b/lib/rewire.js
@@ -9,15 +9,11 @@ var Module = require("module"),
/**
* Does actual rewiring the module. For further documentation @see index.js
*/
-function internalRewire(parentModulePath, targetPath, opts) {
+function internalRewire(parentModulePath, targetPath) {
var targetModule,
prelude,
appendix,
- src,
- isTransform;
-
- opts = typeof opts === "object" ? opts : {};
- isTransform = !!opts.convertConst;
+ src;
// Checking params
if (typeof targetPath !== "string") {
@@ -27,14 +23,6 @@ function internalRewire(parentModulePath, targetPath, opts) {
// Resolve full filename relative to the parent module
targetPath = Module._resolveFilename(targetPath, parentModulePath);
- // Special support for older node versions that returned an array on Module._resolveFilename
- // @see https://github.com/joyent/node/blob/865b077819a9271a29f982faaef99dc635b57fbc/lib/module.js#L319
- // TODO Remove this switch on the next major release
- /* istanbul ignore next because it will be removed soon */
- if (Array.isArray(targetPath)) {
- targetPath = targetPath[1];
- }
-
// Create testModule as it would be created by require()
targetModule = new Module(targetPath, parentModulePath);
@@ -59,12 +47,7 @@ function internalRewire(parentModulePath, targetPath, opts) {
}
moduleEnv.inject(prelude, appendix);
-
- if(isTransform) {
- moduleEnv.compile(targetModule, src, targetPath);
- } else {
- moduleEnv.load(targetModule);
- }
+ moduleEnv.load(targetModule);
return targetModule.exports;
}
diff --git a/package-lock.json b/package-lock.json
index 18e4793..2571170 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -105,27 +105,16 @@
"babel-runtime": "6.26.0"
}
},
- "babel-plugin-transform-es2015-constants": {
- "version": "6.1.4",
- "resolved": "https://registry.npmjs.org/babel-plugin-transform-es2015-constants/-/babel-plugin-transform-es2015-constants-6.1.4.tgz",
- "integrity": "sha1-5LjHj7SKuYsBB/Mp+rYEDnnDWjM=",
+ "babel-plugin-transform-es2015-block-scoping": {
+ "version": "6.26.0",
+ "resolved": "https://registry.npmjs.org/babel-plugin-transform-es2015-block-scoping/-/babel-plugin-transform-es2015-block-scoping-6.26.0.tgz",
+ "integrity": "sha1-1w9SmcEwjQXBL0Y4E7CgnnOxiV8=",
"requires": {
- "babel-runtime": "5.8.38"
- },
- "dependencies": {
- "babel-runtime": {
- "version": "5.8.38",
- "resolved": "https://registry.npmjs.org/babel-runtime/-/babel-runtime-5.8.38.tgz",
- "integrity": "sha1-HAsC62MxL18If/IEUIJ7QlydTBk=",
- "requires": {
- "core-js": "1.2.7"
- }
- },
- "core-js": {
- "version": "1.2.7",
- "resolved": "https://registry.npmjs.org/core-js/-/core-js-1.2.7.tgz",
- "integrity": "sha1-ZSKUwUZR2yj6k70tX/KYOk8IxjY="
- }
+ "babel-runtime": "6.26.0",
+ "babel-template": "6.26.0",
+ "babel-traverse": "6.26.0",
+ "babel-types": "6.26.0",
+ "lodash": "4.17.4"
}
},
"babel-register": {
@@ -224,6 +213,12 @@
"concat-map": "0.0.1"
}
},
+ "browser-stdout": {
+ "version": "1.3.0",
+ "resolved": "https://registry.npmjs.org/browser-stdout/-/browser-stdout-1.3.0.tgz",
+ "integrity": "sha1-81HTKWnTL6XXpVZxVCY9korjvR8=",
+ "dev": true
+ },
"chalk": {
"version": "1.1.3",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz",
@@ -243,49 +238,16 @@
}
}
},
- "coffee": {
- "version": "4.1.0",
- "resolved": "https://registry.npmjs.org/coffee/-/coffee-4.1.0.tgz",
- "integrity": "sha512-u+FW/4zAddbyKhyMz1A/2pbxmBTbGLdx6U3rzl7m6X1qUkrL6CKvt9b1oEGpmMVQKljWSqivPMf9BNQObmZhaQ==",
- "dev": true,
- "requires": {
- "cross-spawn": "5.1.0",
- "debug": "2.6.9"
- },
- "dependencies": {
- "debug": {
- "version": "2.6.9",
- "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
- "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==",
- "dev": true,
- "requires": {
- "ms": "2.0.0"
- }
- },
- "ms": {
- "version": "2.0.0",
- "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
- "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=",
- "dev": true
- }
- }
- },
"coffee-script": {
"version": "1.12.7",
"resolved": "https://registry.npmjs.org/coffee-script/-/coffee-script-1.12.7.tgz",
"integrity": "sha512-fLeEhqwymYat/MpTPUjSKHVYYl0ec2mOyALEMLmzr5i1isuG+6jfI2j2d5oBO3VIzgUXgBVIcOT9uH1TFxBckw==",
"dev": true
},
- "coffeescript": {
- "version": "2.0.1",
- "resolved": "https://registry.npmjs.org/coffeescript/-/coffeescript-2.0.1.tgz",
- "integrity": "sha512-mrgXUOwRocrvSRwIi/2tAesQOJz2KxSyMNMlLSVhnJk/eZHKinYSJGcgUYZkzmSQZtpb3UhGJeZv0AAUeh7yyQ==",
- "dev": true
- },
"commander": {
- "version": "2.3.0",
- "resolved": "https://registry.npmjs.org/commander/-/commander-2.3.0.tgz",
- "integrity": "sha1-/UMOiJgy7DU7ms0d4hfBHLPu+HM=",
+ "version": "2.11.0",
+ "resolved": "https://registry.npmjs.org/commander/-/commander-2.11.0.tgz",
+ "integrity": "sha512-b0553uYA5YAEGgyYIGYROzKQ7X5RAqedkfjiZxwi0kL1g3bOaBNNZfYkzt/CL0umgD5wc9Jec2FbB98CjkMRvQ==",
"dev": true
},
"concat-map": {
@@ -303,36 +265,13 @@
"resolved": "https://registry.npmjs.org/core-js/-/core-js-2.5.1.tgz",
"integrity": "sha1-rmh03GaTd4m4B1T/VCjfZoGcpQs="
},
- "cross-spawn": {
- "version": "5.1.0",
- "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-5.1.0.tgz",
- "integrity": "sha1-6L0O/uWPz/b4+UUQoKVUu/ojVEk=",
- "dev": true,
- "requires": {
- "lru-cache": "4.1.1",
- "shebang-command": "1.2.0",
- "which": "1.3.0"
- },
- "dependencies": {
- "lru-cache": {
- "version": "4.1.1",
- "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-4.1.1.tgz",
- "integrity": "sha512-q4spe4KTfsAS1SUHLO0wz8Qiyf1+vMIAgpRYioFYDMNqKfHQbg+AVDH3i4fvpl71/P1L0dBl+fQi+P37UYf0ew==",
- "dev": true,
- "requires": {
- "pseudomap": "1.0.2",
- "yallist": "2.1.2"
- }
- }
- }
- },
"debug": {
- "version": "2.2.0",
- "resolved": "https://registry.npmjs.org/debug/-/debug-2.2.0.tgz",
- "integrity": "sha1-+HBX6ZWxofauaklgZkE3vFbwOdo=",
+ "version": "3.1.0",
+ "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz",
+ "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==",
"dev": true,
"requires": {
- "ms": "0.7.1"
+ "ms": "2.0.0"
}
},
"detect-indent": {
@@ -344,9 +283,9 @@
}
},
"diff": {
- "version": "1.4.0",
- "resolved": "https://registry.npmjs.org/diff/-/diff-1.4.0.tgz",
- "integrity": "sha1-fyjS657nsVqX79ic5j3P2qPMur8=",
+ "version": "3.3.1",
+ "resolved": "https://registry.npmjs.org/diff/-/diff-3.3.1.tgz",
+ "integrity": "sha512-MKPHZDMB0o6yHyDryUOScqZibp914ksXwAMYMTHj6KO8UeKsRYNJD3oNCKjTqZon+V488P7N/HzXF8t7ZR95ww==",
"dev": true
},
"escape-string-regexp": {
@@ -365,14 +304,24 @@
"integrity": "sha1-sKWaDS7/VDdUTr8M6qYBWEHQm1s=",
"dev": true
},
+ "fs.realpath": {
+ "version": "1.0.0",
+ "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz",
+ "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=",
+ "dev": true
+ },
"glob": {
- "version": "3.2.11",
- "resolved": "https://registry.npmjs.org/glob/-/glob-3.2.11.tgz",
- "integrity": "sha1-Spc/Y1uRkPcV0QmH1cAP0oFevj0=",
+ "version": "7.1.2",
+ "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.2.tgz",
+ "integrity": "sha512-MJTUg1kjuLeQCJ+ccE4Vpa6kKVXkPYJ2mOCQyUuKLcLQsdrMCpBPUi8qVE6+YuaJkozeA9NusTAw3hLr8Xe5EQ==",
"dev": true,
"requires": {
+ "fs.realpath": "1.0.0",
+ "inflight": "1.0.6",
"inherits": "2.0.3",
- "minimatch": "0.3.0"
+ "minimatch": "3.0.4",
+ "once": "1.4.0",
+ "path-is-absolute": "1.0.1"
}
},
"globals": {
@@ -381,9 +330,9 @@
"integrity": "sha512-S0nG3CLEQiY/ILxqtztTWH/3iRRdyBLw6KMDxnKMchrtbj2OFmehVh0WUCfW3DUrIgx/qFrJPICrq4Z4sTR9UQ=="
},
"growl": {
- "version": "1.9.2",
- "resolved": "https://registry.npmjs.org/growl/-/growl-1.9.2.tgz",
- "integrity": "sha1-Dqd0NxXbjY3ixe3hd14bRayFwC8=",
+ "version": "1.10.3",
+ "resolved": "https://registry.npmjs.org/growl/-/growl-1.10.3.tgz",
+ "integrity": "sha512-hKlsbA5Vu3xsh1Cg3J7jSmX/WaW6A5oBeqzM88oNbCRQFz+zUaXm6yxS4RVytp1scBoJzSYl4YAEOQIt6O8V1Q==",
"dev": true
},
"has-ansi": {
@@ -394,6 +343,18 @@
"ansi-regex": "2.1.1"
}
},
+ "has-flag": {
+ "version": "2.0.0",
+ "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-2.0.0.tgz",
+ "integrity": "sha1-6CB68cx7MNRGzHC3NLXovhj4jVE=",
+ "dev": true
+ },
+ "he": {
+ "version": "1.1.1",
+ "resolved": "https://registry.npmjs.org/he/-/he-1.1.1.tgz",
+ "integrity": "sha1-k0EP0hsAlzUVH4howvJx80J+I/0=",
+ "dev": true
+ },
"home-or-tmp": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/home-or-tmp/-/home-or-tmp-2.0.0.tgz",
@@ -403,6 +364,16 @@
"os-tmpdir": "1.0.2"
}
},
+ "inflight": {
+ "version": "1.0.6",
+ "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz",
+ "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=",
+ "dev": true,
+ "requires": {
+ "once": "1.4.0",
+ "wrappy": "1.0.2"
+ }
+ },
"inherits": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz",
@@ -425,36 +396,6 @@
"number-is-nan": "1.0.1"
}
},
- "isexe": {
- "version": "2.0.0",
- "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz",
- "integrity": "sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=",
- "dev": true
- },
- "jade": {
- "version": "0.26.3",
- "resolved": "https://registry.npmjs.org/jade/-/jade-0.26.3.tgz",
- "integrity": "sha1-jxDXl32NefL2/4YqgbBRPMslaGw=",
- "dev": true,
- "requires": {
- "commander": "0.6.1",
- "mkdirp": "0.3.0"
- },
- "dependencies": {
- "commander": {
- "version": "0.6.1",
- "resolved": "https://registry.npmjs.org/commander/-/commander-0.6.1.tgz",
- "integrity": "sha1-+mihT2qUXVTbvlDYzbMyDp47GgY=",
- "dev": true
- },
- "mkdirp": {
- "version": "0.3.0",
- "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.3.0.tgz",
- "integrity": "sha1-G79asbqCevI1dRQ0kEJkVfSB/h4=",
- "dev": true
- }
- }
- },
"js-tokens": {
"version": "3.0.2",
"resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-3.0.2.tgz",
@@ -483,20 +424,13 @@
"js-tokens": "3.0.2"
}
},
- "lru-cache": {
- "version": "2.7.3",
- "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-2.7.3.tgz",
- "integrity": "sha1-bUUk6LlV+V1PW1iFHOId1y+06VI=",
- "dev": true
- },
"minimatch": {
- "version": "0.3.0",
- "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-0.3.0.tgz",
- "integrity": "sha1-J12O2qxPG7MyZHIInnlJyDlGmd0=",
+ "version": "3.0.4",
+ "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz",
+ "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==",
"dev": true,
"requires": {
- "lru-cache": "2.7.3",
- "sigmund": "1.0.1"
+ "brace-expansion": "1.1.8"
}
},
"minimist": {
@@ -513,27 +447,35 @@
}
},
"mocha": {
- "version": "2.5.3",
- "resolved": "https://registry.npmjs.org/mocha/-/mocha-2.5.3.tgz",
- "integrity": "sha1-FhvlvetJZ3HrmzV0UFC2IrWu/Fg=",
+ "version": "4.0.1",
+ "resolved": "https://registry.npmjs.org/mocha/-/mocha-4.0.1.tgz",
+ "integrity": "sha512-evDmhkoA+cBNiQQQdSKZa2b9+W2mpLoj50367lhy+Klnx9OV8XlCIhigUnn1gaTFLQCa0kdNhEGDr0hCXOQFDw==",
"dev": true,
"requires": {
- "commander": "2.3.0",
- "debug": "2.2.0",
- "diff": "1.4.0",
- "escape-string-regexp": "1.0.2",
- "glob": "3.2.11",
- "growl": "1.9.2",
- "jade": "0.26.3",
+ "browser-stdout": "1.3.0",
+ "commander": "2.11.0",
+ "debug": "3.1.0",
+ "diff": "3.3.1",
+ "escape-string-regexp": "1.0.5",
+ "glob": "7.1.2",
+ "growl": "1.10.3",
+ "he": "1.1.1",
"mkdirp": "0.5.1",
- "supports-color": "1.2.0",
- "to-iso-string": "0.0.2"
+ "supports-color": "4.4.0"
+ },
+ "dependencies": {
+ "escape-string-regexp": {
+ "version": "1.0.5",
+ "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz",
+ "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=",
+ "dev": true
+ }
}
},
"ms": {
- "version": "0.7.1",
- "resolved": "https://registry.npmjs.org/ms/-/ms-0.7.1.tgz",
- "integrity": "sha1-nNE8A62/8ltl7/3nzoZO6VIBcJg=",
+ "version": "2.0.0",
+ "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
+ "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=",
"dev": true
},
"number-is-nan": {
@@ -541,6 +483,15 @@
"resolved": "https://registry.npmjs.org/number-is-nan/-/number-is-nan-1.0.1.tgz",
"integrity": "sha1-CXtgK1NCKlIsGvuHkDGDNpQaAR0="
},
+ "once": {
+ "version": "1.4.0",
+ "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
+ "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=",
+ "dev": true,
+ "requires": {
+ "wrappy": "1.0.2"
+ }
+ },
"os-homedir": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/os-homedir/-/os-homedir-1.0.2.tgz",
@@ -561,12 +512,6 @@
"resolved": "https://registry.npmjs.org/private/-/private-0.1.8.tgz",
"integrity": "sha512-VvivMrbvd2nKkiG38qjULzlc+4Vx4wm/whI9pQD35YrARNnhxeiRktSOhSukRLFNlzg6Br/cJPet5J/u19r/mg=="
},
- "pseudomap": {
- "version": "1.0.2",
- "resolved": "https://registry.npmjs.org/pseudomap/-/pseudomap-1.0.2.tgz",
- "integrity": "sha1-8FKijacOYYkX7wqKw0wa5aaChrM=",
- "dev": true
- },
"regenerator-runtime": {
"version": "0.11.0",
"resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.11.0.tgz",
@@ -580,27 +525,6 @@
"is-finite": "1.0.2"
}
},
- "shebang-command": {
- "version": "1.2.0",
- "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-1.2.0.tgz",
- "integrity": "sha1-RKrGW2lbAzmJaMOfNj/uXer98eo=",
- "dev": true,
- "requires": {
- "shebang-regex": "1.0.0"
- }
- },
- "shebang-regex": {
- "version": "1.0.0",
- "resolved": "https://registry.npmjs.org/shebang-regex/-/shebang-regex-1.0.0.tgz",
- "integrity": "sha1-2kL0l0DAtC2yypcoVxyxkMmO/qM=",
- "dev": true
- },
- "sigmund": {
- "version": "1.0.1",
- "resolved": "https://registry.npmjs.org/sigmund/-/sigmund-1.0.1.tgz",
- "integrity": "sha1-P/IfGYytIXX587eBhT/ZTQ0ZtZA=",
- "dev": true
- },
"slash": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/slash/-/slash-1.0.0.tgz",
@@ -628,40 +552,28 @@
}
},
"supports-color": {
- "version": "1.2.0",
- "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-1.2.0.tgz",
- "integrity": "sha1-/x7R5hFp0Gs88tWI4YixjYhH4X4=",
- "dev": true
+ "version": "4.4.0",
+ "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-4.4.0.tgz",
+ "integrity": "sha512-rKC3+DyXWgK0ZLKwmRsrkyHVZAjNkfzeehuFWdGGcqGDTZFH73+RH6S/RDAAxl9GusSjZSUWYLmT9N5pzXFOXQ==",
+ "dev": true,
+ "requires": {
+ "has-flag": "2.0.0"
+ }
},
"to-fast-properties": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/to-fast-properties/-/to-fast-properties-1.0.3.tgz",
"integrity": "sha1-uDVx+k2MJbguIxsG46MFXeTKGkc="
},
- "to-iso-string": {
- "version": "0.0.2",
- "resolved": "https://registry.npmjs.org/to-iso-string/-/to-iso-string-0.0.2.tgz",
- "integrity": "sha1-TcGeZk38y+Jb2NtQiwDG2hWCVdE=",
- "dev": true
- },
"trim-right": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/trim-right/-/trim-right-1.0.1.tgz",
"integrity": "sha1-yy4SAwZ+DI3h9hQJS5/kVwTqYAM="
},
- "which": {
- "version": "1.3.0",
- "resolved": "https://registry.npmjs.org/which/-/which-1.3.0.tgz",
- "integrity": "sha512-xcJpopdamTuY5duC/KnTTNBraPK54YwpenP4lzxU8H91GudWpFv38u0CKjclE1Wi2EH2EDz5LRcHcKbCIzqGyg==",
- "dev": true,
- "requires": {
- "isexe": "2.0.0"
- }
- },
- "yallist": {
- "version": "2.1.2",
- "resolved": "https://registry.npmjs.org/yallist/-/yallist-2.1.2.tgz",
- "integrity": "sha1-HBH5IY8HYImkfdUS+TxmmaaoHVI=",
+ "wrappy": {
+ "version": "1.0.2",
+ "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
+ "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=",
"dev": true
}
}
diff --git a/package.json b/package.json
index 3987512..3ba775e 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "rewire",
- "version": "2.5.2",
+ "version": "3.0.0",
"description": "Easy dependency injection for node.js unit testing",
"keywords": [
"dependency",
@@ -32,15 +32,15 @@
"devDependencies": {
"coffee-script": "^1.8.0",
"expect.js": "^0.3.1",
- "mocha": "^2.1.0"
+ "mocha": "^4.0.1"
},
"license": "MIT",
"scripts": {
- "test": "mocha -R spec --check-leaks --globals __core-js_shared__",
+ "test": "mocha -R spec",
"coverage": "istanbul cover ./node_modules/mocha/bin/_mocha"
},
"dependencies": {
- "babel-core": "~6.26.0",
- "babel-plugin-transform-es2015-constants": "~6.1.4"
+ "babel-core": "^6.26.0",
+ "babel-plugin-transform-es2015-block-scoping": "^6.26.0"
}
}
diff --git a/test/getImportGlobalsSrc.test.js b/test/getImportGlobalsSrc.test.js
index ab9e946..0c40e5a 100644
--- a/test/getImportGlobalsSrc.test.js
+++ b/test/getImportGlobalsSrc.test.js
@@ -25,17 +25,13 @@ describe("getImportGlobalsSrc", function () {
delete global.module;
delete global.exports;
delete global.require;
+ delete global['__core-js_shared__'];
delete global['a-b'];
expectedGlobals = Object.keys(global);
vm.runInNewContext(src, context);
- actualGlobals = Object.keys(context).filter(function (key) {
- // node v0.10 does not set a constructor property on the context
- // node v0.11 does set a constructor property
- // so just lets filter it, because it doesn't make sense to mock it anyway
- return key !== "constructor";
- });
+ actualGlobals = Object.keys(context);
actualGlobals.sort();
expectedGlobals.sort();
expect(actualGlobals).to.eql(expectedGlobals);
@@ -56,12 +52,7 @@ describe("getImportGlobalsSrc", function () {
return ignore.indexOf(value) === -1;
});
vm.runInNewContext(src, context);
- actualGlobals = Object.keys(context).filter(function (key) {
- // node v0.10 does not set a constructor property on the context
- // node v0.11 does set a constructor property
- // so just lets filter it, because it doesn't make sense to mock it anyway
- return key !== "constructor";
- });
+ actualGlobals = Object.keys(context);
actualGlobals.sort();
expectedGlobals.sort();
expect(actualGlobals).to.eql(expectedGlobals);
diff --git a/test/rewire.test.js b/test/rewire.test.js
index ecf3cc9..b6e1499 100644
--- a/test/rewire.test.js
+++ b/test/rewire.test.js
@@ -16,9 +16,7 @@ describe("rewire", function () {
fs.renameSync(fakeNodeModules, path.resolve(__dirname, "../testLib/node_modules"));
}
});
- it("should pass all shared test cases", function () {
- require("../testLib/sharedTestCases.js");
- });
+ require("../testLib/sharedTestCases.js")();
it("should also work with CoffeeScript", function () {
var coffeeModule;
diff --git a/testLib/ES2015Module.js b/testLib/constModule.js
index 81eba2b..77396d9 100644
--- a/testLib/ES2015Module.js
+++ b/testLib/constModule.js
@@ -1,14 +1,13 @@
-
const someOtherModule = require("./someOtherModule");
const language = "nl";
-module.exports.getLang = () => {
+exports.getLang = () => {
return language;
-}
+};
exports.getOtherModuleName = () => {
return someOtherModule.name;
-}
+};
exports.filename = __filename;
exports.dirname = __dirname;
diff --git a/testLib/sharedTestCases.js b/testLib/sharedTestCases.js
index ddb8b04..b6caf6a 100644
--- a/testLib/sharedTestCases.js
+++ b/testLib/sharedTestCases.js
@@ -17,7 +17,7 @@ function checkForTypeError(err) {
expect(err.constructor).to.be(TypeError);
}
-describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv + ")"), function () {
+module.exports = function () {
it("should work like require()", function () {
rewire("./moduleA.js").getFilename();
@@ -265,7 +265,7 @@ describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv +
throwError();
} catch (err) {
if (err.stack) {
- expect(err.stack.split("\n")[1]).to.match(/:2:11/);
+ expect(err.stack.split("\n")[1]).to.match(/:7:11/);
}
}
});
@@ -372,51 +372,22 @@ describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv +
revert();
});
- it("Should be possible to mock a const variable using __with__ syntax", function() {
- var ES2015Module = rewire("./ES2015Module", {
- convertConst: true
- });
-
- ES2015Module.__with__({
- language: "en"
- })(function() {
- expect(ES2015Module.getLang()).to.equal("en");
- expect(ES2015Module.getOtherModuleName()).to.equal("somOtherModule");
- });
- });
+ it("should be possible to mock a set a const variable using __set__ syntax", function() {
+ var constModule = rewire("./constModule");
- it("Should be possible to mock a const required variable using __with__ syntax", function() {
- var ES2015Module = rewire("./ES2015Module", {
- convertConst: true
+ constModule.__set__("language", "de");
+ constModule.__set__("someOtherModule", {
+ name: "differentModule"
});
-
- ES2015Module.__with__({
- someOtherModule: {
- name: "mocked"
- }
- })(function() {
- expect(ES2015Module.getLang()).to.equal("nl");
- expect(ES2015Module.getOtherModuleName()).to.equal("mocked");
- });
- });
-
- it("Should be possible to mock a set a const variable using __set__ syntax", function() {
- var ES2015Module = rewire("./ES2015Module", {
- convertConst: true
- });
-
- ES2015Module.__set__("language", "de");
-
- expect(ES2015Module.getLang()).to.equal("de");
-
- ES2015Module.__set__("language", "nl");
-
- expect(ES2015Module.getLang()).to.equal("nl");
+ expect(constModule.getLang()).to.equal("de");
+ expect(constModule.getOtherModuleName()).to.equal("differentModule");
})
- it("Should have correct __filename and __dirname when mocked using convertConst", function() {
- expect(rewire("./ES2015Module", { convertConst: true }).filename).to.equal(require("./ES2015Module").filename);
- expect(rewire("./ES2015Module", { convertConst: true }).dirname).to.equal(require("./ES2015Module").dirname);
+ it("should have correct __filename and __dirname when mocked using convertConst", function() {
+ var constModule = rewire("./constModule");
+
+ expect(constModule.filename).to.equal(require("./constModule").filename);
+ expect(constModule.dirname).to.equal(require("./constModule").dirname);
});
-});
+};
diff --git a/testLib/throwError.js b/testLib/throwError.js
index 9bdf68b..ffb6a71 100644
--- a/testLib/throwError.js
+++ b/testLib/throwError.js
@@ -1,3 +1,8 @@
+// Using const here because we know that Babel will transform that part
+const test = 1;
+
module.exports = function () {
+ let test = 1;
+
throw new Error();
};