mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-10-06 15:57:13 +00:00
Merge pull request #33013 from antoinebrault/jest-mockreturn-type
[jest] add mock result type property
This commit is contained in:
Vendored
+20
-11
@@ -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>>;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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 = {
|
||||
|
||||
Reference in New Issue
Block a user