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:
Diffstat (limited to 'testLib')
-rw-r--r--testLib/constModule.js49
-rw-r--r--testLib/objectRestOperator.js2
-rw-r--r--testLib/objectSpreadOperator.js1
-rw-r--r--testLib/sharedTestCases.js71
-rw-r--r--testLib/throwError.js9
-rw-r--r--testLib/wrongConstModule.js4
6 files changed, 91 insertions, 45 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/objectRestOperator.js b/testLib/objectRestOperator.js
new file mode 100644
index 0000000..4a2753d
--- /dev/null
+++ b/testLib/objectRestOperator.js
@@ -0,0 +1,2 @@
+let { ...a } = {};
+module.exports = a;
diff --git a/testLib/objectSpreadOperator.js b/testLib/objectSpreadOperator.js
new file mode 100644
index 0000000..83b7414
--- /dev/null
+++ b/testLib/objectSpreadOperator.js
@@ -0,0 +1 @@
+module.exports = { ...{} };
diff --git a/testLib/sharedTestCases.js b/testLib/sharedTestCases.js
index b6caf6a..6cd9b2e 100644
--- a/testLib/sharedTestCases.js
+++ b/testLib/sharedTestCases.js
@@ -13,6 +13,23 @@ var expect = require("expect.js"),
__get__Src = require("../lib/__get__.js").toString(),
__with__Src = require("../lib/__with__.js").toString();
+var supportsObjectSpread = (function () {
+ try {
+ eval("({...{}})");
+ return true;
+ } catch (err) {
+ return false;
+ }
+})();
+var supportsObjectRest = (function () {
+ try {
+ eval("const {...a} = {}");
+ return true;
+ } catch (err) {
+ return false;
+ }
+})();
+
function checkForTypeError(err) {
expect(err.constructor).to.be(TypeError);
}
@@ -220,22 +237,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 () {
+ rewire("./null.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 a sealed object", function () {
+ rewire("./sealedObject.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();
+ (supportsObjectSpread ? it : it.skip)("should not be a problem to have a module that uses object spread operator", function () {
+ rewire("./objectSpreadOperator.js"); // should not throw
+ });
+
+ (supportsObjectRest ? it : it.skip)("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 () {
@@ -258,15 +277,10 @@ module.exports = function () {
it("should not modify line numbers in stack traces", function () {
var throwError = rewire("./throwError.js");
- if (process.env.running_under_istanbul === "1") {
- return;
- }
try {
throwError();
} catch (err) {
- if (err.stack) {
- expect(err.stack.split("\n")[1]).to.match(/:7:11/);
- }
+ expect(err.stack.split("\n")[1]).to.match(/:6:26/);
}
});
@@ -372,22 +386,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..0993618 100644
--- a/testLib/throwError.js
+++ b/testLib/throwError.js
@@ -1,8 +1,7 @@
-// Using const here because we know that Babel will transform that part
-const test = 1;
+// Using deliberately const here because we know that we're transform const to let
+const a = "a";
module.exports = function () {
- let test = 1;
-
- throw new Error();
+ // Ensure that column numbers are correct
+ const b = "b"; throw new Error();
};
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";