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:
authorJohannes Ewald <johannes.ewald@peerigon.com>2018-04-09 21:49:49 +0300
committerJohannes Ewald <johannes.ewald@peerigon.com>2018-04-09 22:00:59 +0300
commit9b77ed9a293c538ec3eb5160bcb933e012ce517f (patch)
tree9b3d93d4cedeeef376d54a412636b7c862229e6a /testLib
parentcbb2802ea5e30b47003298f2756c62ce20b41bb0 (diff)
Replace babel with a regex-based transformation
This regex replacement is not 100% safe because transforming JavaScript requires an actual parser. However, parsing (e.g. via babel) comes with its own problems because now the parser needs to be aware of syntax extensions which might not be supported by the parser, but the underlying JavaScript engine. In fact, rewire used to have babel in place but required an extra transform for the object spread operator (check out commit d9a81c0cdacf6995b24d205b4a2068adbd8b34ff or see https://github.com/jhnns/rewire/pull/128). It was also notable slower (see https://github.com/jhnns/rewire/issues/132). There is another issue: replacing const with let is not safe because of their different behavior. That's why we also have ESLint in place which tries to identify this error case. There is one edge case though: when a new syntax is used *and* a const re-assignment happens, rewire would compile happily in this situation but the actual code wouldn't work. However, since most projects have a seperate linting step which catches these const re-assignment errors anyway, it's probably still a reasonable trade-off. Fixes https://github.com/jhnns/rewire/issues/132
Diffstat (limited to 'testLib')
-rw-r--r--testLib/constModule.js49
-rw-r--r--testLib/sharedTestCases.js51
-rw-r--r--testLib/throwError.js2
-rw-r--r--testLib/wrongConstModule.js4
4 files changed, 63 insertions, 43 deletions
diff --git a/testLib/constModule.js b/testLib/constModule.js
index 77396d9..efff495 100644
--- a/testLib/constModule.js
+++ b/testLib/constModule.js
@@ -1,13 +1,42 @@
-const someOtherModule = require("./someOtherModule");
-const language = "nl";
+const j = "j"; // At the beginning of the file
+// This module contains some weird combinations where valid const declarations could appear.
+// Syntax oddities are totally on purpose here.
+const a = require("./someOtherModule");const b = "b"; const e = "e"
+const c = "c";
+{}const d = "d";
+ const f = "f"; // there's an irregular whitespace before and after const
+const
+g = "g";
+const/*wtf this is valid*/h = "h";
+const /*and this is also*/i = "i";
-exports.getLang = () => {
- return language;
+exports.a = function () {
+ return a;
};
-
-exports.getOtherModuleName = () => {
- return someOtherModule.name;
+exports.b = function () {
+ return b;
+};
+exports.c = function () {
+ return c;
+};
+exports.d = function () {
+ return d;
+};
+exports.e = function () {
+ return e;
+};
+exports.f = function () {
+ return f;
+};
+exports.g = function () {
+ return g;
+};
+exports.h = function () {
+ return h;
+};
+exports.i = function () {
+ return i;
+};
+exports.j = function () {
+ return j;
};
-
-exports.filename = __filename;
-exports.dirname = __dirname;
diff --git a/testLib/sharedTestCases.js b/testLib/sharedTestCases.js
index 991453b..611a1d6 100644
--- a/testLib/sharedTestCases.js
+++ b/testLib/sharedTestCases.js
@@ -220,34 +220,24 @@ module.exports = function () {
expect(rewired.__get__("someVar")).to.be("hello");
});
- it("should not be a problem to have a module that exports a boolean", function() {
- expect(function() {
- var rewired = rewire("./boolean.js");
- }).to.not.throwException();
+ it("should not be a problem to have a module that exports a boolean", function( ) {
+ rewire("./boolean.js"); // should not throw
});
- it("should not be a problem to have a module that exports null", function() {
- expect(function() {
- var rewired = rewire("./null.js");
- }).to.not.throwException();
+ it("should not be a problem to have a module that exports null", function () {
+ rewire("./null.js"); // should not throw
});
- it("should not be a problem to have a module that exports a sealed object", function() {
- expect(function() {
- var rewired = rewire("./sealedObject.js");
- }).to.not.throwException();
+ it("should not be a problem to have a module that exports a sealed object", function () {
+ rewire("./sealedObject.js"); // should not throw
});
- it("should not be a problem to have a module that uses object spread operator", function() {
- expect(function() {
- var rewired = rewire("./objectSpreadOperator.js");
- }).to.not.throwException();
+ it("should not be a problem to have a module that uses object spread operator", function () {
+ rewire("./objectSpreadOperator.js"); // should not throw
});
- it("should not be a problem to have a module that uses object rest operator", function() {
- expect(function() {
- var rewired = rewire("./objectRestOperator.js");
- }).to.not.throwException();
+ it("should not be a problem to have a module that uses object rest operator", function () {
+ rewire("./objectRestOperator.js"); // should not throw
});
it("should not influence the original require if nothing has been required within the rewired module", function () {
@@ -384,22 +374,19 @@ module.exports = function () {
revert();
});
- it("should be possible to mock a set a const variable using __set__ syntax", function() {
+ it("should be possible to set a const variable", function () {
var constModule = rewire("./constModule");
- constModule.__set__("language", "de");
- constModule.__set__("someOtherModule", {
- name: "differentModule"
+ "abcdefghij".split("").forEach(letter => {
+ constModule.__set__(letter, "this has been changed"); // should not throw
+ expect(constModule[letter]()).to.equal("this has been changed");
});
- expect(constModule.getLang()).to.equal("de");
- expect(constModule.getOtherModuleName()).to.equal("differentModule");
- })
-
- 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);
+ it("should fail with a helpful TypeError when const is re-assigned", function () {
+ expect(function () {
+ rewire("./wrongConstModule");
+ }).to.throwException(/^Assignment to constant variable at .+?wrongConstModule\.js:4:1$/);
});
};
diff --git a/testLib/throwError.js b/testLib/throwError.js
index ffb6a71..e85ea36 100644
--- a/testLib/throwError.js
+++ b/testLib/throwError.js
@@ -1,4 +1,4 @@
-// Using const here because we know that Babel will transform that part
+// Using deliberately const here because we know that we're transform const to let
const test = 1;
module.exports = function () {
diff --git a/testLib/wrongConstModule.js b/testLib/wrongConstModule.js
new file mode 100644
index 0000000..901892c
--- /dev/null
+++ b/testLib/wrongConstModule.js
@@ -0,0 +1,4 @@
+// Assigning to a const should fail
+const a = "a";
+
+a = "b";