Socket
Socket
Sign inDemoInstall

@vitest/spy

Package Overview
Dependencies
Maintainers
0
Versions
95
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@vitest/spy - npm Package Compare versions

Comparing version 2.0.0-beta.11 to 2.0.0-beta.12

97

dist/index.d.ts

@@ -29,3 +29,3 @@ interface MockResultReturn<T> {

type MockSettledResult<T> = MockSettledResultFulfilled<T> | MockSettledResultRejected;
interface MockContext<TArgs, TReturns> {
interface MockContext<T extends Procedure> {
/**

@@ -45,8 +45,12 @@ * This is an array containing all arguments for each call. One item of the array is the arguments of that call.

*/
calls: TArgs[];
calls: Parameters<T>[];
/**
* This is an array containing all instances that were instantiated when mock was called with a `new` keyword. Note that this is an actual context (`this`) of the function, not a return value.
*/
instances: TReturns[];
instances: ReturnType<T>[];
/**
* An array of `this` values that were used during each call to the mock function.
*/
contexts: ThisParameterType<T>[];
/**
* The order of mock's execution. This returns an array of numbers which are shared between all defined mocks.

@@ -94,3 +98,3 @@ *

*/
results: MockResult<TReturns>[];
results: MockResult<ReturnType<T>>[];
/**

@@ -123,7 +127,7 @@ * An array containing all values that were `resolved` or `rejected` from the function.

*/
settledResults: MockSettledResult<Awaited<TReturns>>[];
settledResults: MockSettledResult<Awaited<ReturnType<T>>>[];
/**
* This contains the arguments of the last call. If spy wasn't called, will return `undefined`.
*/
lastCall: TArgs | undefined;
lastCall: Parameters<T> | undefined;
}

@@ -140,20 +144,15 @@ type Procedure = (...args: any[]) => any;

}[keyof T] & (string | symbol);
/**
* @deprecated Use MockInstance<A, R> instead
*/
interface SpyInstance<TArgs extends any[] = any[], TReturns = any> extends MockInstance<TArgs, TReturns> {
}
interface MockInstance<TArgs extends any[] = any[], TReturns = any> {
interface MockInstance<T extends Procedure = Procedure> {
/**
* Use it to return the name given to mock with method `.mockName(name)`.
*/
getMockName: () => string;
getMockName(): string;
/**
* Sets internal mock name. Useful to see the name of the mock if an assertion fails.
*/
mockName: (n: string) => this;
mockName(n: string): this;
/**
* Current context of the mock. It stores information about all invocation calls, instances, and results.
*/
mock: MockContext<TArgs, TReturns>;
mock: MockContext<T>;
/**

@@ -164,3 +163,3 @@ * Clears all information about every call. After calling it, all properties on `.mock` will return an empty state. This method does not reset implementations.

*/
mockClear: () => this;
mockClear(): this;
/**

@@ -171,3 +170,3 @@ * Does what `mockClear` does and makes inner implementation an empty function (returning `undefined` when invoked). This also resets all "once" implementations.

*/
mockReset: () => this;
mockReset(): this;
/**

@@ -178,3 +177,3 @@ * Does what `mockReset` does and restores inner implementation to the original function.

*/
mockRestore: () => void;
mockRestore(): void;
/**

@@ -187,3 +186,3 @@ * Returns current mock implementation if there is one.

*/
getMockImplementation: () => ((...args: TArgs) => TReturns) | undefined;
getMockImplementation(): T | undefined;
/**

@@ -195,3 +194,3 @@ * Accepts a function that will be used as an implementation of the mock.

*/
mockImplementation: (fn: (...args: TArgs) => TReturns) => this;
mockImplementation(fn: T): this;
/**

@@ -204,3 +203,3 @@ * Accepts a function that will be used as a mock implementation during the next call. Can be chained so that multiple function calls produce different results.

*/
mockImplementationOnce: (fn: (...args: TArgs) => TReturns) => this;
mockImplementationOnce(fn: T): this;
/**

@@ -217,11 +216,11 @@ * Overrides the original mock implementation temporarily while the callback is being executed.

*/
withImplementation: <T>(fn: (...args: TArgs) => TReturns, cb: () => T) => T extends Promise<unknown> ? Promise<this> : this;
withImplementation<T2>(fn: T, cb: () => T2): T2 extends Promise<unknown> ? Promise<this> : this;
/**
* Use this if you need to return `this` context from the method without invoking actual implementation.
*/
mockReturnThis: () => this;
mockReturnThis(): this;
/**
* Accepts a value that will be returned whenever the mock function is called.
*/
mockReturnValue: (obj: TReturns) => this;
mockReturnValue(obj: ReturnType<T>): this;
/**

@@ -241,3 +240,3 @@ * Accepts a value that will be returned during the next function call. If chained, every consecutive call will return the specified value.

*/
mockReturnValueOnce: (obj: TReturns) => this;
mockReturnValueOnce(obj: ReturnType<T>): this;
/**

@@ -249,3 +248,3 @@ * Accepts a value that will be resolved when async function is called.

*/
mockResolvedValue: (obj: Awaited<TReturns>) => this;
mockResolvedValue(obj: Awaited<ReturnType<T>>): this;
/**

@@ -263,3 +262,3 @@ * Accepts a value that will be resolved during the next function call. If chained, every consecutive call will resolve specified value.

*/
mockResolvedValueOnce: (obj: Awaited<TReturns>) => this;
mockResolvedValueOnce(obj: Awaited<ReturnType<T>>): this;
/**

@@ -271,3 +270,3 @@ * Accepts an error that will be rejected when async function is called.

*/
mockRejectedValue: (obj: any) => this;
mockRejectedValue(obj: any): this;
/**

@@ -284,21 +283,22 @@ * Accepts a value that will be rejected during the next function call. If chained, every consecutive call will reject specified value.

*/
mockRejectedValueOnce: (obj: any) => this;
mockRejectedValueOnce(obj: any): this;
}
interface Mock<TArgs extends any[] = any, TReturns = any> extends MockInstance<TArgs, TReturns> {
new (...args: TArgs): TReturns;
(...args: TArgs): TReturns;
interface Mock<T extends Procedure = Procedure> extends MockInstance<T> {
new (...args: Parameters<T>): ReturnType<T>;
(...args: Parameters<T>): ReturnType<T>;
}
interface PartialMock<TArgs extends any[] = any, TReturns = any> extends MockInstance<TArgs, TReturns extends Promise<Awaited<TReturns>> ? Promise<Partial<Awaited<TReturns>>> : Partial<TReturns>> {
new (...args: TArgs): TReturns;
(...args: TArgs): TReturns;
type PartialMaybePromise<T> = T extends Promise<Awaited<T>> ? Promise<Partial<Awaited<T>>> : Partial<T>;
interface PartialMock<T extends Procedure = Procedure> extends MockInstance<(...args: Parameters<T>) => PartialMaybePromise<ReturnType<T>>> {
new (...args: Parameters<T>): ReturnType<T>;
(...args: Parameters<T>): ReturnType<T>;
}
type MaybeMockedConstructor<T> = T extends new (...args: Array<any>) => infer R ? Mock<ConstructorParameters<T>, R> : T;
type MockedFunction<T extends Procedure> = Mock<Parameters<T>, ReturnType<T>> & {
type MaybeMockedConstructor<T> = T extends new (...args: Array<any>) => infer R ? Mock<(...args: ConstructorParameters<T>) => R> : T;
type MockedFunction<T extends Procedure> = Mock<T> & {
[K in keyof T]: T[K];
};
type PartiallyMockedFunction<T extends Procedure> = PartialMock<Parameters<T>, ReturnType<T>> & {
type PartiallyMockedFunction<T extends Procedure> = PartialMock<T> & {
[K in keyof T]: T[K];
};
type MockedFunctionDeep<T extends Procedure> = Mock<Parameters<T>, ReturnType<T>> & MockedObjectDeep<T>;
type PartiallyMockedFunctionDeep<T extends Procedure> = PartialMock<Parameters<T>, ReturnType<T>> & MockedObjectDeep<T>;
type MockedFunctionDeep<T extends Procedure> = Mock<T> & MockedObjectDeep<T>;
type PartiallyMockedFunctionDeep<T extends Procedure> = PartialMock<T> & MockedObjectDeep<T>;
type MockedObject<T> = MaybeMockedConstructor<T> & {

@@ -321,3 +321,3 @@ [K in Methods<T>]: T[K] extends Procedure ? MockedFunction<T[K]> : T[K];

}
type MockedClass<T extends Constructable> = MockInstance<T extends new (...args: infer P) => any ? P : never, InstanceType<T>> & {
type MockedClass<T extends Constructable> = MockInstance<(...args: ConstructorParameters<T>) => InstanceType<T>> & {
prototype: T extends {

@@ -328,14 +328,13 @@ prototype: any;

type Mocked<T> = {
[P in keyof T]: T[P] extends (...args: infer Args) => infer Returns ? MockInstance<Args, Returns> : T[P] extends Constructable ? MockedClass<T[P]> : T[P];
[P in keyof T]: T[P] extends Procedure ? MockInstance<T[P]> : T[P] extends Constructable ? MockedClass<T[P]> : T[P];
} & T;
declare const mocks: Set<MockInstance<any[], any>>;
declare const mocks: Set<MockInstance<Procedure>>;
declare function isMockFunction(fn: any): fn is MockInstance;
declare function spyOn<T, S extends Properties<Required<T>>>(obj: T, methodName: S, accessType: 'get'): MockInstance<[], T[S]>;
declare function spyOn<T, G extends Properties<Required<T>>>(obj: T, methodName: G, accessType: 'set'): MockInstance<[T[G]], void>;
declare function spyOn<T, S extends Properties<Required<T>>>(obj: T, methodName: S, accessType: 'get'): MockInstance<() => T[S]>;
declare function spyOn<T, G extends Properties<Required<T>>>(obj: T, methodName: G, accessType: 'set'): MockInstance<(arg: T[G]) => void>;
declare function spyOn<T, M extends Classes<Required<T>> | Methods<Required<T>>>(obj: T, methodName: M): Required<T>[M] extends {
new (...args: infer A): infer R;
} | ((...args: infer A) => infer R) ? MockInstance<A, R> : never;
declare function fn<TArgs extends any[] = any, R = any>(): Mock<TArgs, R>;
declare function fn<TArgs extends any[] = any[], R = any>(implementation: (...args: TArgs) => R): Mock<TArgs, R>;
} | ((...args: infer A) => infer R) ? MockInstance<(...args: A) => R> : never;
declare function fn<T extends Procedure = Procedure>(implementation?: T): Mock<T>;
export { type MaybeMocked, type MaybeMockedConstructor, type MaybeMockedDeep, type MaybePartiallyMocked, type MaybePartiallyMockedDeep, type Mock, type MockContext, type MockInstance, type MockResult, type MockSettledResult, type Mocked, type MockedClass, type MockedFunction, type MockedFunctionDeep, type MockedObject, type MockedObjectDeep, type PartialMock, type PartiallyMockedFunction, type PartiallyMockedFunctionDeep, type SpyInstance, fn, isMockFunction, mocks, spyOn };
export { type MaybeMocked, type MaybeMockedConstructor, type MaybeMockedDeep, type MaybePartiallyMocked, type MaybePartiallyMockedDeep, type Mock, type MockContext, type MockInstance, type MockResult, type MockSettledResult, type Mocked, type MockedClass, type MockedFunction, type MockedFunctionDeep, type MockedObject, type MockedObjectDeep, type PartialMock, type PartiallyMockedFunction, type PartiallyMockedFunctionDeep, fn, isMockFunction, mocks, spyOn };

@@ -21,2 +21,3 @@ import * as tinyspy from 'tinyspy';

let instances = [];
let contexts = [];
let invocations = [];

@@ -28,2 +29,5 @@ const state = tinyspy.getInternalState(spy);

},
get contexts() {
return contexts;
},
get instances() {

@@ -55,2 +59,3 @@ return instances;

instances.push(this);
contexts.push(this);
invocations.push(++callOrder);

@@ -70,2 +75,3 @@ const impl = implementationChangedTemporarily ? implementation : onceImplementations.shift() || implementation || state.getOriginal() || (() => {

instances = [];
contexts = [];
invocations = [];

@@ -133,6 +139,6 @@ return stub;

function fn(implementation) {
const enhancedSpy = enhanceSpy(
tinyspy.internalSpyOn({ spy: implementation || (() => {
}) }, "spy")
);
const enhancedSpy = enhanceSpy(tinyspy.internalSpyOn({
spy: implementation || function() {
}
}, "spy"));
if (implementation) {

@@ -139,0 +145,0 @@ enhancedSpy.mockImplementation(implementation);

{
"name": "@vitest/spy",
"type": "module",
"version": "2.0.0-beta.11",
"version": "2.0.0-beta.12",
"description": "Lightweight Jest compatible spy implementation",

@@ -6,0 +6,0 @@ "license": "MIT",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc