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
|
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
}, 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 context,
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);
returnedObj = browserifyRewire("/a.js", "/b.js");
}
function moduleB() {
"use strict";
browserifyRewire.register("/b.js", setter, getter);
return exportsObj;
}
function fakeResolve() {
return "/b.js";
}
function fakeRequire(requirePath) {
if (requirePath === "path") {
return pathUtil;
} else {
return moduleB();
}
}
fakeRequire.resolve = fakeResolve;
function setter() {}
function getter() {}
context = {
require: fakeRequire,
module: {},
console: console,
window: {
browserifyRequire: {
modules: {
"/b.js": {
_cached : {}
}
}
}
}
};
fs.readFile(pathUtil.resolve(__dirname, "../lib/browserify/browserifyRewire.js"), "utf8", function onBrowserifyRewireRead(err, src) {
vm.runInNewContext(src, context);
browserifyRewire = context.module.exports;
moduleA();
expect(returnedObj).not.to.be(exportsObj);
expect(returnedObj.__set__).to.be(setter);
expect(returnedObj.__get__).to.be(getter);
expect(context.window.browserifyRequire.modules["/b.js"]._cached).to.be(returnedObj);
done();
});
});
it("should run all sharedTestCases without exception", function (done) {
var b = browserify({debug: true}),
middleware = require("rewire").browserify,
browserOutput = __dirname + "/browser/browseroutput.js",
browserBundle,
vmBundle;
b.use(middleware);
b.require(__dirname + "/testModules/sharedTestCases.js");
vmBundle = b.bundle();
browserBundle = vmBundle;
// Setup for mocha
browserBundle += 'window.onload = function () {' +
'mocha.setup("bdd");' +
'window.browserifyRequire("/test/testModules/sharedTestCases.js");' +
'mocha.run();' +
'};';
vmBundle += 'window.browserifyRequire("/test/testModules/sharedTestCases.js");';
// Output for browser-testing
fs.mkdir(__dirname + "/browser", function onMkDir() {
fs.writeFile(browserOutput, browserBundle, "utf8", done);
// This should throw no exception.
runInFakeBrowserContext(vmBundle, browserOutput);
});
});
});
|