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/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";