jest-mock
Advanced tools
Comparing version 27.5.1 to 28.0.0-alpha.0
@@ -7,85 +7,149 @@ /** | ||
*/ | ||
export declare type MockFunctionMetadataType = 'object' | 'array' | 'regexp' | 'function' | 'constant' | 'collection' | 'null' | 'undefined'; | ||
export declare type MockFunctionMetadata<T, Y extends Array<unknown>, Type = MockFunctionMetadataType> = { | ||
ref?: number; | ||
members?: Record<string, MockFunctionMetadata<T, Y>>; | ||
mockImpl?: (...args: Y) => T; | ||
name?: string; | ||
refID?: number; | ||
type?: Type; | ||
value?: T; | ||
length?: number; | ||
}; | ||
export declare type MockableFunction = (...args: Array<any>) => any; | ||
export declare type ArgsType<T> = T extends (...args: infer A) => any | ||
? A | ||
: never; | ||
export declare type ArgumentsOf<T> = T extends (...args: infer A) => any | ||
? A | ||
: never; | ||
export declare interface Constructable { | ||
new (...args: Array<any>): any; | ||
} | ||
export declare type ConstructorArgumentsOf<T> = T extends new ( | ||
...args: infer A | ||
) => any | ||
? A | ||
: never; | ||
export declare const fn: <T, Y extends unknown[]>( | ||
implementation?: ((...args: Y) => T) | undefined, | ||
) => Mock<T, Y>; | ||
declare type FunctionPropertyNames<T> = { | ||
[K in keyof T]: T[K] extends (...args: Array<any>) => any ? K : never; | ||
}[keyof T] & | ||
string; | ||
export declare type MaybeMocked<T> = T extends MockableFunction | ||
? MockedFunction<T> | ||
: T extends object | ||
? MockedObject<T> | ||
: T; | ||
export declare type MaybeMockedConstructor<T> = T extends new ( | ||
...args: Array<any> | ||
) => infer R | ||
? MockInstance<R, ConstructorArgumentsOf<T>> | ||
: T; | ||
export declare type MaybeMockedDeep<T> = T extends MockableFunction | ||
? MockedFunctionDeep<T> | ||
: T extends object | ||
? MockedObjectDeep<T> | ||
: T; | ||
export declare type MethodKeysOf<T> = { | ||
[K in keyof T]: T[K] extends MockableFunction ? K : never; | ||
[K in keyof T]: T[K] extends MockableFunction ? K : never; | ||
}[keyof T]; | ||
export declare type PropertyKeysOf<T> = { | ||
[K in keyof T]: T[K] extends MockableFunction ? never : K; | ||
}[keyof T]; | ||
export declare type ArgumentsOf<T> = T extends (...args: infer A) => any ? A : never; | ||
export declare type ConstructorArgumentsOf<T> = T extends new (...args: infer A) => any ? A : never; | ||
export declare type MaybeMockedConstructor<T> = T extends new (...args: Array<any>) => infer R ? MockInstance<R, ConstructorArgumentsOf<T>> : T; | ||
export declare type MockedFunction<T extends MockableFunction> = MockWithArgs<T> & { | ||
export declare interface Mock<T, Y extends Array<unknown> = Array<unknown>> | ||
extends Function, | ||
MockInstance<T, Y> { | ||
new (...args: Y): T; | ||
(...args: Y): T; | ||
} | ||
export declare type MockableFunction = (...args: Array<any>) => any; | ||
export declare type Mocked<T> = { | ||
[P in keyof T]: T[P] extends (...args: Array<any>) => any | ||
? MockInstance<ReturnType<T[P]>, ArgsType<T[P]>> | ||
: T[P] extends Constructable | ||
? MockedClass<T[P]> | ||
: T[P]; | ||
} & T; | ||
export declare const mocked: { | ||
<T>(item: T, deep?: false | undefined): MaybeMocked<T>; | ||
<T_1>(item: T_1, deep: true): MaybeMockedDeep<T_1>; | ||
}; | ||
export declare type MockedClass<T extends Constructable> = MockInstance< | ||
InstanceType<T>, | ||
T extends new (...args: infer P) => any ? P : never | ||
> & { | ||
prototype: T extends { | ||
prototype: any; | ||
} | ||
? Mocked<T['prototype']> | ||
: never; | ||
} & T; | ||
export declare type MockedFunction<T extends MockableFunction> = | ||
MockWithArgs<T> & { | ||
[K in keyof T]: T[K]; | ||
}; | ||
export declare type MockedFunctionDeep<T extends MockableFunction> = MockWithArgs<T> & MockedObjectDeep<T>; | ||
}; | ||
export declare type MockedFunctionDeep<T extends MockableFunction> = | ||
MockWithArgs<T> & MockedObjectDeep<T>; | ||
export declare type MockedObject<T> = MaybeMockedConstructor<T> & { | ||
[K in MethodKeysOf<T>]: T[K] extends MockableFunction ? MockedFunction<T[K]> : T[K]; | ||
[K in MethodKeysOf<T>]: T[K] extends MockableFunction | ||
? MockedFunction<T[K]> | ||
: T[K]; | ||
} & { | ||
[K in PropertyKeysOf<T>]: T[K]; | ||
[K in PropertyKeysOf<T>]: T[K]; | ||
}; | ||
export declare type MockedObjectDeep<T> = MaybeMockedConstructor<T> & { | ||
[K in MethodKeysOf<T>]: T[K] extends MockableFunction ? MockedFunctionDeep<T[K]> : T[K]; | ||
[K in MethodKeysOf<T>]: T[K] extends MockableFunction | ||
? MockedFunctionDeep<T[K]> | ||
: T[K]; | ||
} & { | ||
[K in PropertyKeysOf<T>]: MaybeMockedDeep<T[K]>; | ||
[K in PropertyKeysOf<T>]: MaybeMockedDeep<T[K]>; | ||
}; | ||
export declare type MaybeMockedDeep<T> = T extends MockableFunction ? MockedFunctionDeep<T> : T extends object ? MockedObjectDeep<T> : T; | ||
export declare type MaybeMocked<T> = T extends MockableFunction ? MockedFunction<T> : T extends object ? MockedObject<T> : T; | ||
export declare type ArgsType<T> = T extends (...args: infer A) => any ? A : never; | ||
export declare type Mocked<T> = { | ||
[P in keyof T]: T[P] extends (...args: Array<any>) => any ? MockInstance<ReturnType<T[P]>, ArgsType<T[P]>> : T[P] extends Constructable ? MockedClass<T[P]> : T[P]; | ||
} & T; | ||
export declare type MockedClass<T extends Constructable> = MockInstance<InstanceType<T>, T extends new (...args: infer P) => any ? P : never> & { | ||
prototype: T extends { | ||
prototype: any; | ||
} ? Mocked<T['prototype']> : never; | ||
} & T; | ||
export interface Constructable { | ||
new (...args: Array<any>): any; | ||
} | ||
export interface MockWithArgs<T extends MockableFunction> extends MockInstance<ReturnType<T>, ArgumentsOf<T>> { | ||
new (...args: ConstructorArgumentsOf<T>): T; | ||
(...args: ArgumentsOf<T>): ReturnType<T>; | ||
} | ||
export interface Mock<T, Y extends Array<unknown> = Array<unknown>> extends Function, MockInstance<T, Y> { | ||
new (...args: Y): T; | ||
(...args: Y): T; | ||
} | ||
export interface SpyInstance<T, Y extends Array<unknown>> extends MockInstance<T, Y> { | ||
} | ||
export interface MockInstance<T, Y extends Array<unknown>> { | ||
_isMockFunction: true; | ||
_protoImpl: Function; | ||
getMockName(): string; | ||
getMockImplementation(): Function | undefined; | ||
mock: MockFunctionState<T, Y>; | ||
mockClear(): this; | ||
mockReset(): this; | ||
mockRestore(): void; | ||
mockImplementation(fn: (...args: Y) => T): this; | ||
mockImplementation(fn: () => Promise<T>): this; | ||
mockImplementationOnce(fn: (...args: Y) => T): this; | ||
mockImplementationOnce(fn: () => Promise<T>): this; | ||
mockName(name: string): this; | ||
mockReturnThis(): this; | ||
mockReturnValue(value: T): this; | ||
mockReturnValueOnce(value: T): this; | ||
mockResolvedValue(value: Unpromisify<T>): this; | ||
mockResolvedValueOnce(value: Unpromisify<T>): this; | ||
mockRejectedValue(value: unknown): this; | ||
mockRejectedValueOnce(value: unknown): this; | ||
} | ||
declare type Unpromisify<T> = T extends Promise<infer R> ? R : never; | ||
export declare type MockFunctionMetadata< | ||
T, | ||
Y extends Array<unknown>, | ||
Type = MockFunctionMetadataType, | ||
> = { | ||
ref?: number; | ||
members?: Record<string, MockFunctionMetadata<T, Y>>; | ||
mockImpl?: (...args: Y) => T; | ||
name?: string; | ||
refID?: number; | ||
type?: Type; | ||
value?: T; | ||
length?: number; | ||
}; | ||
export declare type MockFunctionMetadataType = | ||
| 'object' | ||
| 'array' | ||
| 'regexp' | ||
| 'function' | ||
| 'constant' | ||
| 'collection' | ||
| 'null' | ||
| 'undefined'; | ||
/** | ||
* Represents the result of a single call to a mock function. | ||
*/ | ||
declare type MockFunctionResult = { | ||
/** | ||
* Indicates how the call completed. | ||
*/ | ||
type: MockFunctionResultType; | ||
/** | ||
* The value that was either thrown or returned by the function. | ||
* Undefined when type === 'incomplete'. | ||
*/ | ||
value: unknown; | ||
}; | ||
/** | ||
* Possible types of a MockFunctionResult. | ||
@@ -99,89 +163,145 @@ * 'return': The call completed by returning normally. | ||
declare type MockFunctionResultType = 'return' | 'throw' | 'incomplete'; | ||
/** | ||
* Represents the result of a single call to a mock function. | ||
*/ | ||
declare type MockFunctionResult = { | ||
/** | ||
* Indicates how the call completed. | ||
*/ | ||
type: MockFunctionResultType; | ||
/** | ||
* The value that was either thrown or returned by the function. | ||
* Undefined when type === 'incomplete'. | ||
*/ | ||
value: unknown; | ||
}; | ||
declare type MockFunctionState<T, Y extends Array<unknown>> = { | ||
calls: Array<Y>; | ||
instances: Array<T>; | ||
invocationCallOrder: Array<number>; | ||
/** | ||
* Getter for retrieving the last call arguments | ||
*/ | ||
lastCall?: Y; | ||
/** | ||
* List of results of calls to the mock function. | ||
*/ | ||
results: Array<MockFunctionResult>; | ||
calls: Array<Y>; | ||
instances: Array<T>; | ||
invocationCallOrder: Array<number>; | ||
/** | ||
* Getter for retrieving the last call arguments | ||
*/ | ||
lastCall?: Y; | ||
/** | ||
* List of results of calls to the mock function. | ||
*/ | ||
results: Array<MockFunctionResult>; | ||
}; | ||
declare type NonFunctionPropertyNames<T> = { | ||
[K in keyof T]: T[K] extends (...args: Array<any>) => any ? never : K; | ||
}[keyof T] & string; | ||
declare type FunctionPropertyNames<T> = { | ||
[K in keyof T]: T[K] extends (...args: Array<any>) => any ? K : never; | ||
}[keyof T] & string; | ||
export declare interface MockInstance<T, Y extends Array<unknown>> { | ||
_isMockFunction: true; | ||
_protoImpl: Function; | ||
getMockName(): string; | ||
getMockImplementation(): Function | undefined; | ||
mock: MockFunctionState<T, Y>; | ||
mockClear(): this; | ||
mockReset(): this; | ||
mockRestore(): void; | ||
mockImplementation(fn: (...args: Y) => T): this; | ||
mockImplementation(fn: () => Promise<T>): this; | ||
mockImplementationOnce(fn: (...args: Y) => T): this; | ||
mockImplementationOnce(fn: () => Promise<T>): this; | ||
mockName(name: string): this; | ||
mockReturnThis(): this; | ||
mockReturnValue(value: T): this; | ||
mockReturnValueOnce(value: T): this; | ||
mockResolvedValue(value: Unpromisify<T>): this; | ||
mockResolvedValueOnce(value: Unpromisify<T>): this; | ||
mockRejectedValue(value: unknown): this; | ||
mockRejectedValueOnce(value: unknown): this; | ||
} | ||
export declare interface MockWithArgs<T extends MockableFunction> | ||
extends MockInstance<ReturnType<T>, ArgumentsOf<T>> { | ||
new (...args: ConstructorArgumentsOf<T>): T; | ||
(...args: ArgumentsOf<T>): ReturnType<T>; | ||
} | ||
export declare class ModuleMocker { | ||
private _environmentGlobal; | ||
private _mockState; | ||
private _mockConfigRegistry; | ||
private _spyState; | ||
private _invocationCallCounter; | ||
/** | ||
* @see README.md | ||
* @param global Global object of the test environment, used to create | ||
* mocks | ||
*/ | ||
constructor(global: typeof globalThis); | ||
private _getSlots; | ||
private _ensureMockConfig; | ||
private _ensureMockState; | ||
private _defaultMockConfig; | ||
private _defaultMockState; | ||
private _makeComponent; | ||
private _createMockFunction; | ||
private _generateMock; | ||
/** | ||
* @see README.md | ||
* @param _metadata Metadata for the mock in the schema returned by the | ||
* getMetadata method of this module. | ||
*/ | ||
generateFromMetadata<T, Y extends Array<unknown>>(_metadata: MockFunctionMetadata<T, Y>): Mock<T, Y>; | ||
/** | ||
* @see README.md | ||
* @param component The component for which to retrieve metadata. | ||
*/ | ||
getMetadata<T, Y extends Array<unknown>>(component: T, _refs?: Map<T, number>): MockFunctionMetadata<T, Y> | null; | ||
isMockFunction<T>(fn: unknown): fn is Mock<T>; | ||
fn<T, Y extends Array<unknown>>(implementation?: (...args: Y) => T): Mock<T, Y>; | ||
spyOn<T extends {}, M extends NonFunctionPropertyNames<T>>(object: T, methodName: M, accessType: 'get'): SpyInstance<T[M], []>; | ||
spyOn<T extends {}, M extends NonFunctionPropertyNames<T>>(object: T, methodName: M, accessType: 'set'): SpyInstance<void, [T[M]]>; | ||
spyOn<T extends {}, M extends FunctionPropertyNames<T>>(object: T, methodName: M): T[M] extends (...args: Array<any>) => any ? SpyInstance<ReturnType<T[M]>, Parameters<T[M]>> : never; | ||
private _spyOnProperty; | ||
clearAllMocks(): void; | ||
resetAllMocks(): void; | ||
restoreAllMocks(): void; | ||
private _typeOf; | ||
mocked<T>(item: T, deep?: false): MaybeMocked<T>; | ||
mocked<T>(item: T, deep: true): MaybeMockedDeep<T>; | ||
private _environmentGlobal; | ||
private _mockState; | ||
private _mockConfigRegistry; | ||
private _spyState; | ||
private _invocationCallCounter; | ||
/** | ||
* @see README.md | ||
* @param global Global object of the test environment, used to create | ||
* mocks | ||
*/ | ||
constructor(global: typeof globalThis); | ||
private _getSlots; | ||
private _ensureMockConfig; | ||
private _ensureMockState; | ||
private _defaultMockConfig; | ||
private _defaultMockState; | ||
private _makeComponent; | ||
private _createMockFunction; | ||
private _generateMock; | ||
/** | ||
* @see README.md | ||
* @param _metadata Metadata for the mock in the schema returned by the | ||
* getMetadata method of this module. | ||
*/ | ||
generateFromMetadata<T, Y extends Array<unknown>>( | ||
_metadata: MockFunctionMetadata<T, Y>, | ||
): Mock<T, Y>; | ||
/** | ||
* @see README.md | ||
* @param component The component for which to retrieve metadata. | ||
*/ | ||
getMetadata<T, Y extends Array<unknown>>( | ||
component: T, | ||
_refs?: Map<T, number>, | ||
): MockFunctionMetadata<T, Y> | null; | ||
isMockFunction<T>(fn: unknown): fn is Mock<T>; | ||
fn<T, Y extends Array<unknown>>( | ||
implementation?: (...args: Y) => T, | ||
): Mock<T, Y>; | ||
spyOn<T extends {}, M extends NonFunctionPropertyNames<T>>( | ||
object: T, | ||
methodName: M, | ||
accessType: 'get', | ||
): SpyInstance<T[M], []>; | ||
spyOn<T extends {}, M extends NonFunctionPropertyNames<T>>( | ||
object: T, | ||
methodName: M, | ||
accessType: 'set', | ||
): SpyInstance<void, [T[M]]>; | ||
spyOn<T extends {}, M extends FunctionPropertyNames<T>>( | ||
object: T, | ||
methodName: M, | ||
): T[M] extends (...args: Array<any>) => any | ||
? SpyInstance<ReturnType<T[M]>, Parameters<T[M]>> | ||
: never; | ||
private _spyOnProperty; | ||
clearAllMocks(): void; | ||
resetAllMocks(): void; | ||
restoreAllMocks(): void; | ||
private _typeOf; | ||
mocked<T>(item: T, deep?: false): MaybeMocked<T>; | ||
mocked<T>(item: T, deep: true): MaybeMockedDeep<T>; | ||
} | ||
export declare const fn: <T, Y extends unknown[]>(implementation?: ((...args: Y) => T) | undefined) => Mock<T, Y>; | ||
export declare const spyOn: { | ||
<T extends {}, M extends NonFunctionPropertyNames<T>>(object: T, methodName: M, accessType: 'get'): SpyInstance<T[M], []>; | ||
<T_2 extends {}, M_2 extends NonFunctionPropertyNames<T_2>>(object: T_2, methodName: M_2, accessType: 'set'): SpyInstance<void, [T_2[M_2]]>; | ||
<T_4 extends {}, M_4 extends FunctionPropertyNames<T_4>>(object: T_4, methodName: M_4): T_4[M_4] extends (...args: Array<any>) => any ? SpyInstance<ReturnType<T_4[M_4]>, Parameters<T_4[M_4]>> : never; | ||
declare type NonFunctionPropertyNames<T> = { | ||
[K in keyof T]: T[K] extends (...args: Array<any>) => any ? never : K; | ||
}[keyof T] & | ||
string; | ||
export declare type PropertyKeysOf<T> = { | ||
[K in keyof T]: T[K] extends MockableFunction ? never : K; | ||
}[keyof T]; | ||
export declare interface SpyInstance<T, Y extends Array<unknown>> | ||
extends MockInstance<T, Y> {} | ||
declare const spyOn_2: { | ||
<T extends {}, M extends NonFunctionPropertyNames<T>>( | ||
object: T, | ||
methodName: M, | ||
accessType: 'get', | ||
): SpyInstance<T[M], []>; | ||
<T_1 extends {}, M_1 extends NonFunctionPropertyNames<T_1>>( | ||
object: T_1, | ||
methodName: M_1, | ||
accessType: 'set', | ||
): SpyInstance<void, [T_1[M_1]]>; | ||
<T_2 extends {}, M_2 extends FunctionPropertyNames<T_2>>( | ||
object: T_2, | ||
methodName: M_2, | ||
): T_2[M_2] extends (...args: Array<any>) => any | ||
? SpyInstance<ReturnType<T_2[M_2]>, Parameters<T_2[M_2]>> | ||
: never; | ||
}; | ||
export declare const mocked: { | ||
<T>(item: T, deep?: false | undefined): MaybeMocked<T>; | ||
<T_2>(item: T_2, deep: true): MaybeMockedDeep<T_2>; | ||
}; | ||
export {spyOn_2 as spyOn}; | ||
declare type Unpromisify<T> = T extends Promise<infer R> ? R : never; | ||
export {}; |
@@ -8,16 +8,2 @@ 'use strict'; | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
/** | ||
@@ -250,2 +236,7 @@ * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
class ModuleMocker { | ||
_environmentGlobal; | ||
_mockState; | ||
_mockConfigRegistry; | ||
_spyState; | ||
_invocationCallCounter; | ||
/** | ||
@@ -256,13 +247,4 @@ * @see README.md | ||
*/ | ||
constructor(global) { | ||
_defineProperty(this, '_environmentGlobal', void 0); | ||
_defineProperty(this, '_mockState', void 0); | ||
_defineProperty(this, '_mockConfigRegistry', void 0); | ||
_defineProperty(this, '_spyState', void 0); | ||
_defineProperty(this, '_invocationCallCounter', void 0); | ||
this._environmentGlobal = global; | ||
@@ -269,0 +251,0 @@ this._mockState = new WeakMap(); |
{ | ||
"name": "jest-mock", | ||
"version": "27.5.1", | ||
"version": "28.0.0-alpha.0", | ||
"repository": { | ||
@@ -10,6 +10,6 @@ "type": "git", | ||
"engines": { | ||
"node": "^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0" | ||
"node": "^12.13.0 || ^14.15.0 || ^16.13.0 || >=17.0.0" | ||
}, | ||
"dependencies": { | ||
"@jest/types": "^27.5.1", | ||
"@jest/types": "^28.0.0-alpha.0", | ||
"@types/node": "*" | ||
@@ -30,3 +30,3 @@ }, | ||
}, | ||
"gitHead": "67c1aa20c5fec31366d733e901fee2b981cb1850" | ||
"gitHead": "89275b08977065d98e42ad71fcf223f4ad169f09" | ||
} |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
1045
38809
2
+ Added@jest/schemas@28.1.3(transitive)
+ Added@jest/types@28.1.3(transitive)
+ Added@sinclair/typebox@0.24.51(transitive)
+ Added@types/yargs@17.0.32(transitive)
- Removed@jest/types@27.5.1(transitive)
- Removed@types/yargs@16.0.9(transitive)
Updated@jest/types@^28.0.0-alpha.0