jest-mock
- Version 30.4.1
- Published
- 47.1 kB
- 3 dependencies
- MIT license
Install
npm i jest-mockyarn add jest-mockpnpm add jest-mockOverview
Overview not available.
Index
Variables
Functions
Classes
Interfaces
Type Aliases
- ClassLike
- ConstructorLikeKeys
- FunctionLike
- MethodLikeKeys
- Mocked
- MockedClass
- MockedFunction
- MockedFunctionShallow
- MockedObject
- MockedObjectShallow
- MockedShallow
- MockFunctionResult
- MockFunctionResultIncomplete
- MockFunctionResultReturn
- MockFunctionResultThrow
- MockFunctionState
- MockMetadata
- MockMetadataType
- PropertyLikeKeys
- RejectType
- ResolveType
- Spied
- SpiedClass
- SpiedFunction
- SpiedGetter
- SpiedSetter
- UnknownClass
- UnknownFunction
Variables
variable mocked
const mocked: { <T extends object>(source: T, options?: { shallow: false }): Mocked<T>; <T extends object>(source: T, options: { shallow: true }): MockedShallow<T>;};variable spyOn
const 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;};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 clearMocksOnScope
clearMocksOnScope: (scope: object) => void;Walks the own keys of
scopeand calls.mockClear()on each value that is a Jest mock function. Used byresetModulesto clear mocks that user code installed directly on the test environment's global object (where the mock-fn registry doesn't see them).
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.
FunctionLiketype ensures that and helps to constrain the type as well. The default ofUnknownFunctionmakes sure thatanys do not leak to the user side. For instance, callingfn()without implementation will return a mock of(...args: Array<unknown>) => unknowntype. 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> extends Disposable {}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.
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 MockedFunctionShallow
type MockedFunctionShallow<T extends FunctionLike> = MockInstance<T> & 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 MockedObjectShallow
type MockedObjectShallow<T extends object> = { [K in keyof T]: T[K] extends ClassLike ? MockedClass<T[K]> : T[K] extends FunctionLike ? MockedFunctionShallow<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 MockFunctionResult
type MockFunctionResult<T extends FunctionLike = UnknownFunction> = | MockFunctionResultIncomplete | MockFunctionResultReturn<T> | MockFunctionResultThrow;type MockFunctionResultIncomplete
type MockFunctionResultIncomplete = { type: 'incomplete'; /** * Result of a single call to a mock function that has not yet completed. * This occurs if you test the result from within the mock function itself, * or from within a function that was called by the mock. */ value: undefined;};type MockFunctionResultReturn
type MockFunctionResultReturn<T extends FunctionLike = UnknownFunction> = { type: 'return'; /** * Result of a single call to a mock function that returned. */ value: ReturnType<T>;};type MockFunctionResultThrow
type MockFunctionResultThrow = { type: 'throw'; /** * Result of a single call to a mock function that threw. */ value: unknown;};type MockFunctionState
type MockFunctionState<T extends FunctionLike = UnknownFunction> = { /** * List of the call arguments of all calls that have been made to the mock. */ calls: Array<Parameters<T>>; /** * List of all the object instances that have been instantiated from the mock. */ instances: Array<ReturnType<T>>; /** * List of all the function contexts that have been applied to calls to the mock. */ contexts: Array<ThisParameterType<T>>; /** * List of the call order indexes of the mock. Jest is indexing the order of * invocations of all mocks in a test file. The index is starting with `1`. */ invocationCallOrder: Array<number>; /** * List of the call arguments of the last call that was made to the mock. * If the function was not called, it will return `undefined`. */ lastCall?: Parameters<T>; /** * List of the results of all calls that have been made to the mock. */ results: Array<MockFunctionResult<T>>;};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 RejectType
type RejectType<T extends FunctionLike> = ReturnType<T> extends PromiseLike<any> ? unknown : never;type ResolveType
type ResolveType<T extends FunctionLike> = ReturnType<T> extends PromiseLike<infer U> ? U : never;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 (0)
No dev dependencies.
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[](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 4403 ms. - Missing or incorrect documentation? Open an issue for this package.
