1 | const realBinding = process.binding('fs');
|
2 | let storedBinding;
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | exports = module.exports = function bypass(fn) {
|
12 | if (typeof fn !== 'function') {
|
13 | throw new Error(`Must provide a function to perform for mock.bypass()`);
|
14 | }
|
15 |
|
16 | disable();
|
17 |
|
18 | let result;
|
19 | try {
|
20 | result = fn();
|
21 | if (result && typeof result.then === 'function') {
|
22 | return result.then(
|
23 | r => {
|
24 | enable();
|
25 | return r;
|
26 | },
|
27 | err => {
|
28 | enable();
|
29 | throw err;
|
30 | }
|
31 | );
|
32 | } else {
|
33 | enable();
|
34 | return result;
|
35 | }
|
36 | } catch (err) {
|
37 | enable();
|
38 | throw err;
|
39 | }
|
40 | };
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | function disable() {
|
46 | if (realBinding._mockedBinding) {
|
47 | storedBinding = realBinding._mockedBinding;
|
48 | delete realBinding._mockedBinding;
|
49 | }
|
50 | }
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | function enable() {
|
56 | if (storedBinding) {
|
57 | realBinding._mockedBinding = storedBinding;
|
58 | storedBinding = undefined;
|
59 | }
|
60 | }
|