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

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

describe("getImportGlobalsSrc", function () {
    it("should declare all globals with a var", function () {
        var context = {
                global: global
            },
            expectedGlobals,
            src,
            actualGlobals;

        // Temporarily set module-internal variables on the global scope to check if getImportGlobalsSrc()
        // ignores them properly
        global.module = module;
        global.exports = exports;
        global.require = require;

        // Also make sure it ignores invalid variable names
        global['a-b'] = true;

        src = getImportGlobalsSrc();

        delete global.module;
        delete global.exports;
        delete global.require;
        delete global['__core-js_shared__'];
        delete global['a-b'];

        expectedGlobals = Object.keys(global);

        vm.runInNewContext(src, context);
        actualGlobals = Object.keys(context);
        actualGlobals.sort();
        expectedGlobals.sort();
        expect(actualGlobals).to.eql(expectedGlobals);
        expect(actualGlobals.length).to.be.above(1);
    });
    it("should ignore the given variables", function () {
        var context = {
                global: global
            },
            ignore = ["console", "setTimeout"],
            src,
            actualGlobals,
            expectedGlobals = Object.keys(global);

        // getImportGlobalsSrc modifies the ignore array, so let's create a copy
        src = getImportGlobalsSrc(ignore.slice(0));
        expectedGlobals = expectedGlobals.filter(function filterIgnoredVars(value) {
            return ignore.indexOf(value) === -1;
        });
        vm.runInNewContext(src, context);
        actualGlobals = Object.keys(context);
        actualGlobals.sort();
        expectedGlobals.sort();
        expect(actualGlobals).to.eql(expectedGlobals);
        expect(actualGlobals.length).to.be.above(1);
    });
});