UNPKG

5.3 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.stub = exports.mockFn = exports.mockDeep = exports.mockReset = exports.mockClear = exports.JestMockExtended = void 0;
7const CalledWithFn_1 = __importDefault(require("./CalledWithFn"));
8const DEFAULT_CONFIG = {
9 ignoreProps: ['then'],
10};
11let GLOBAL_CONFIG = DEFAULT_CONFIG;
12exports.JestMockExtended = {
13 DEFAULT_CONFIG,
14 configure: (config) => {
15 // Shallow merge so they can override anything they want.
16 GLOBAL_CONFIG = Object.assign(Object.assign({}, DEFAULT_CONFIG), config);
17 },
18 resetConfig: () => {
19 GLOBAL_CONFIG = DEFAULT_CONFIG;
20 },
21};
22const mockClear = (mock) => {
23 for (let key of Object.keys(mock)) {
24 if (mock[key] === null || mock[key] === undefined) {
25 continue;
26 }
27 if (mock[key]._isMockObject) {
28 (0, exports.mockClear)(mock[key]);
29 }
30 if (mock[key]._isMockFunction) {
31 mock[key].mockClear();
32 }
33 }
34 // This is a catch for if they pass in a jest.fn()
35 if (!mock._isMockObject) {
36 return mock.mockClear();
37 }
38};
39exports.mockClear = mockClear;
40const mockReset = (mock) => {
41 for (let key of Object.keys(mock)) {
42 if (mock[key] === null || mock[key] === undefined) {
43 continue;
44 }
45 if (mock[key]._isMockObject) {
46 (0, exports.mockReset)(mock[key]);
47 }
48 if (mock[key]._isMockFunction) {
49 mock[key].mockReset();
50 }
51 }
52 // This is a catch for if they pass in a jest.fn()
53 // Worst case, we will create a jest.fn() (since this is a proxy)
54 // below in the get and call mockReset on it
55 if (!mock._isMockObject) {
56 return mock.mockReset();
57 }
58};
59exports.mockReset = mockReset;
60function mockDeep(arg1, arg2) {
61 const [opts, mockImplementation] = typeof arg1 === 'object' && (typeof arg1.fallbackMockImplementation === 'function' || arg1.funcPropSupport === true)
62 ? [arg1, arg2]
63 : [{}, arg1];
64 return mock(mockImplementation, { deep: true, fallbackMockImplementation: opts.fallbackMockImplementation });
65}
66exports.mockDeep = mockDeep;
67const overrideMockImp = (obj, opts) => {
68 const proxy = new Proxy(obj, handler(opts));
69 for (let name of Object.keys(obj)) {
70 if (typeof obj[name] === 'object' && obj[name] !== null) {
71 proxy[name] = overrideMockImp(obj[name], opts);
72 }
73 else {
74 proxy[name] = obj[name];
75 }
76 }
77 return proxy;
78};
79const handler = (opts) => ({
80 ownKeys(target) {
81 return Reflect.ownKeys(target);
82 },
83 set: (obj, property, value) => {
84 // @ts-ignore All of these ignores are due to https://github.com/microsoft/TypeScript/issues/1863
85 obj[property] = value;
86 return true;
87 },
88 get: (obj, property) => {
89 var _a;
90 let fn = (0, CalledWithFn_1.default)({ fallbackMockImplementation: opts === null || opts === void 0 ? void 0 : opts.fallbackMockImplementation });
91 // @ts-ignore
92 if (!(property in obj)) {
93 if ((_a = GLOBAL_CONFIG.ignoreProps) === null || _a === void 0 ? void 0 : _a.includes(property)) {
94 return undefined;
95 }
96 // Jest's internal equality checking does some wierd stuff to check for iterable equality
97 if (property === Symbol.iterator) {
98 // @ts-ignore
99 return obj[property];
100 }
101 // So this calls check here is totally not ideal - jest internally does a
102 // check to see if this is a spy - which we want to say no to, but blindly returning
103 // an proxy for calls results in the spy check returning true. This is another reason
104 // why deep is opt in.
105 if ((opts === null || opts === void 0 ? void 0 : opts.deep) && property !== 'calls') {
106 // @ts-ignore
107 obj[property] = new Proxy(fn, handler(opts));
108 // @ts-ignore
109 obj[property]._isMockObject = true;
110 }
111 else {
112 // @ts-ignore
113 obj[property] = (0, CalledWithFn_1.default)({ fallbackMockImplementation: opts === null || opts === void 0 ? void 0 : opts.fallbackMockImplementation });
114 }
115 }
116 // @ts-ignore
117 if (obj instanceof Date && typeof obj[property] === 'function') {
118 // @ts-ignore
119 return obj[property].bind(obj);
120 }
121 // @ts-ignore
122 return obj[property];
123 },
124});
125const mock = (mockImplementation = {}, opts) => {
126 // @ts-ignore private
127 mockImplementation._isMockObject = true;
128 return overrideMockImp(mockImplementation, opts);
129};
130const mockFn = () => {
131 // @ts-ignore
132 return (0, CalledWithFn_1.default)();
133};
134exports.mockFn = mockFn;
135const stub = () => {
136 return new Proxy({}, {
137 get: (obj, property) => {
138 if (property in obj) {
139 // @ts-ignore
140 return obj[property];
141 }
142 return jest.fn();
143 },
144 });
145};
146exports.stub = stub;
147exports.default = mock;