jest-mock
- Version 29.7.0
- Published
- 47.5 kB
- 3 dependencies
- MIT license
Install
npm i jest-mock
yarn add jest-mock
pnpm add jest-mock
Overview
**Note:** More details on user side API can be found in [Jest documentation](https://jestjs.io/docs/mock-function-api).
Index
Variables
Functions
Classes
Interfaces
Type Aliases
Variables
variable mocked
const mocked: { <T extends object>(source: T, options?: { shallow: false }): Mocked<T>; <T_1 extends object>( source: T_1, options: { shallow: true } ): MockedShallow<T_1>;};
variable spyOn
const spyOn: { < T extends object, K_2 extends Exclude< keyof T, | keyof { [K in keyof T as Required<T>[K] extends ClassLike ? K : never]: T[K]; } | keyof { [K_1 in keyof T as Required<T>[K_1] extends FunctionLike ? K_1 : never]: T[K_1]; } >, V extends Required<T>[K_2], A extends 'get' | 'set' >( object: T, methodKey: K_2, accessType: A ): A extends 'get' ? SpiedGetter<V> : A extends 'set' ? SpiedSetter<V> : never; < T_1 extends object, K_5 extends | keyof { [K_3 in keyof T_1 as Required<T_1>[K_3] extends ClassLike ? K_3 : never]: T_1[K_3]; } | keyof { [K_4 in keyof T_1 as Required<T_1>[K_4] extends FunctionLike ? K_4 : never]: T_1[K_4]; }, V_1 extends Required<T_1>[K_5] >( object: T_1, methodKey: K_5 ): V_1 extends FunctionLike | ClassLike ? Spied<V_1> : never;};
Functions
function fn
fn: <T extends FunctionLike = UnknownFunction>(implementation?: T) => Mock<T>;
function replaceProperty
replaceProperty: <T extends object, K extends keyof T>( object: T, propertyKey: K, value: T[K]) => Replaced<T[K]>;
Classes
class ModuleMocker
class ModuleMocker {}
constructor
constructor(global: typeof globalThis);
Parameter global
Global object of the test environment, used to create mocks
See Also
README.md
method clearAllMocks
clearAllMocks: () => void;
method fn
fn: <T extends FunctionLike = UnknownFunction>(implementation?: T) => Mock<T>;
method generateFromMetadata
generateFromMetadata: <T>(metadata: MockMetadata<T>) => Mocked<T>;
Parameter metadata
Metadata for the mock in the schema returned by the getMetadata method of this module.
See Also
README.md
method getMetadata
getMetadata: <T = unknown>( component: T, _refs?: Map<T, number>) => MockMetadata<T> | null;
Parameter component
The component for which to retrieve metadata.
See Also
README.md
method isMockFunction
isMockFunction: { <T extends FunctionLike = UnknownFunction>( fn: MockInstance<T> ): fn is MockInstance<T>; <P extends unknown[], R>(fn: (...args: P) => R): fn is Mock< (...args: P) => R >; (fn: unknown): fn is Mock<UnknownFunction>;};
method mocked
mocked: { <T extends object>(source: T, options?: { shallow: false }): Mocked<T>; <T extends object>(source: T, options: { shallow: true }): MockedShallow<T>;};
method replaceProperty
replaceProperty: <T extends object, K extends keyof T>( object: T, propertyKey: K, value: T[K]) => Replaced<T[K]>;
method resetAllMocks
resetAllMocks: () => void;
method restoreAllMocks
restoreAllMocks: () => void;
method spyOn
spyOn: { < T extends object, K extends Exclude< keyof T, | keyof { [K in keyof T as Required<T>[K] extends ClassLike ? K : never]: T[K]; } | keyof { [K in keyof T as Required<T>[K] extends FunctionLike ? K : never]: T[K]; } >, V extends Required<T>[K], A extends 'get' | 'set' >( object: T, methodKey: K, accessType: A ): A extends 'get' ? SpiedGetter<V> : A extends 'set' ? SpiedSetter<V> : never; < T extends object, K extends | keyof { [K in keyof T as Required<T>[K] extends ClassLike ? K : never]: T[K]; } | keyof { [K in keyof T as Required<T>[K] extends FunctionLike ? K : never]: T[K]; }, V extends Required<T>[K] >( object: T, methodKey: K ): V extends FunctionLike | ClassLike ? Spied<V> : never;};
Interfaces
interface Mock
interface Mock<T extends FunctionLike = UnknownFunction> extends Function, MockInstance<T> {}
All what the internal typings need is to be sure that we have any-function.
FunctionLike
type ensures that and helps to constrain the type as well. The default ofUnknownFunction
makes sure thatany
s do not leak to the user side. For instance, callingfn()
without implementation will return a mock of(...args: Array<unknown>) => unknown
type. If implementation is provided, its typings are inferred correctly.
construct signature
new (...args: Parameters<T>): ReturnType<T>;
call signature
(...args: Parameters<T>): ReturnType<T>;
interface MockInstance
interface MockInstance<T extends FunctionLike = UnknownFunction> {}
property mock
mock: MockFunctionState<T>;
method getMockImplementation
getMockImplementation: () => T | undefined;
method getMockName
getMockName: () => string;
method mockClear
mockClear: () => this;
method mockImplementation
mockImplementation: (fn: T) => this;
method mockImplementationOnce
mockImplementationOnce: (fn: T) => this;
method mockName
mockName: (name: string) => this;
method mockRejectedValue
mockRejectedValue: (value: RejectType<T>) => this;
method mockRejectedValueOnce
mockRejectedValueOnce: (value: RejectType<T>) => this;
method mockReset
mockReset: () => this;
method mockResolvedValue
mockResolvedValue: (value: ResolveType<T>) => this;
method mockResolvedValueOnce
mockResolvedValueOnce: (value: ResolveType<T>) => this;
method mockRestore
mockRestore: () => void;
method mockReturnThis
mockReturnThis: () => this;
method mockReturnValue
mockReturnValue: (value: ReturnType<T>) => this;
method mockReturnValueOnce
mockReturnValueOnce: (value: ReturnType<T>) => this;
method withImplementation
withImplementation: { (fn: T, callback: () => Promise<unknown>): Promise<void>; (fn: T, callback: () => void): void;};
interface Replaced
interface Replaced<T = unknown> {}
method replaceValue
replaceValue: (value: T) => this;
Change the value of the property.
method restore
restore: () => void;
Restore property to its original value known at the time of mocking.
interface SpyInstance
interface SpyInstance<T extends FunctionLike = UnknownFunction> extends MockInstance<T> {}
Type Aliases
type ClassLike
type ClassLike = { new (...args: any): any;};
Copyright (c) Meta Platforms, Inc. and affiliates.
This source code is licensed under the MIT license found in the LICENSE file in the root directory of this source tree.
type ConstructorLikeKeys
type ConstructorLikeKeys<T> = keyof { [K in keyof T as Required<T>[K] extends ClassLike ? K : never]: T[K];};
type FunctionLike
type FunctionLike = (...args: any) => any;
type MethodLikeKeys
type MethodLikeKeys<T> = keyof { [K in keyof T as Required<T>[K] extends FunctionLike ? K : never]: T[K];};
type Mocked
type Mocked<T> = T extends ClassLike ? MockedClass<T> : T extends FunctionLike ? MockedFunction<T> : T extends object ? MockedObject<T> : T;
type MockedClass
type MockedClass<T extends ClassLike> = MockInstance< (...args: ConstructorParameters<T>) => Mocked<InstanceType<T>>> & MockedObject<T>;
type MockedFunction
type MockedFunction<T extends FunctionLike> = MockInstance<T> & MockedObject<T>;
type MockedObject
type MockedObject<T extends object> = { [K in keyof T]: T[K] extends ClassLike ? MockedClass<T[K]> : T[K] extends FunctionLike ? MockedFunction<T[K]> : T[K] extends object ? MockedObject<T[K]> : T[K];} & T;
type MockedShallow
type MockedShallow<T> = T extends ClassLike ? MockedClass<T> : T extends FunctionLike ? MockedFunctionShallow<T> : T extends object ? MockedObjectShallow<T> : T;
type MockFunctionMetadata
type MockFunctionMetadata< T = unknown, MetadataType = MockMetadataType> = MockMetadata<T, MetadataType>;
type MockFunctionMetadataType
type MockFunctionMetadataType = MockMetadataType;
type MockMetadata
type MockMetadata<T, MetadataType = MockMetadataType> = { ref?: number; members?: Record<string, MockMetadata<T>>; mockImpl?: T; name?: string; refID?: number; type?: MetadataType; value?: T; length?: number;};
type MockMetadataType
type MockMetadataType = | 'object' | 'array' | 'regexp' | 'function' | 'constant' | 'collection' | 'null' | 'undefined';
type PropertyLikeKeys
type PropertyLikeKeys<T> = Exclude< keyof T, ConstructorLikeKeys<T> | MethodLikeKeys<T>>;
type Spied
type Spied<T extends ClassLike | FunctionLike> = T extends ClassLike ? SpiedClass<T> : T extends FunctionLike ? SpiedFunction<T> : never;
type SpiedClass
type SpiedClass<T extends ClassLike = UnknownClass> = MockInstance< (...args: ConstructorParameters<T>) => InstanceType<T>>;
type SpiedFunction
type SpiedFunction<T extends FunctionLike = UnknownFunction> = MockInstance< (...args: Parameters<T>) => ReturnType<T>>;
type SpiedGetter
type SpiedGetter<T> = MockInstance<() => T>;
type SpiedSetter
type SpiedSetter<T> = MockInstance<(arg: T) => void>;
type UnknownClass
type UnknownClass = { new (...args: Array<unknown>): unknown;};
type UnknownFunction
type UnknownFunction = (...args: Array<unknown>) => unknown;
Package Files (1)
Dependencies (3)
Dev Dependencies (2)
Peer Dependencies (0)
No peer dependencies.
Badge
To add a badge like this oneto your package's README, use the codes available below.
You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/jest-mock
.
- Markdown[![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/jest-mock)
- HTML<a href="https://www.jsdocs.io/package/jest-mock"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>
- Updated .
Package analyzed in 1629 ms. - Missing or incorrect documentation? Open an issue for this package.