Skip to content

Commit 99d9cff

Browse files
palmerj3cpojer
authored andcommitted
Make sure function mocks match original arity (#4170)
* Make sure function mocks match original arity * Mocks maintain function arity with 9 or less arguments * Update index.js
1 parent d882674 commit 99d9cff

2 files changed

Lines changed: 73 additions & 3 deletions

File tree

packages/jest-mock/src/__tests__/jest_mock.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,14 @@ describe('moduleMocker', () => {
341341
expect(fn1()).not.toEqual('abcd');
342342
expect(fn2()).not.toEqual('abcd');
343343
});
344+
345+
it('maintains function arity', () => {
346+
const mockFunctionArity1 = moduleMocker.fn(x => x);
347+
const mockFunctionArity2 = moduleMocker.fn((x, y) => y);
348+
349+
expect(mockFunctionArity1.length).toBe(1);
350+
expect(mockFunctionArity2.length).toBe(2);
351+
});
344352
});
345353

346354
it('supports mock value returning undefined', () => {

packages/jest-mock/src/index.js

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export type MockFunctionMetadata = {
1919
refID?: string | number,
2020
type?: string,
2121
value?: any,
22+
length?: number,
2223
};
2324

2425
type MockFunctionState = {
@@ -88,6 +89,65 @@ const RESERVED_KEYWORDS = Object.assign(Object.create(null), {
8889
yield: true,
8990
});
9091

92+
function matchArity(fn: any, length: number): any {
93+
let mockConstructor;
94+
95+
switch (length) {
96+
case 1:
97+
mockConstructor = function(a) {
98+
return fn.apply(this, arguments);
99+
};
100+
break;
101+
case 2:
102+
mockConstructor = function(a, b) {
103+
return fn.apply(this, arguments);
104+
};
105+
break;
106+
case 3:
107+
mockConstructor = function(a, b, c) {
108+
return fn.apply(this, arguments);
109+
};
110+
break;
111+
case 4:
112+
mockConstructor = function(a, b, c, d) {
113+
return fn.apply(this, arguments);
114+
};
115+
break;
116+
case 5:
117+
mockConstructor = function(a, b, c, d, e) {
118+
return fn.apply(this, arguments);
119+
};
120+
break;
121+
case 6:
122+
mockConstructor = function(a, b, c, d, e, f) {
123+
return fn.apply(this, arguments);
124+
};
125+
break;
126+
case 7:
127+
mockConstructor = function(a, b, c, d, e, f, g) {
128+
return fn.apply(this, arguments);
129+
};
130+
break;
131+
case 8:
132+
mockConstructor = function(a, b, c, d, e, f, g, h) {
133+
return fn.apply(this, arguments);
134+
};
135+
break;
136+
case 9:
137+
mockConstructor = function(a, b, c, d, e, f, g, h, i) {
138+
return fn.apply(this, arguments);
139+
};
140+
break;
141+
default:
142+
mockConstructor = function() {
143+
return fn.apply(this, arguments);
144+
};
145+
break;
146+
}
147+
148+
return mockConstructor;
149+
}
150+
91151
function isA(typeName: string, value: any): boolean {
92152
return Object.prototype.toString.apply(value) === '[object ' + typeName + ']';
93153
}
@@ -242,7 +302,7 @@ class ModuleMockerClass {
242302
{};
243303
const prototypeSlots = getSlots(prototype);
244304
const mocker = this;
245-
const mockConstructor = function() {
305+
const mockConstructor = matchArity(function() {
246306
const mockState = mocker._ensureMockState(f);
247307
const mockConfig = mocker._ensureMockConfig(f);
248308
mockState.instances.push(this);
@@ -298,7 +358,7 @@ class ModuleMockerClass {
298358
}
299359

300360
return returnValue;
301-
};
361+
}, metadata.length || 0);
302362

303363
f = this._createMockFunction(metadata, mockConstructor);
304364
f._isMockFunction = true;
@@ -431,6 +491,7 @@ class ModuleMockerClass {
431491
MOCK_CONSTRUCTOR_NAME,
432492
body,
433493
);
494+
434495
return createConstructor(mockConstructor);
435496
}
436497

@@ -565,7 +626,8 @@ class ModuleMockerClass {
565626
}
566627

567628
fn(implementation?: any): any {
568-
const fn = this._makeComponent({type: 'function'});
629+
const length = implementation ? implementation.length : 0;
630+
const fn = this._makeComponent({length, type: 'function'});
569631
if (implementation) {
570632
fn.mockImplementation(implementation);
571633
}

0 commit comments

Comments
 (0)