Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81152 views
1
/**
2
* Copyright 2013-2014, Facebook, Inc.
3
* All rights reserved.
4
*
5
* This source code is licensed under the BSD-style license found in the
6
* LICENSE file in the root directory of this source tree. An additional grant
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* @providesModule mock-modules
10
*/
11
12
var mocks = require("mocks");
13
var exportsRegistry = {};
14
var hasOwn = exportsRegistry.hasOwnProperty;
15
var explicitMockMap = {};
16
17
function getMock(exports) {
18
try {
19
return mocks.generateFromMetadata(mocks.getMetadata(exports));
20
} catch (err) {
21
console.warn(err);
22
return exports;
23
}
24
}
25
26
// This function should be called at the bottom of any module that might
27
// need to be mocked, after the final value of module.exports is known.
28
exports.register = function(id, module) {
29
exportsRegistry[id] = {
30
module: module,
31
actual: module.exports,
32
mocked: null // Filled in lazily later.
33
};
34
35
// If doMock or doNotMock was called earlier, before the module was
36
// registered, then the choice should have been recorded in
37
// explicitMockMap. Now that the module is registered, we can finally
38
// fulfill the request.
39
if (hasOwn.call(explicitMockMap, id)) {
40
if (explicitMockMap[id]) {
41
doMock(id);
42
} else {
43
doNotMock(id);
44
}
45
}
46
47
return exports;
48
};
49
50
function resetEntry(id) {
51
if (hasOwn.call(exportsRegistry, id)) {
52
delete exportsRegistry[id].module.exports;
53
delete exportsRegistry[id];
54
}
55
}
56
57
exports.dumpCache = function() {
58
require("mocks").clear();
59
60
// Deleting module.exports will cause the module to be lazily
61
// reevaluated the next time it is required.
62
for (var id in exportsRegistry) {
63
resetEntry(id);
64
}
65
66
return exports;
67
};
68
69
exports.getMockMap = function() {
70
return explicitMockMap;
71
};
72
73
exports.clearMockMap = function() {
74
explicitMockMap = {};
75
};
76
77
exports.setMockMap = function(mockMap) {
78
exports.dumpCache();
79
exports.clearMockMap();
80
for (var id in mockMap) {
81
if (mockMap[id]) {
82
doMock(id);
83
} else {
84
doNotMock(id);
85
}
86
}
87
88
return exports;
89
};
90
91
// Call this function to ensure that require(id) returns the actual
92
// exports object created by the module.
93
function doNotMock(id) {
94
explicitMockMap[id] = false;
95
96
var entry = exportsRegistry[id];
97
if (entry && entry.module && entry.actual) {
98
entry.module.exports = entry.actual;
99
}
100
101
return exports;
102
}
103
104
// Call this function to ensure that require(id) returns a mock exports
105
// object based on the actual exports object created by the module.
106
function doMock(id) {
107
explicitMockMap[id] = true;
108
109
var entry = exportsRegistry[id];
110
if (entry && entry.module && entry.actual) {
111
// Because mocking can be expensive, create the mock exports object on
112
// demand, the first time doMock is called.
113
entry.mocked || (entry.mocked = getMock(entry.actual));
114
entry.module.exports = entry.mocked;
115
}
116
117
return exports;
118
}
119
120
var global = Function("return this")();
121
require('test/mock-timers').installMockTimers(global);
122
123
// Exported names are different for backwards compatibility.
124
exports.dontMock = doNotMock;
125
exports.mock = doMock;
126
127