[jest] add mock result type

This commit is contained in:
antoinebrault
2019-02-12 17:06:30 -05:00
parent fb4c74ca5b
commit 17a24edb01
2 changed files with 33 additions and 11 deletions
+20 -11
View File
@@ -976,20 +976,29 @@ declare namespace jest {
}
/**
* Represents the result of a single call to a mock function.
* Represents the result of a single call to a mock function with a return value.
*/
interface MockResult {
/**
* True if the function threw.
* False if the function returned.
*/
isThrow: boolean;
/**
* The value that was either thrown or returned by the function.
*/
interface MockResultReturn<T> {
type: 'return';
value: T;
}
/**
* Represents the result of a single incomplete call to a mock function.
*/
interface MockResultIncomplete {
type: 'incomplete';
value: undefined;
}
/**
* Represents the result of a single call to a mock function with a thrown error.
*/
interface MockResultThrow {
type: 'throw';
value: any;
}
type MockResult<T> = MockResultReturn<T> | MockResultThrow | MockResultIncomplete;
interface MockContext<T, Y extends any[]> {
calls: Y[];
instances: T[];
@@ -997,7 +1006,7 @@ declare namespace jest {
/**
* List of results of calls to the mock function.
*/
results: MockResult[];
results: Array<MockResult<T>>;
}
}
+13
View File
@@ -480,6 +480,19 @@ mocked.test4.mockRejectedValue(new Error());
// $ExpectError
mocked.test4.mockRejectedValueOnce(new Error());
const mockResult = jest.fn(() => 1).mock.results[0];
switch (mockResult.type) {
case 'return':
mockResult.value; // $ExpectType number
break;
case 'incomplete':
mockResult.value; // $ExpectType undefined
break;
case 'throw':
mockResult.value; // $ExpectType any
break;
}
/* Snapshot serialization */
const snapshotSerializerPlugin: jest.SnapshotSerializerPlugin = {