This repository has been archived by the owner on Nov 27, 2018. It is now read-only.
/
module_utils.spec.js
69 lines (51 loc) · 2.12 KB
/
module_utils.spec.js
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
var module_utils = require("../../lib/utils/module_utils.js");
describe("require and setup", function() {
var sampleConfig = {};
beforeEach(function() {
spyOn(process, "cwd").andReturn("/current/path");
});
it("locate local modules relative to current working directory", function() {
spyOn(module_utils, "wrappedRequire").andCallFake(function(path) {
return {}
});
module_utils.requireAndSetup("./modules/local_module", sampleConfig);
expect(module_utils.wrappedRequire).toHaveBeenCalledWith("/current/path/modules/local_module");
});
it("try to locate required module from the current working directory's node modules", function() {
spyOn(module_utils, "wrappedRequire").andCallFake(function(path) {
return {}
});
module_utils.requireAndSetup("custom_module", sampleConfig);
expect(module_utils.wrappedRequire).toHaveBeenCalledWith("/current/path/node_modules/custom_module");
});
it("traverse till to top of the tree, looking for the module in each node modules directory", function() {
spyOn(module_utils, "wrappedRequire").andCallFake(function(path) {
if (path === "/current/node_modules/custom_module") {
return {}
} else {
throw("module not found");
}
});
module_utils.requireAndSetup("custom_module", sampleConfig);
expect(module_utils.wrappedRequire).toHaveBeenCalledWith("/current/node_modules/custom_module");
});
it("delegate module loading to node's paths, if module cannot be found in current working tree", function() {
spyOn(module_utils, "wrappedRequire").andCallFake(function(path) {
if (path === "custom_module") {
return {}
} else {
throw("module not found");
}
});
module_utils.requireAndSetup("custom_module", sampleConfig);
expect(module_utils.wrappedRequire).toHaveBeenCalledWith("custom_module");
});
it("call the setup function of the loaded module", function() {
var spySetup = jasmine.createSpy();
spyOn(module_utils, "wrappedRequire").andCallFake(function(path) {
return { "setup": spySetup };
});
module_utils.requireAndSetup("./local_module", sampleConfig);
expect(spySetup).toHaveBeenCalledWith(sampleConfig);
});
});