Merge pull request #33013 from antoinebrault/jest-mockreturn-type

[jest] add mock result type property
This commit is contained in:
Jessica Franco
2019-02-18 11:55:38 +09:00
committed by GitHub
2 changed files with 33 additions and 11 deletions
+20 -11
View File
@@ -978,20 +978,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[];
@@ -999,7 +1008,7 @@ declare namespace jest {
/**
* List of results of calls to the mock function.
*/
results: MockResult[];
results: Array<MockResult<T>>;
}
}
+13
View File
@@ -485,6 +485,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 = {