Skip to content

Commit 61ed7a0

Browse files
assign a default value when return values array is empty (#94)
1 parent cb45abc commit 61ed7a0

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/states/FunctionState.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,12 @@ export class FunctionState implements ContextState {
134134
|| property === SubstituteMethods.resolves
135135
|| property === SubstituteMethods.rejects
136136
) {
137-
return (...returns: any[]) => {
137+
return (...returnValues: any[]) => {
138138
if (!this._lastArgs) {
139-
throw SubstituteException.generic('Eh, there\'s a bug, no args recorded for this return :/')
139+
throw SubstituteException.generic('Eh, there\'s a bug, no args recorded for this return :/');
140140
}
141141
const returnMock: Partial<ReturnMock> = { returnIndex: 0, args: this._lastArgs };
142+
const returns = returnValues.length === 0 ? [void 0] : returnValues
142143
switch (property) {
143144
case SubstituteMethods.returns:
144145
returnMock.returnValues = returns;
@@ -149,6 +150,10 @@ export class FunctionState implements ContextState {
149150
case SubstituteMethods.rejects:
150151
returnMock.returnValues = returns.map(value => Promise.reject(value));
151152
break;
153+
default:
154+
throw SubstituteException.generic(
155+
`Expected one of the following methods: "${SubstituteMethods.returns}", "${SubstituteMethods.resolves}" or "${SubstituteMethods.rejects}"`
156+
);
152157
}
153158
this.returns.push(<ReturnMock>returnMock);
154159
this._calls.pop()

0 commit comments

Comments
 (0)