1 | "use strict";
|
2 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3 | if (k2 === undefined) k2 = k;
|
4 | var desc = Object.getOwnPropertyDescriptor(m, k);
|
5 | if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6 | desc = { enumerable: true, get: function() { return m[k]; } };
|
7 | }
|
8 | Object.defineProperty(o, k2, desc);
|
9 | }) : (function(o, m, k, k2) {
|
10 | if (k2 === undefined) k2 = k;
|
11 | o[k2] = m[k];
|
12 | }));
|
13 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
14 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
15 | }) : function(o, v) {
|
16 | o["default"] = v;
|
17 | });
|
18 | var __importStar = (this && this.__importStar) || function (mod) {
|
19 | if (mod && mod.__esModule) return mod;
|
20 | var result = {};
|
21 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
22 | __setModuleDefault(result, mod);
|
23 | return result;
|
24 | };
|
25 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
26 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
27 | };
|
28 | Object.defineProperty(exports, "__esModule", { value: true });
|
29 | exports.Test6 = void 0;
|
30 | const Mock_1 = __importStar(require("./Mock"));
|
31 | const Matchers_1 = require("./Matchers");
|
32 | const CalledWithFn_1 = __importDefault(require("./CalledWithFn"));
|
33 | class Test1 {
|
34 | constructor(id) {
|
35 | this.deepProp = new Test2();
|
36 | this.id = id;
|
37 | this.anotherPart = id;
|
38 | }
|
39 | ofAnother(test) {
|
40 | return test.getNumber();
|
41 | }
|
42 | getNumber() {
|
43 | return this.id;
|
44 | }
|
45 | getNumberWithMockArg(mock) {
|
46 | return this.id;
|
47 | }
|
48 | getSomethingWithArgs(arg1, arg2) {
|
49 | return this.id;
|
50 | }
|
51 | getSomethingWithMoreArgs(arg1, arg2, arg3) {
|
52 | return this.id;
|
53 | }
|
54 | }
|
55 | class Test2 {
|
56 | constructor() {
|
57 | this.deeperProp = new Test3();
|
58 | }
|
59 | getNumber(num) {
|
60 | return num * 2;
|
61 | }
|
62 | getAnotherString(str) {
|
63 | return `${str} another string`;
|
64 | }
|
65 | }
|
66 | class Test3 {
|
67 | getNumber(num) {
|
68 | return num ^ 2;
|
69 | }
|
70 | }
|
71 | class Test4 {
|
72 | constructor(test1, int) { }
|
73 | }
|
74 | class Test6 {
|
75 | constructor(funcValueProp, id) {
|
76 | this.id = id;
|
77 | this.funcValueProp = funcValueProp;
|
78 | }
|
79 | }
|
80 | exports.Test6 = Test6;
|
81 | describe('jest-mock-extended', () => {
|
82 | test('Can be assigned back to itself even when there are private parts', () => {
|
83 |
|
84 | const mockObj = (0, Mock_1.default)();
|
85 |
|
86 | new Test1(1).ofAnother(mockObj);
|
87 | expect(mockObj.getNumber).toHaveBeenCalledTimes(1);
|
88 | });
|
89 | test('Check that a jest.fn() is created without any invocation to the mock method', () => {
|
90 | const mockObj = (0, Mock_1.default)();
|
91 | expect(mockObj.getNumber).toHaveBeenCalledTimes(0);
|
92 | });
|
93 | test('Check that invocations are registered', () => {
|
94 | const mockObj = (0, Mock_1.default)();
|
95 | mockObj.getNumber();
|
96 | mockObj.getNumber();
|
97 | expect(mockObj.getNumber).toHaveBeenCalledTimes(2);
|
98 | });
|
99 | test('Can mock a return value', () => {
|
100 | const mockObj = (0, Mock_1.default)();
|
101 | mockObj.getNumber.mockReturnValue(12);
|
102 | expect(mockObj.getNumber()).toBe(12);
|
103 | });
|
104 | test('Can specify args', () => {
|
105 | const mockObj = (0, Mock_1.default)();
|
106 | mockObj.getSomethingWithArgs(1, 2);
|
107 | expect(mockObj.getSomethingWithArgs).toBeCalledWith(1, 2);
|
108 | });
|
109 | test('Can specify calledWith', () => {
|
110 | const mockObj = (0, Mock_1.default)();
|
111 | mockObj.getSomethingWithArgs.calledWith(1, 2).mockReturnValue(1);
|
112 | expect(mockObj.getSomethingWithArgs(1, 2)).toBe(1);
|
113 | });
|
114 | test('Can specify fallbackMockImplementation', () => {
|
115 | const mockObj = (0, Mock_1.default)({}, {
|
116 | fallbackMockImplementation: () => {
|
117 | throw new Error('not mocked');
|
118 | },
|
119 | });
|
120 | expect(() => mockObj.getSomethingWithArgs(1, 2)).toThrowError('not mocked');
|
121 | });
|
122 | test('Can specify multiple calledWith', () => {
|
123 | const mockObj = (0, Mock_1.default)();
|
124 | mockObj.getSomethingWithArgs.calledWith(1, 2).mockReturnValue(3);
|
125 | mockObj.getSomethingWithArgs.calledWith(6, 7).mockReturnValue(13);
|
126 | expect(mockObj.getSomethingWithArgs(1, 2)).toBe(3);
|
127 | expect(mockObj.getSomethingWithArgs(6, 7)).toBe(13);
|
128 | });
|
129 | test('Can set props', () => {
|
130 | const mockObj = (0, Mock_1.default)();
|
131 | mockObj.id = 17;
|
132 | expect(mockObj.id).toBe(17);
|
133 | });
|
134 | test('Can set false and null boolean props', () => {
|
135 | const mockObj = (0, Mock_1.default)({
|
136 | someValue: false,
|
137 | });
|
138 | const mockObj2 = (0, Mock_1.default)({
|
139 | someValue: null,
|
140 | });
|
141 | expect(mockObj.someValue).toBe(false);
|
142 | expect(mockObj2.someValue).toBe(null);
|
143 | });
|
144 | test('can set undefined explicitly', () => {
|
145 | const mockObj = (0, Mock_1.default)({
|
146 | someValue: undefined,
|
147 | });
|
148 | expect(mockObj.someValue).toBe(undefined);
|
149 | });
|
150 | test('Equals self', () => {
|
151 | const mockObj = (0, Mock_1.default)();
|
152 | expect(mockObj).toBe(mockObj);
|
153 | expect(mockObj).toEqual(mockObj);
|
154 | const spy = jest.fn();
|
155 | spy(mockObj);
|
156 | expect(spy).toHaveBeenCalledWith(mockObj);
|
157 | });
|
158 | describe('Mimic Type', () => {
|
159 | test('can use MockProxy in place of Mock Type', () => {
|
160 | const t1 = (0, Mock_1.default)();
|
161 | const i1 = (0, Mock_1.default)();
|
162 |
|
163 | const f = new Test4(t1, i1);
|
164 | });
|
165 | });
|
166 | describe('calledWith', () => {
|
167 | test('can use calledWith without mock', () => {
|
168 | const mockFunc = (0, CalledWithFn_1.default)();
|
169 | mockFunc.calledWith((0, Matchers_1.anyNumber)(), (0, Matchers_1.anyNumber)()).mockReturnValue(3);
|
170 | expect(mockFunc(1, 2)).toBe(3);
|
171 | });
|
172 | test('Can specify matchers', () => {
|
173 | const mockObj = (0, Mock_1.default)();
|
174 | mockObj.getSomethingWithArgs.calledWith((0, Matchers_1.anyNumber)(), (0, Matchers_1.anyNumber)()).mockReturnValue(3);
|
175 | expect(mockObj.getSomethingWithArgs(1, 2)).toBe(3);
|
176 | });
|
177 | test('does not match when one arg does not match Matcher', () => {
|
178 | const mockObj = (0, Mock_1.default)();
|
179 | mockObj.getSomethingWithArgs.calledWith((0, Matchers_1.anyNumber)(), (0, Matchers_1.anyNumber)()).mockReturnValue(3);
|
180 |
|
181 | expect(mockObj.getSomethingWithArgs('1', 2)).toBe(undefined);
|
182 | });
|
183 | test('can use literals', () => {
|
184 | const mockObj = (0, Mock_1.default)();
|
185 | mockObj.getSomethingWithArgs.calledWith(1, 2).mockReturnValue(3);
|
186 | expect(mockObj.getSomethingWithArgs(1, 2)).toBe(3);
|
187 | });
|
188 | test('can mix Matchers with literals', () => {
|
189 | const mockObj = (0, Mock_1.default)();
|
190 | mockObj.getSomethingWithArgs.calledWith(1, (0, Matchers_1.anyNumber)()).mockReturnValue(3);
|
191 | expect(mockObj.getSomethingWithArgs(1, 2)).toBe(3);
|
192 | });
|
193 | test('supports multiple calledWith', () => {
|
194 | const mockObj = (0, Mock_1.default)();
|
195 | mockObj.getSomethingWithArgs.calledWith(2, (0, Matchers_1.anyNumber)()).mockReturnValue(4);
|
196 | mockObj.getSomethingWithArgs.calledWith(1, (0, Matchers_1.anyNumber)()).mockReturnValue(3);
|
197 | mockObj.getSomethingWithArgs.calledWith(6, (0, Matchers_1.anyNumber)()).mockReturnValue(7);
|
198 | expect(mockObj.getSomethingWithArgs(2, 2)).toBe(4);
|
199 | expect(mockObj.getSomethingWithArgs(1, 2)).toBe(3);
|
200 | expect(mockObj.getSomethingWithArgs(6, 2)).toBe(7);
|
201 | expect(mockObj.getSomethingWithArgs(7, 2)).toBe(undefined);
|
202 | });
|
203 | test('supports overriding with same args', () => {
|
204 | const mockObj = (0, Mock_1.default)();
|
205 | mockObj.getSomethingWithArgs.calledWith(1, 2).mockReturnValue(4);
|
206 | mockObj.getSomethingWithArgs.calledWith(1, 2).mockReturnValue(3);
|
207 | expect(mockObj.getSomethingWithArgs(1, 2)).toBe(3);
|
208 | });
|
209 | test('Support jest matcher', () => {
|
210 | const mockObj = (0, Mock_1.default)();
|
211 | mockObj.getSomethingWithArgs.calledWith(expect.anything(), expect.anything()).mockReturnValue(3);
|
212 | expect(mockObj.getSomethingWithArgs(1, 2)).toBe(3);
|
213 | });
|
214 | test('Suport mix Matchers with literals and with jest matcher', () => {
|
215 | const mockObj = (0, Mock_1.default)();
|
216 | mockObj.getSomethingWithMoreArgs.calledWith((0, Matchers_1.anyNumber)(), expect.anything(), 3).mockReturnValue(4);
|
217 | expect(mockObj.getSomethingWithMoreArgs(1, 2, 3)).toBe(4);
|
218 | expect(mockObj.getSomethingWithMoreArgs(1, 2, 4)).toBeUndefined;
|
219 | });
|
220 | test('Can use calledWith with an other mock', () => {
|
221 | const mockObj = (0, Mock_1.default)();
|
222 | const mockArg = (0, Mock_1.default)();
|
223 | mockObj.getNumberWithMockArg.calledWith(mockArg).mockReturnValue(4);
|
224 | expect(mockObj.getNumberWithMockArg(mockArg)).toBe(4);
|
225 | });
|
226 | });
|
227 | describe('Matchers with toHaveBeenCalledWith', () => {
|
228 | test('matchers allow all args to be Matcher based', () => {
|
229 | const mockObj = (0, Mock_1.default)();
|
230 | mockObj.getSomethingWithArgs(2, 4);
|
231 | expect(mockObj.getSomethingWithArgs).toHaveBeenCalledWith((0, Matchers_1.anyNumber)(), (0, Matchers_1.anyNumber)());
|
232 | });
|
233 | test('matchers allow for a mix of Matcher and literal', () => {
|
234 | const mockObj = (0, Mock_1.default)();
|
235 | mockObj.getSomethingWithArgs(2, 4);
|
236 | expect(mockObj.getSomethingWithArgs).toHaveBeenCalledWith((0, Matchers_1.anyNumber)(), 4);
|
237 | });
|
238 | test('matchers allow for not.toHaveBeenCalledWith', () => {
|
239 | const mockObj = (0, Mock_1.default)();
|
240 | mockObj.getSomethingWithArgs(2, 4);
|
241 | expect(mockObj.getSomethingWithArgs).not.toHaveBeenCalledWith((0, Matchers_1.anyNumber)(), 5);
|
242 | });
|
243 | });
|
244 | describe('Deep mock support', () => {
|
245 | test('can deep mock members', () => {
|
246 | const mockObj = (0, Mock_1.mockDeep)();
|
247 | mockObj.deepProp.getNumber.calledWith(1).mockReturnValue(4);
|
248 | expect(mockObj.deepProp.getNumber(1)).toBe(4);
|
249 | });
|
250 | test('three level deep mock', () => {
|
251 | const mockObj = (0, Mock_1.mockDeep)();
|
252 | mockObj.deepProp.deeperProp.getNumber.calledWith(1).mockReturnValue(4);
|
253 | expect(mockObj.deepProp.deeperProp.getNumber(1)).toBe(4);
|
254 | });
|
255 | test('maintains API for deep mocks', () => {
|
256 | const mockObj = (0, Mock_1.mockDeep)();
|
257 | mockObj.deepProp.getNumber(100);
|
258 | expect(mockObj.deepProp.getNumber.mock.calls[0][0]).toBe(100);
|
259 | });
|
260 | test('non deep expectation work as expected', () => {
|
261 | const mockObj = (0, Mock_1.mockDeep)();
|
262 | new Test1(1).ofAnother(mockObj);
|
263 | expect(mockObj.getNumber).toHaveBeenCalledTimes(1);
|
264 | });
|
265 | test('deep expectation work as expected', () => {
|
266 | const mockObj = (0, Mock_1.mockDeep)();
|
267 | mockObj.deepProp.getNumber(2);
|
268 | expect(mockObj.deepProp.getNumber).toHaveBeenCalledTimes(1);
|
269 | });
|
270 | test('fallback mock implementation can be overridden', () => {
|
271 | const mockObj = (0, Mock_1.mockDeep)({
|
272 | fallbackMockImplementation: () => {
|
273 | throw new Error('not mocked');
|
274 | },
|
275 | });
|
276 | mockObj.deepProp.getAnotherString.calledWith('foo');
|
277 | expect(() => mockObj.getNumber()).toThrowError('not mocked');
|
278 | expect(() => mockObj.deepProp.getAnotherString('foo')).toThrowError('not mocked');
|
279 | });
|
280 | test('fallback mock implementation can be overridden while also providing a mock implementation', () => {
|
281 | const mockObj = (0, Mock_1.mockDeep)({
|
282 | fallbackMockImplementation: () => {
|
283 | throw new Error('not mocked');
|
284 | },
|
285 | }, {
|
286 | getNumber: () => {
|
287 | return 150;
|
288 | },
|
289 | });
|
290 | mockObj.deepProp.getAnotherString.calledWith('?').mockReturnValue('mocked');
|
291 | expect(mockObj.getNumber()).toBe(150);
|
292 | expect(mockObj.deepProp.getAnotherString('?')).toBe('mocked');
|
293 | expect(() => mockObj.deepProp.getNumber(1)).toThrowError('not mocked');
|
294 | expect(() => mockObj.deepProp.getAnotherString('!')).toThrowError('not mocked');
|
295 | });
|
296 | });
|
297 | describe('Deep mock support for class variables which are functions but also have nested properties and functions', () => {
|
298 | test('can deep mock members', () => {
|
299 | const mockObj = (0, Mock_1.mockDeep)({ funcPropSupport: true });
|
300 | const input = new Test1(1);
|
301 | mockObj.funcValueProp.nonDeepProp.calledWith(input).mockReturnValue(4);
|
302 | expect(mockObj.funcValueProp.nonDeepProp(input)).toBe(4);
|
303 | });
|
304 | test('three or more level deep mock', () => {
|
305 | const mockObj = (0, Mock_1.mockDeep)({ funcPropSupport: true });
|
306 | mockObj.funcValueProp.deepProp.deeperProp.getNumber.calledWith(1).mockReturnValue(4);
|
307 | expect(mockObj.funcValueProp.deepProp.deeperProp.getNumber(1)).toBe(4);
|
308 | });
|
309 | test('maintains API for deep mocks', () => {
|
310 | const mockObj = (0, Mock_1.mockDeep)({ funcPropSupport: true });
|
311 | mockObj.funcValueProp.deepProp.getNumber(100);
|
312 | expect(mockObj.funcValueProp.deepProp.getNumber.mock.calls[0][0]).toBe(100);
|
313 | });
|
314 | test('deep expectation work as expected', () => {
|
315 | const mockObj = (0, Mock_1.mockDeep)();
|
316 | mockObj.funcValueProp.deepProp.getNumber(2);
|
317 | expect(mockObj.funcValueProp.deepProp.getNumber).toHaveBeenCalledTimes(1);
|
318 | });
|
319 | test('can mock base function which have properties', () => {
|
320 | const mockObj = (0, Mock_1.mockDeep)();
|
321 | mockObj.funcValueProp.calledWith(1).mockReturnValue(2);
|
322 | expect(mockObj.funcValueProp(1)).toBe(2);
|
323 | });
|
324 | test('base function expectation work as expected', () => {
|
325 | const mockObj = (0, Mock_1.mockDeep)();
|
326 | mockObj.funcValueProp(1);
|
327 | expect(mockObj.funcValueProp).toHaveBeenCalledTimes(1);
|
328 | });
|
329 | });
|
330 | describe('mock implementation support', () => {
|
331 | test('can provide mock implementation for props', () => {
|
332 | const mockObj = (0, Mock_1.default)({
|
333 | id: 61,
|
334 | });
|
335 | expect(mockObj.id).toBe(61);
|
336 | });
|
337 | test('can provide mock implementation for functions', () => {
|
338 | const mockObj = (0, Mock_1.default)({
|
339 | getNumber: () => {
|
340 | return 150;
|
341 | },
|
342 | });
|
343 | expect(mockObj.getNumber()).toBe(150);
|
344 | });
|
345 | test('Partially mocked implementations can have non-mocked function expectations', () => {
|
346 | const mockObj = (0, Mock_1.default)({
|
347 | getNumber: () => {
|
348 | return 150;
|
349 | },
|
350 | });
|
351 | mockObj.getSomethingWithArgs.calledWith(1, 2).mockReturnValue(3);
|
352 | expect(mockObj.getSomethingWithArgs(1, 2)).toBe(3);
|
353 | });
|
354 | test('can provide deep mock implementations', () => {
|
355 | const mockObj = (0, Mock_1.mockDeep)({
|
356 | deepProp: {
|
357 | getNumber: (num) => {
|
358 | return 76;
|
359 | },
|
360 | },
|
361 | });
|
362 | expect(mockObj.deepProp.getNumber(123)).toBe(76);
|
363 | });
|
364 | test('Partially mocked implementations of deep mocks can have non-mocked function expectations', () => {
|
365 | const mockObj = (0, Mock_1.mockDeep)({
|
366 | deepProp: {
|
367 | getNumber: (num) => {
|
368 | return 76;
|
369 | },
|
370 | },
|
371 | });
|
372 | mockObj.deepProp.getAnotherString.calledWith('abc').mockReturnValue('this string');
|
373 | expect(mockObj.deepProp.getAnotherString('abc')).toBe('this string');
|
374 | });
|
375 | });
|
376 | describe('Promise', () => {
|
377 | test('Can return as Promise.resolve', async () => {
|
378 | const mockObj = (0, Mock_1.default)();
|
379 | mockObj.id = 17;
|
380 | const promiseMockObj = Promise.resolve(mockObj);
|
381 | await expect(promiseMockObj).resolves.toBeDefined();
|
382 | await expect(promiseMockObj).resolves.toMatchObject({ id: 17 });
|
383 | });
|
384 | test('Can return as Promise.reject', async () => {
|
385 | const mockError = (0, Mock_1.default)();
|
386 | mockError.message = '17';
|
387 | const promiseMockObj = Promise.reject(mockError);
|
388 | try {
|
389 | await promiseMockObj;
|
390 | fail('Promise must be rejected');
|
391 | }
|
392 | catch (e) {
|
393 | await expect(e).toBeDefined();
|
394 | await expect(e).toBe(mockError);
|
395 | await expect(e).toHaveProperty('message', '17');
|
396 | }
|
397 | await expect(promiseMockObj).rejects.toBeDefined();
|
398 | await expect(promiseMockObj).rejects.toBe(mockError);
|
399 | await expect(promiseMockObj).rejects.toHaveProperty('message', '17');
|
400 | });
|
401 | test('Can mock a then function', async () => {
|
402 | const mockPromiseObj = Promise.resolve(42);
|
403 | const mockObj = (0, Mock_1.default)();
|
404 | mockObj.id = 17;
|
405 |
|
406 | mockObj.then = mockPromiseObj.then.bind(mockPromiseObj);
|
407 | const promiseMockObj = Promise.resolve(mockObj);
|
408 | await promiseMockObj;
|
409 | await expect(promiseMockObj).resolves.toBeDefined();
|
410 | await expect(promiseMockObj).resolves.toEqual(42);
|
411 | });
|
412 | });
|
413 | describe('clearing / resetting', () => {
|
414 | test('mockReset supports jest.fn()', () => {
|
415 | const fn = jest.fn().mockImplementation(() => true);
|
416 | expect(fn()).toBe(true);
|
417 | (0, Mock_1.mockReset)(fn);
|
418 | expect(fn()).toBe(undefined);
|
419 | });
|
420 | test('mockClear supports jest.fn()', () => {
|
421 | const fn = jest.fn().mockImplementation(() => true);
|
422 | fn();
|
423 | expect(fn.mock.calls.length).toBe(1);
|
424 | (0, Mock_1.mockClear)(fn);
|
425 | expect(fn.mock.calls.length).toBe(0);
|
426 | });
|
427 | test('mockReset object', () => {
|
428 | const mockObj = (0, Mock_1.default)();
|
429 | mockObj.getSomethingWithArgs.calledWith(1, (0, Matchers_1.anyNumber)()).mockReturnValue(3);
|
430 | expect(mockObj.getSomethingWithArgs(1, 2)).toBe(3);
|
431 | (0, Mock_1.mockReset)(mockObj);
|
432 | expect(mockObj.getSomethingWithArgs(1, 2)).toBe(undefined);
|
433 | mockObj.getSomethingWithArgs.calledWith(1, (0, Matchers_1.anyNumber)()).mockReturnValue(3);
|
434 | expect(mockObj.getSomethingWithArgs(1, 2)).toBe(3);
|
435 | });
|
436 | test('mockClear object', () => {
|
437 | const mockObj = (0, Mock_1.default)();
|
438 | mockObj.getSomethingWithArgs.calledWith(1, (0, Matchers_1.anyNumber)()).mockReturnValue(3);
|
439 | expect(mockObj.getSomethingWithArgs(1, 2)).toBe(3);
|
440 | expect(mockObj.getSomethingWithArgs.mock.calls.length).toBe(1);
|
441 | (0, Mock_1.mockClear)(mockObj);
|
442 | expect(mockObj.getSomethingWithArgs.mock.calls.length).toBe(0);
|
443 |
|
444 | expect(mockObj.getSomethingWithArgs(1, 2)).toBe(3);
|
445 | });
|
446 | test('mockReset deep', () => {
|
447 | const mockObj = (0, Mock_1.mockDeep)();
|
448 | mockObj.deepProp.getNumber.calledWith(1).mockReturnValue(4);
|
449 | expect(mockObj.deepProp.getNumber(1)).toBe(4);
|
450 | (0, Mock_1.mockReset)(mockObj);
|
451 | expect(mockObj.deepProp.getNumber(1)).toBe(undefined);
|
452 | });
|
453 | test('mockClear deep', () => {
|
454 | const mockObj = (0, Mock_1.mockDeep)();
|
455 | mockObj.deepProp.getNumber.calledWith(1).mockReturnValue(4);
|
456 | expect(mockObj.deepProp.getNumber(1)).toBe(4);
|
457 | expect(mockObj.deepProp.getNumber.mock.calls.length).toBe(1);
|
458 | (0, Mock_1.mockClear)(mockObj);
|
459 | expect(mockObj.deepProp.getNumber.mock.calls.length).toBe(0);
|
460 |
|
461 | expect(mockObj.deepProp.getNumber(1)).toBe(4);
|
462 | });
|
463 | test('mockReset ignores undefined properties', () => {
|
464 | const mockObj = (0, Mock_1.default)();
|
465 | mockObj.someValue = undefined;
|
466 | mockObj.getSomethingWithArgs.calledWith(1, (0, Matchers_1.anyNumber)()).mockReturnValue(3);
|
467 | (0, Mock_1.mockReset)(mockObj);
|
468 | expect(mockObj.getSomethingWithArgs(1, 2)).toBe(undefined);
|
469 | });
|
470 | test('mockReset ignores null properties', () => {
|
471 | const mockObj = (0, Mock_1.default)();
|
472 | mockObj.someValue = null;
|
473 | mockObj.getSomethingWithArgs.calledWith(1, (0, Matchers_1.anyNumber)()).mockReturnValue(3);
|
474 | (0, Mock_1.mockReset)(mockObj);
|
475 | expect(mockObj.getSomethingWithArgs(1, 2)).toBe(undefined);
|
476 | });
|
477 | test('mockClear ignores undefined properties', () => {
|
478 | const mockObj = (0, Mock_1.default)();
|
479 | mockObj.someValue = undefined;
|
480 | mockObj.getSomethingWithArgs.calledWith(1, (0, Matchers_1.anyNumber)()).mockReturnValue(3);
|
481 | expect(mockObj.getSomethingWithArgs(1, 2)).toBe(3);
|
482 | expect(mockObj.getSomethingWithArgs.mock.calls.length).toBe(1);
|
483 | (0, Mock_1.mockClear)(mockObj);
|
484 | expect(mockObj.getSomethingWithArgs.mock.calls.length).toBe(0);
|
485 | });
|
486 | test('mockClear ignores null properties', () => {
|
487 | const mockObj = (0, Mock_1.default)();
|
488 | mockObj.someValue = null;
|
489 | mockObj.getSomethingWithArgs.calledWith(1, (0, Matchers_1.anyNumber)()).mockReturnValue(3);
|
490 | expect(mockObj.getSomethingWithArgs(1, 2)).toBe(3);
|
491 | expect(mockObj.getSomethingWithArgs.mock.calls.length).toBe(1);
|
492 | (0, Mock_1.mockClear)(mockObj);
|
493 | expect(mockObj.getSomethingWithArgs.mock.calls.length).toBe(0);
|
494 | });
|
495 | });
|
496 | describe('function mock', () => {
|
497 | test('should mock function', async () => {
|
498 | const mockFunc = (0, Mock_1.mockFn)();
|
499 | mockFunc.mockResolvedValue(`str`);
|
500 | const result = await mockFunc(1, 2);
|
501 | expect(result).toBe(`str`);
|
502 | });
|
503 | test('should mock function and use calledWith', async () => {
|
504 | const mockFunc = (0, Mock_1.mockFn)();
|
505 | mockFunc.calledWith(1, 2).mockResolvedValue(`str`);
|
506 | const result = await mockFunc(1, 2);
|
507 | expect(result).toBe(`str`);
|
508 | });
|
509 | });
|
510 | describe('ignoreProps', () => {
|
511 | test('can configure ignoreProps', async () => {
|
512 | Mock_1.JestMockExtended.configure({ ignoreProps: ['ignoreMe'] });
|
513 | const mockObj = (0, Mock_1.default)();
|
514 | expect(mockObj.ignoreMe).toBeUndefined();
|
515 | expect(mockObj.dontIgnoreMe).toBeDefined();
|
516 | });
|
517 | });
|
518 | describe('JestMockExtended config', () => {
|
519 | test('can mock then', async () => {
|
520 | Mock_1.JestMockExtended.configure({ ignoreProps: [] });
|
521 | const mockObj = (0, Mock_1.default)();
|
522 | mockObj.then();
|
523 | expect(mockObj.then).toHaveBeenCalled();
|
524 | });
|
525 | test('can reset config', async () => {
|
526 | Mock_1.JestMockExtended.configure({ ignoreProps: [] });
|
527 | Mock_1.JestMockExtended.resetConfig();
|
528 | const mockObj = (0, Mock_1.default)();
|
529 | expect(mockObj.then).toBeUndefined();
|
530 | });
|
531 | });
|
532 | describe('mock Date', () => {
|
533 | test('should call built-in date functions', () => {
|
534 | const mockObj = (0, Mock_1.default)({ date: new Date('2000-01-15') });
|
535 | expect(mockObj.date.getFullYear()).toBe(2000);
|
536 | expect(mockObj.date.getMonth()).toBe(0);
|
537 | expect(mockObj.date.getDate()).toBe(15);
|
538 | });
|
539 | });
|
540 | });
|