mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-08-12 04:50:18 +00:00
[Jest] MockInstance functions should return self (#34936)
* [Jest] MockInstance functions should return self * [Jest] MockInstance functions should return self * [Jest] fixed tests * [jest-when] WhenMock interface should extend MockInstance
This commit is contained in:
committed by
Nathan Shively-Sanders
parent
dccf648e59
commit
88e22907b4
Vendored
+13
-12
@@ -2,24 +2,25 @@
|
||||
// Project: https://github.com/timkindberg/jest-when#readme
|
||||
// Definitions by: Alden Taylor <https://github.com/aldentaylor>
|
||||
// Trung Dang <https://github.com/immanuel192>
|
||||
// Gregor Stamać <https://github.com/gstamac>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 3.0
|
||||
|
||||
/// <reference types="jest" />
|
||||
|
||||
export interface WhenMock<T = any, Y extends any[] = any> extends jest.Mock<T, Y> {
|
||||
calledWith(...matchers: Y): WhenMock<T, Y>;
|
||||
expectCalledWith(...matchers: Y): WhenMock<T, Y>;
|
||||
mockReturnValue(value: T): WhenMock<T, Y>;
|
||||
mockReturnValueOnce(value: T): WhenMock<T, Y>;
|
||||
mockResolvedValue(value: jest.ResolvedValue<T>): WhenMock<T, Y>;
|
||||
mockResolvedValueOnce(value: jest.ResolvedValue<T>): WhenMock<T, Y>;
|
||||
mockRejectedValue(value: jest.RejectedValue<T>): WhenMock<T, Y>;
|
||||
mockRejectedValueOnce(value: jest.RejectedValue<T>): WhenMock<T, Y>;
|
||||
mockImplementation(fn: (...args: Y) => T): WhenMock<T, Y>;
|
||||
mockImplementationOnce(fn?: (...args: Y) => T): WhenMock<T, Y>;
|
||||
export interface WhenMock<T = any, Y extends any[] = any> extends jest.MockInstance<T, Y> {
|
||||
calledWith(...matchers: Y): this;
|
||||
expectCalledWith(...matchers: Y): this;
|
||||
mockReturnValue(value: T): this;
|
||||
mockReturnValueOnce(value: T): this;
|
||||
mockResolvedValue(value: jest.ResolvedValue<T>): this;
|
||||
mockResolvedValueOnce(value: jest.ResolvedValue<T>): this;
|
||||
mockRejectedValue(value: jest.RejectedValue<T>): this;
|
||||
mockRejectedValueOnce(value: jest.RejectedValue<T>): this;
|
||||
mockImplementation(fn: (...args: Y) => T): this;
|
||||
mockImplementationOnce(fn?: (...args: Y) => T): this;
|
||||
}
|
||||
|
||||
export type When = <T, Y extends any[]>(fn: jest.Mock<T, Y>) => WhenMock<T, Y>;
|
||||
export type When = <T, Y extends any[]>(fn: jest.MockInstance<T, Y>) => WhenMock<T, Y>;
|
||||
|
||||
export const when: When;
|
||||
|
||||
Vendored
+11
-10
@@ -19,6 +19,7 @@
|
||||
// Andy <https://github.com/andys8>
|
||||
// Antoine Brault <https://github.com/antoinebrault>
|
||||
// Jeroen Claassens <https://github.com/favna>
|
||||
// Gregor Stamać <https://github.com/gstamac>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 3.0
|
||||
|
||||
@@ -904,7 +905,7 @@ declare namespace jest {
|
||||
*
|
||||
* Note: `jest.fn(implementation)` is a shorthand for `jest.fn().mockImplementation(implementation)`.
|
||||
*/
|
||||
mockImplementation(fn?: (...args: Y) => T): Mock<T, Y>;
|
||||
mockImplementation(fn?: (...args: Y) => T): this;
|
||||
/**
|
||||
* Accepts a function that will be used as an implementation of the mock for one call to the mocked function.
|
||||
* Can be chained so that multiple function calls produce different results.
|
||||
@@ -920,9 +921,9 @@ declare namespace jest {
|
||||
*
|
||||
* myMockFn((err, val) => console.log(val)); // false
|
||||
*/
|
||||
mockImplementationOnce(fn: (...args: Y) => T): Mock<T, Y>;
|
||||
mockImplementationOnce(fn: (...args: Y) => T): this;
|
||||
/** Sets the name of the mock`. */
|
||||
mockName(name: string): Mock<T, Y>;
|
||||
mockName(name: string): this;
|
||||
/**
|
||||
* Just a simple sugar function for:
|
||||
*
|
||||
@@ -932,7 +933,7 @@ declare namespace jest {
|
||||
* return this;
|
||||
* });
|
||||
*/
|
||||
mockReturnThis(): Mock<T, Y>;
|
||||
mockReturnThis(): this;
|
||||
/**
|
||||
* Accepts a value that will be returned whenever the mock function is called.
|
||||
*
|
||||
@@ -944,7 +945,7 @@ declare namespace jest {
|
||||
* mock.mockReturnValue(43);
|
||||
* mock(); // 43
|
||||
*/
|
||||
mockReturnValue(value: T): Mock<T, Y>;
|
||||
mockReturnValue(value: T): this;
|
||||
/**
|
||||
* Accepts a value that will be returned for one call to the mock function. Can be chained so that
|
||||
* successive calls to the mock function return different values. When there are no more
|
||||
@@ -961,11 +962,11 @@ declare namespace jest {
|
||||
* console.log(myMockFn(), myMockFn(), myMockFn(), myMockFn());
|
||||
*
|
||||
*/
|
||||
mockReturnValueOnce(value: T): Mock<T, Y>;
|
||||
mockReturnValueOnce(value: T): this;
|
||||
/**
|
||||
* Simple sugar function for: `jest.fn().mockImplementation(() => Promise.resolve(value));`
|
||||
*/
|
||||
mockResolvedValue(value: ResolvedValue<T>): Mock<T, Y>;
|
||||
mockResolvedValue(value: ResolvedValue<T>): this;
|
||||
/**
|
||||
* Simple sugar function for: `jest.fn().mockImplementationOnce(() => Promise.resolve(value));`
|
||||
*
|
||||
@@ -985,7 +986,7 @@ declare namespace jest {
|
||||
* });
|
||||
*
|
||||
*/
|
||||
mockResolvedValueOnce(value: ResolvedValue<T>): Mock<T, Y>;
|
||||
mockResolvedValueOnce(value: ResolvedValue<T>): this;
|
||||
/**
|
||||
* Simple sugar function for: `jest.fn().mockImplementation(() => Promise.reject(value));`
|
||||
*
|
||||
@@ -997,7 +998,7 @@ declare namespace jest {
|
||||
* await asyncMock(); // throws "Async error"
|
||||
* });
|
||||
*/
|
||||
mockRejectedValue(value: RejectedValue<T>): Mock<T, Y>;
|
||||
mockRejectedValue(value: RejectedValue<T>): this;
|
||||
|
||||
/**
|
||||
* Simple sugar function for: `jest.fn().mockImplementationOnce(() => Promise.reject(value));`
|
||||
@@ -1015,7 +1016,7 @@ declare namespace jest {
|
||||
* });
|
||||
*
|
||||
*/
|
||||
mockRejectedValueOnce(value: RejectedValue<T>): Mock<T, Y>;
|
||||
mockRejectedValueOnce(value: RejectedValue<T>): this;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -463,22 +463,22 @@ class TestMocked {
|
||||
const mocked: jest.Mocked<TestMocked> = new TestMocked() as any;
|
||||
mocked.test1.mockImplementation(() => Promise.resolve({ a: 1 }));
|
||||
mocked.test1.mockReturnValue(Promise.resolve({ a: 1 }));
|
||||
// $ExpectType Mock<Promise<Type1>, [Type1]>
|
||||
// $ExpectType MockInstance<Promise<Type1>, [Type1]> & ((x: Type1) => Promise<Type1>)
|
||||
mocked.test1.mockResolvedValue({ a: 1 });
|
||||
mocked.test1.mockResolvedValueOnce({ a: 1 });
|
||||
// $ExpectType Mock<Promise<Type1>, [Type1]>
|
||||
// $ExpectType MockInstance<Promise<Type1>, [Type1]> & ((x: Type1) => Promise<Type1>)
|
||||
mocked.test1.mockResolvedValue(Promise.resolve({ a: 1 }));
|
||||
mocked.test1.mockResolvedValueOnce(Promise.resolve({ a: 1 }));
|
||||
// $ExpectType Mock<Promise<Type1>, [Promise<Type1>]>
|
||||
// $ExpectType MockInstance<Promise<Type1>, [Promise<Type1>]> & ((x: Promise<Type1>) => Promise<Type1>)
|
||||
mocked.test2.mockResolvedValue({ a: 1 });
|
||||
mocked.test2.mockResolvedValueOnce({ a: 1 });
|
||||
// $ExpectType Mock<Promise<Type1>, [Promise<Type1>]>
|
||||
// $ExpectType MockInstance<Promise<Type1>, [Promise<Type1>]> & ((x: Promise<Type1>) => Promise<Type1>)
|
||||
mocked.test2.mockResolvedValue(Promise.resolve({ a: 1 }));
|
||||
mocked.test2.mockResolvedValueOnce(Promise.resolve({ a: 1 }));
|
||||
// $ExpectType Mock<Promise<Type2>, [Promise<Type1>]>
|
||||
// $ExpectType MockInstance<Promise<Type2>, [Promise<Type1>]> & ((x: Promise<Type1>) => Promise<Type2>)
|
||||
mocked.test3.mockResolvedValue({ b: 1 });
|
||||
mocked.test3.mockResolvedValueOnce({ b: 1 });
|
||||
// $ExpectType Mock<Promise<Type2>, [Promise<Type1>]>
|
||||
// $ExpectType MockInstance<Promise<Type2>, [Promise<Type1>]> & ((x: Promise<Type1>) => Promise<Type2>)
|
||||
mocked.test3.mockResolvedValue(Promise.resolve({ b: 1 }));
|
||||
mocked.test3.mockResolvedValueOnce(Promise.resolve({ b: 1 }));
|
||||
mocked.test3.mockRejectedValue(new Error());
|
||||
|
||||
Reference in New Issue
Block a user