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

rewire.test.js « test - github.com/twbs/rewire.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9216af610f5f072b814ef2f2e6e45e78645a4708 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"use strict"; // run code in ES5 strict mode

var path = require("path"),
    expect = require("expect.js"),
    rewire = require("../lib/index.js");

var testModules = {
        A: path.resolve(__dirname, "./testModules/moduleA.js"),
        B: path.resolve(__dirname, "./testModules/moduleB.js"),
        someOtherModule: path.resolve(__dirname, "./testModules/someOtherModule.js"),
        emptyModule: path.resolve(__dirname, "./testModules/emptyModule.js")
    };

function cleanRequireCache() {
    var moduleName,
        modulePath;

    for (moduleName in testModules) {
        if (testModules.hasOwnProperty(moduleName)) {
            modulePath = testModules[moduleName];
            delete require.cache[modulePath];
        }
    }
}

describe("rewire", function () {
    beforeEach(cleanRequireCache);  // ensuring a clean test environment
    it("should work like require()", function () {
        expect(rewire("./testModules/moduleA.js")).to.be(require("./testModules/moduleA.js"));
        cleanRequireCache();
        expect(rewire("../test/testModules/moduleA.js")).to.be(require("../test/testModules/moduleA.js"));
        cleanRequireCache();
        expect(rewire(testModules.A)).to.be(require(testModules.A));
    });
    it("should modify the module so it provides a __set__ - function", function () {
        expect(rewire(testModules.A).__set__).to.be.a(Function);
        expect(rewire(testModules.B).__set__).to.be.a(Function);
    });
    it("should modify the module so it provides a __get__ - function", function () {
        expect(rewire(testModules.A).__get__).to.be.a(Function);
        expect(rewire(testModules.B).__get__).to.be.a(Function);
    });
    it("should not influence other modules", function () {
        var rewiredModuleA = rewire(testModules.A);

        expect(require(testModules.someOtherModule).__set__).to.be(undefined);
        expect(require(testModules.someOtherModule).__get__).to.be(undefined);
        expect(require("fs").__set__).to.be(undefined);
        expect(require("fs").__get__).to.be(undefined);
    });
    it("should not influence global objects by default", function () {
        expect(function () {
            rewire(testModules.A).checkSomeGlobals();
            rewire(testModules.B).checkSomeGlobals();
        }).to.not.throwException();
    });
    it("should provide the ability to set private vars", function () {
        var rewiredModuleA = rewire(testModules.A),
            newObj = {};

        expect(rewiredModuleA.getMyNumber()).to.be(0);
        rewiredModuleA.__set__("myNumber", 2);
        expect(rewiredModuleA.getMyNumber()).to.be(2);
        rewiredModuleA.__set__("myObj", newObj);
        expect(rewiredModuleA.getMyObj()).to.be(newObj);
    });
    it("should provide the ability to get private vars", function () {
        var rewiredModuleA = rewire(testModules.A);

        expect(rewiredModuleA.__get__("myNumber")).to.be(rewiredModuleA.getMyNumber());
        expect(rewiredModuleA.__get__("myObj")).to.be(rewiredModuleA.getMyObj());
    });
    it("should provide the ability to inject mocks", function (done) {
        var rewiredModuleA = rewire(testModules.A),
            mockedFs = {
                readFileSync: function (file) {
                    expect(file).to.be("bla.txt");
                    done();
                }
            };

        rewiredModuleA.__set__("fs", mockedFs);
        rewiredModuleA.readFileSync();
    });
    it("should not influence other modules when injecting mocks", function () {
        var rewiredModuleA = rewire(testModules.A),
            someOtherModule,
            mockedFs = {};

        rewiredModuleA.__set__("fs", mockedFs);
        someOtherModule = require(testModules.someOtherModule);
        expect(someOtherModule.fs).not.to.be(mockedFs);
    });
    it("should provide the ability to mock global objects just within the module", function () {
        var rewiredModuleA = rewire(testModules.A),
            rewiredModuleB = rewire(testModules.B),
            consoleMock = {},
            processMock = {},
            newFilename = "myFile.js";

        rewiredModuleA.__set__({
            console: consoleMock,
            process: processMock
        });
        rewiredModuleA.__set__("__filename", newFilename);
        rewiredModuleB.__set__({
            console: consoleMock,
            process: processMock
        });
        rewiredModuleB.__set__("__filename", newFilename);
        expect(rewiredModuleA.getConsole()).to.be(consoleMock);
        expect(rewiredModuleB.getConsole()).to.be(consoleMock);
        expect(console).not.to.be(consoleMock);
        expect(rewiredModuleA.getProcess()).to.be(processMock);
        expect(rewiredModuleB.getProcess()).to.be(processMock);
        expect(process).not.to.be(processMock);
        expect(rewiredModuleA.getFilename()).to.be(newFilename);
        expect(rewiredModuleB.getFilename()).to.be(newFilename);
    });
    it("should cache the rewired module", function () {
        var rewired;

        rewired = rewire(testModules.someOtherModule);
        expect(require(testModules.A).someOtherModule).to.be(rewired);
        cleanRequireCache();
        rewired = rewire(testModules.someOtherModule, true);
        expect(require(testModules.A).someOtherModule).to.be(rewired);
    });
    it("should not cache the rewired module on demand", function () {
        var rewired;

        rewired = rewire(testModules.someOtherModule, false);
        expect(require(testModules.A).someOtherModule).not.to.be(rewired);
    });
    it("should not influence the original node require if nothing has been required within the rewired module", function () {
        rewire(testModules.emptyModule); // nothing happens here because emptyModule doesn't require anything
        expect(require(testModules.A).__set__).to.be(undefined); // if restoring the original node require didn't worked, the module would have a setter
    });
    it("subsequent calls of rewire should always return a new instance", function () {
        expect(rewire(testModules.A)).not.to.be(rewire(testModules.A));
    });
    describe("#reset", function () {
        it("should remove all rewired modules from cache", function () {
            var rewiredModuleA = rewire(testModules.A),
                rewiredModuleB = rewire(testModules.B);

            expect(require(testModules.A)).to.be(rewiredModuleA);
            expect(require(testModules.B)).to.be(rewiredModuleB);
            rewire.reset();
            expect(require(testModules.A)).not.to.be(rewiredModuleA);
            expect(require(testModules.B)).not.to.be(rewiredModuleB);
        });
    });
});