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

browserify.browserifyRewire.test.js « test - github.com/twbs/rewire.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e52a50ca8375bbd41e34b9a5a8734e835bc7e304 (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
var vm = require("vm"),
    fs = require("fs"),
    pathUtil = require("path"),
    expect = require("expect.js"),
    browserify = require("browserify");

/**
 * Executes the source in a context that pretends to be a browser
 * @param {!String} src
 */
function runInFakeBrowserContext(src, filename) {
    vm.runInNewContext(src, {
        window: {
            console: console,
            describe: describe,
            it: it,
            before: before,
            after: after,
            beforeEach: beforeEach,
            afterEach: afterEach,
            setTimeout: setTimeout
        },
        console: console,
        setTimeout: setTimeout,
        clearTimeout: clearTimeout,
        setInterval: setInterval,
        clearInterval: clearInterval,
        parseFloat: parseFloat,
        parseInt: parseInt,
        encodeURIComponent: function () {},
        decodeURIComponent: function () {}
    }, filename);
}

describe("browserifyRewire", function () {
    before(require("./testHelpers/createFakePackageJSON.js"));
    after(require("./testHelpers/removeFakePackageJSON.js"));
    it("should attach __set__ and __get__ to the exports-obj", function (done) {
        var pathToBrowserfiyRewire = pathUtil.resolve(__dirname, "../lib/browserify/browserifyRewire.js"),
            context,
            exportsObj = {},
            moduleObj = {exports: exportsObj},
            returnedObj,
            browserifyRewire;

        // Register with fake objects
        // Using null for objects that are not involved in this particular test
        function moduleA() {
            "use strict";

            browserifyRewire.register("/a.js", null, null, null);
            returnedObj = browserifyRewire("/a.js", "/b.js");
        }

        function moduleB() {
            "use strict";

            browserifyRewire.register("/b.js", moduleObj, setter, getter);

            return exportsObj;
        }

        function fakeResolve() {
            return "/b.js";
        }

        function fakeRequire(path, parentModulePath) {
            var module;

            if (path === "../browser/shims.js") {
                return;
            } else if (path === "../getImportGlobalsSrc.js") {
                return require("../lib/getImportGlobalsSrc.js");
            }

            module = moduleB();

            expect(path).to.be("/b.js");
            expect(parentModulePath).to.be("/a.js");
            fakeRequire.cache["/b.js"] = module;

            return module;
        }
        fakeRequire.resolve = fakeResolve;
        fakeRequire.cache =  {};

        function setter() {}
        function getter() {}

        context = {
            module: {},
            console: console,
            window: {
                browserifyRequire: fakeRequire
            },
            require: fakeRequire
        };

        fs.readFile(pathToBrowserfiyRewire, "utf8", function onBrowserifyRewireRead(err, src) {
            vm.runInNewContext(src, context, pathToBrowserfiyRewire);
            browserifyRewire = context.module.exports;

            moduleA();

            expect(returnedObj.__set__).to.be(setter);
            expect(returnedObj.__get__).to.be(getter);
            // Because browserify does not create the module-object newly when executing the module
            // again we have to copy the module object deeply and store that copy in the
            // cache. Therefore we're checking here if the returned exports-object and the
            // cached module-object are an independent copy.
            expect(returnedObj).not.to.be(exportsObj);
            expect(context.window.browserifyRequire.cache["/b.js"]).not.to.be(moduleObj);

            done();
        });
    });
    it("should run all sharedTestCases without exception", function () {
        var b = browserify({debug: true}),
            middleware = require("rewire").browserify,
            browserOutput = __dirname + "/browserify/bundle.js",
            browserBundle,
            vmBundle;

        b.use(middleware);
        b.require(__dirname + "/testModules/sharedTestCases.js");
        vmBundle = b.bundle();
        browserBundle = vmBundle;

        // Setup for mocha
        browserBundle += 'window.onload = function () {' +
            'console.log("These tests will only work in all browsers with the console being opened");' +
            'mocha.setup("bdd");' +
            'window.browserifyRequire("/test/testModules/sharedTestCases.js");' +
            'mocha.run();' +
        '};';

        vmBundle += 'window.browserifyRequire("/test/testModules/sharedTestCases.js");';

        // Output for browser-testing
        fs.writeFileSync(browserOutput, browserBundle, "utf8");

        // This should throw no exception.
        runInFakeBrowserContext(vmBundle, browserOutput);
    });
});