-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock.js
128 lines (114 loc) · 3.58 KB
/
mock.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
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
const state = require('./state').instance;
const assert = require('assert');
function createUID(strLength) {
if (typeof strLength !== 'number' || strLength <= 0) return false;
if (strLength) {
const possibleCharacters = 'abcdefghijklmnopqrstuvwxyz0123456789';
let str = '';
for (let i = 1; i <= strLength; i++) {
str += possibleCharacters.charAt(
Math.floor(Math.random() * possibleCharacters.length),
);
}
return str;
} else {
return false;
}
}
const ST_MOCK = Symbol.for('ST.Test.Mock');
let globalSymbols = Object.getOwnPropertySymbols(global);
let hasSTMock = globalSymbols.indexOf(ST_MOCK) > -1;
if (!hasSTMock) {
global[ST_MOCK] = {
passthrough: `___${createUID(62)}.ST.Test.Mock.Passthrough`,
mocks: {},
new: module => {
if (typeof module === 'object' && !module.__stTestMockUID) {
module.__stTestMockUID = createUID(20);
global[ST_MOCK].mocks[module.__stTestMockUID] = {
calls: {},
originals: {},
};
}
},
unload: module => {
if (typeof module === 'object' && module.__stTestMockUID) {
Object.keys(
global[ST_MOCK].mocks[module.__stTestMockUID].originals,
).forEach(funName => {
module[funName] =
global[ST_MOCK].mocks[module.__stTestMockUID].originals[funName];
});
delete global[ST_MOCK].mocks[module.__stTestMockUID];
delete module.__stTestMockUID;
}
},
expect: (module, funName, fun) => {
if (typeof module === 'object' && module.__stTestMockUID) {
global[ST_MOCK].mocks[module.__stTestMockUID].originals[funName] =
module[funName];
module[funName] = (...args) => {
global[ST_MOCK].mocks[module.__stTestMockUID].calls[funName] = [
...(global[ST_MOCK].mocks[module.__stTestMockUID].calls[funName] ||
[]),
args,
];
if (typeof fun === 'function') {
return fun(...args);
}
if (fun === global[ST_MOCK].passthrough) {
return (global[ST_MOCK].mocks[module.__stTestMockUID].originals[funName])(...args);
}
if (fun) {
return fun;
}
};
} else {
throw new Error('module not mocked');
}
},
assertCall: (module, funName, countOrParams) => {
state.addAssert();
if (typeof module === 'object' && module.__stTestMockUID) {
if (typeof countOrParams === 'number') {
assert.equal(
global[ST_MOCK].mocks[module.__stTestMockUID].calls[funName].length,
countOrParams,
);
} else if (typeof countOrParams === 'object') {
let found = false;
global[ST_MOCK].mocks[module.__stTestMockUID].calls[funName].forEach(
v => {
try {
assert.deepStrictEqual(v, countOrParams);
found = true;
} catch (_) {}
},
);
if (found) {
assert.ok(true);
} else {
throw new assert.AssertionError({
actual: [],
expected: countOrParams,
operator: 'assertCall',
stackStartFn: global[ST_MOCK].assertCall,
});
}
} else {
throw new Error('invalid parameter');
}
} else {
throw new Error('module not mocked');
}
},
};
}
const singleton = {};
Object.defineProperty(singleton, 'instance', {
get: () => {
return global[ST_MOCK];
},
});
Object.freeze(singleton);
module.exports = singleton;