This repository has been archived by the owner on Nov 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathjestify.js
114 lines (105 loc) · 3.53 KB
/
jestify.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
/*global jest*/
require('./jest-extensions');
const jestify = (fetchMockInstance) => {
const jestifiedInstance = new Proxy(fetchMockInstance, {
get: (originalFetchMock, name) => {
if (name === 'sandbox') {
return new Proxy(originalFetchMock[name], {
apply: (func, thisArg, args) => {
const sandboxedFetchMock = func.apply(originalFetchMock, args);
return jestify(sandboxedFetchMock);
},
});
}
return originalFetchMock[name];
},
});
// spy on the fetch handler so we can use all the
// jest function assertions on it
const spy = jest.fn();
const originalFetchHandler = jestifiedInstance.fetchHandler.bind(
jestifiedInstance
);
jestifiedInstance.fetchHandler = function (...args) {
const result = originalFetchHandler(...args);
spy.mockReturnValueOnce(result);
spy.apply(this, args);
return result;
};
// make sure all the jest expectation helpers can find what they need on fetchMock.mock
Object.assign(jestifiedInstance.mock, spy.mock);
['_isMockFunction', 'mockName', 'getMockName'].forEach((prop) => {
jestifiedInstance[prop] = spy[prop];
});
jestifiedInstance.mockClear = () => {
spy.mockClear();
jestifiedInstance.resetHistory();
Object.assign(jestifiedInstance.mock, spy.mock);
};
jestifiedInstance.mockReset = () => {
spy.mockReset();
jestifiedInstance.reset();
Object.assign(jestifiedInstance.mock, spy.mock);
};
jestifiedInstance.mockRestore = () => {
throw new Error(
"mockRestore not supported on fetch-mock. Use fetch-mock's methods to manage mock responses"
);
};
jestifiedInstance.mockImplementation = () => {
throw new Error(
"mockImplementation not supported on fetch-mock. Use fetch-mock's methods to manage mock responses"
);
};
jestifiedInstance.mockImplementationOnce = () => {
throw new Error(
"mockImplementationOnce not supported on fetch-mock. Use fetch-mock's methods to manage mock responses"
);
};
jestifiedInstance.mockName = () => {
throw new Error(
"mockName not supported on fetch-mock. Use fetch-mock's methods to manage mock responses"
);
};
jestifiedInstance.mockReturnThis = () => {
throw new Error(
"mockReturnThis not supported on fetch-mock. Use fetch-mock's methods to manage mock responses"
);
};
jestifiedInstance.mockReturnValue = () => {
throw new Error(
"mockReturnValue not supported on fetch-mock. Use fetch-mock's methods to manage mock responses"
);
};
jestifiedInstance.mockReturnValueOnce = () => {
throw new Error(
"mockReturnValueOnce not supported on fetch-mock. Use fetch-mock's methods to manage mock responses"
);
};
jestifiedInstance.mockResolvedValue = () => {
throw new Error(
"mockResolvedValue not supported on fetch-mock. Use fetch-mock's methods to manage mock responses"
);
};
jestifiedInstance.mockResolvedValueOnce = () => {
throw new Error(
"mockResolvedValueOnce not supported on fetch-mock. Use fetch-mock's methods to manage mock responses"
);
};
jestifiedInstance.mockRejectedValue = () => {
throw new Error(
"mockRejectedValue not supported on fetch-mock. Use fetch-mock's methods to manage mock responses"
);
};
jestifiedInstance.mockRejectedValueOnce = () => {
throw new Error(
"mockRejectedValueOnce not supported on fetch-mock. Use fetch-mock's methods to manage mock responses"
);
};
// make sure that the mock object that has properties updated
// by the jest spy is the one that is exposed on fetch
spy.mock = jestifiedInstance.mock;
// Return this monster!
return jestifiedInstance;
};
module.exports = (fetchMock) => jestify(fetchMock);