Skip to content

Commit

Permalink
🤖 Merge PR DefinitelyTyped#49683 Update main Sinon API and Sandbox AP…
Browse files Browse the repository at this point in the history
…I to reflect versions >= 8.0.0 by @fatso83

* Add missing: sinon.addBehavior()

* Remove legacy sinon.spyCall()

Was removed on October 19 2019 in sinonjs/sinon@a168abe1d

* Add match and fake to Sandbox API

* Add sinon.setFormatter()

* Remove legacy sandbox api

Was removed on October 19 2019
  • Loading branch information
fatso83 authored Nov 24, 2020
1 parent 9fb6c8d commit f19234f
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 145 deletions.
157 changes: 58 additions & 99 deletions types/sinon/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ interface Document {} // tslint:disable-line no-empty-interface

declare namespace Sinon {
type MatchArguments<T> = {
[K in keyof T]: SinonMatcher
| (T[K] extends object ? MatchArguments<T[K]> : never)
| T[K];
[K in keyof T]: SinonMatcher | (T[K] extends object ? MatchArguments<T[K]> : never) | T[K];
};

interface SinonSpyCallApi<TArgs extends any[] = any[], TReturnValue = any> {
Expand Down Expand Up @@ -86,7 +84,7 @@ declare namespace Sinon {
* Uses deep comparison for objects and arrays. Use spy.returned(sinon.match.same(obj)) for strict comparison (see matchers).
* @param value
*/
returned(value: TReturnValue|SinonMatcher): boolean;
returned(value: TReturnValue | SinonMatcher): boolean;
/**
* Returns true if spy threw an exception at least once.
*/
Expand Down Expand Up @@ -172,9 +170,9 @@ declare namespace Sinon {

interface SinonSpy<TArgs extends any[] = any[], TReturnValue = any>
extends Pick<
SinonSpyCallApi<TArgs, TReturnValue>,
Exclude<keyof SinonSpyCallApi<TArgs, TReturnValue>, 'args'>
> {
SinonSpyCallApi<TArgs, TReturnValue>,
Exclude<keyof SinonSpyCallApi<TArgs, TReturnValue>, 'args'>
> {
// Properties
/**
* The number of recorded calls.
Expand Down Expand Up @@ -378,20 +376,17 @@ declare namespace Sinon {
* The original method can be restored by calling object.method.restore().
* The returned spy is the function object which replaced the original method. spy === object.method.
*/
<T, K extends keyof T>(obj: T, method: K): T[K] extends (
...args: infer TArgs
) => infer TReturnValue
<T, K extends keyof T>(obj: T, method: K): T[K] extends (...args: infer TArgs) => infer TReturnValue
? SinonSpy<TArgs, TReturnValue>
: SinonSpy;

<T, K extends keyof T>(obj: T, method: K, types: Array<('get'|'set')>): PropertyDescriptor & {
<T, K extends keyof T>(obj: T, method: K, types: Array<'get' | 'set'>): PropertyDescriptor & {
get: SinonSpy<[], T[K]>;
set: SinonSpy<[T[K]], void>;
};
}

interface SinonStub<TArgs extends any[] = any[], TReturnValue = any>
extends SinonSpy<TArgs, TReturnValue> {
interface SinonStub<TArgs extends any[] = any[], TReturnValue = any> extends SinonSpy<TArgs, TReturnValue> {
/**
* Resets the stub’s behaviour to the default behaviour
* You can reset behaviour of all stubs using sinon.resetBehavior()
Expand Down Expand Up @@ -436,7 +431,9 @@ declare namespace Sinon {
* The Promise library can be overwritten using the usingPromise method.
* Since sinon@2.0.0
*/
resolves(value?: TReturnValue extends PromiseLike<infer TResolveValue> ? TResolveValue : any): SinonStub<TArgs, TReturnValue>;
resolves(
value?: TReturnValue extends PromiseLike<infer TResolveValue> ? TResolveValue : any,
): SinonStub<TArgs, TReturnValue>;
/**
* Causes the stub to return a Promise which resolves to the argument at the provided index.
* stub.resolvesArg(0); causes the stub to return a Promise which resolves to the first argument.
Expand Down Expand Up @@ -512,11 +509,7 @@ declare namespace Sinon {
* @param context
* @param args
*/
callsArgOnWith(
index: number,
context: any,
...args: any[]
): SinonStub<TArgs, TReturnValue>;
callsArgOnWith(index: number, context: any, ...args: any[]): SinonStub<TArgs, TReturnValue>;
/**
* Same as their corresponding non-Async counterparts, but with callback being deferred at called after all instructions in the current call stack are processed.
* In Node environment the callback is deferred with process.nextTick.
Expand All @@ -543,11 +536,7 @@ declare namespace Sinon {
* In Node environment the callback is deferred with process.nextTick.
* In a browser the callback is deferred with setTimeout(callback, 0).
*/
callsArgOnWithAsync(
index: number,
context: any,
...args: any[]
): SinonStub<TArgs, TReturnValue>;
callsArgOnWithAsync(index: number, context: any, ...args: any[]): SinonStub<TArgs, TReturnValue>;
/**
* Makes the stub call the provided @param func when invoked.
* @param func
Expand Down Expand Up @@ -612,11 +601,7 @@ declare namespace Sinon {
/**
* Like above but with an additional parameter to pass the this context.
*/
yieldsToOn(
property: string,
context: any,
...args: any[]
): SinonStub<TArgs, TReturnValue>;
yieldsToOn(property: string, context: any, ...args: any[]): SinonStub<TArgs, TReturnValue>;
/**
* Same as their corresponding non-Async counterparts, but with callback being deferred at called after all instructions in the current call stack are processed.
* In Node environment the callback is deferred with process.nextTick.
Expand Down Expand Up @@ -648,11 +633,7 @@ declare namespace Sinon {
* @param context
* @param args
*/
yieldsToOnAsync(
property: string,
context: any,
...args: any[]
): SinonStub<TArgs, TReturnValue>;
yieldsToOnAsync(property: string, context: any, ...args: any[]): SinonStub<TArgs, TReturnValue>;
/**
* Stubs the method only for the provided arguments.
* This is useful to be more expressive in your assertions, where you can access the spy with the same call.
Expand Down Expand Up @@ -684,9 +665,7 @@ declare namespace Sinon {
* An exception is thrown if the property is not already a function.
* The original function can be restored by calling object.method.restore(); (or stub.restore();).
*/
<T, K extends keyof T>(obj: T, method: K): T[K] extends (
...args: infer TArgs
) => infer TReturnValue
<T, K extends keyof T>(obj: T, method: K): T[K] extends (...args: infer TArgs) => infer TReturnValue
? SinonStub<TArgs, TReturnValue>
: SinonStub;
}
Expand Down Expand Up @@ -908,13 +887,7 @@ declare namespace Sinon {
* @param filter
*/
addFilter(
filter: (
method: string,
url: string,
async: boolean,
username: string,
password: string
) => boolean
filter: (method: string, url: string, async: boolean, username: string, password: string) => boolean,
): void;
/**
* By assigning a function to the onCreate property of the returned object from useFakeXMLHttpRequest()
Expand Down Expand Up @@ -981,10 +954,7 @@ declare namespace Sinon {
/**
* Responds to all requests to given URL, e.g. /posts/1.
*/
respondWith(
url: string,
fn: (xhr: SinonFakeXMLHttpRequest) => void
): void;
respondWith(url: string, fn: (xhr: SinonFakeXMLHttpRequest) => void): void;
/**
* Responds to all method requests to the given URL with the given response.
* method is an HTTP verb.
Expand All @@ -999,11 +969,7 @@ declare namespace Sinon {
* Responds to all method requests to the given URL with the given response.
* method is an HTTP verb.
*/
respondWith(
method: string,
url: string,
fn: (xhr: SinonFakeXMLHttpRequest) => void
): void;
respondWith(method: string, url: string, fn: (xhr: SinonFakeXMLHttpRequest) => void): void;
/**
* URL may be a regular expression, e.g. /\\/post\\//\\d+
* If the response is a Function, it will be passed any capture groups from the regular expression along with the XMLHttpRequest object:
Expand All @@ -1018,10 +984,7 @@ declare namespace Sinon {
* URL may be a regular expression, e.g. /\\/post\\//\\d+
* If the response is a Function, it will be passed any capture groups from the regular expression along with the XMLHttpRequest object:
*/
respondWith(
url: RegExp,
fn: (xhr: SinonFakeXMLHttpRequest) => void
): void;
respondWith(url: RegExp, fn: (xhr: SinonFakeXMLHttpRequest) => void): void;
/**
* Responds to all method requests to URLs matching the regular expression.
*/
Expand All @@ -1033,11 +996,7 @@ declare namespace Sinon {
/**
* Responds to all method requests to URLs matching the regular expression.
*/
respondWith(
method: string,
url: RegExp,
fn: (xhr: SinonFakeXMLHttpRequest) => void
): void;
respondWith(method: string, url: RegExp, fn: (xhr: SinonFakeXMLHttpRequest) => void): void;
/**
* Causes all queued asynchronous requests to receive a response.
* If none of the responses added through respondWith match, the default response is [404, {}, ""].
Expand Down Expand Up @@ -1149,7 +1108,10 @@ declare namespace Sinon {
* @param spyOrSpyCall
* @param args
*/
calledWith<TArgs extends any[]>(spyOrSpyCall: SinonSpy<TArgs> | SinonSpyCall<TArgs>, ...args: MatchArguments<TArgs>): void;
calledWith<TArgs extends any[]>(
spyOrSpyCall: SinonSpy<TArgs> | SinonSpyCall<TArgs>,
...args: MatchArguments<TArgs>
): void;
/**
* Passes if spy was always called with the provided arguments.
* @param spy
Expand Down Expand Up @@ -1190,10 +1152,7 @@ declare namespace Sinon {
* This behaves the same way as sinon.assert.calledWith(spy, sinon.match(arg1), sinon.match(arg2), ...).
* It's possible to assert on a dedicated spy call: sinon.assert.calledWithMatch(spy.secondCall, arg1, arg2, ...);.
*/
calledWithMatch<TArgs extends any[]>(
spyOrSpyCall: SinonSpy<TArgs> | SinonSpyCall<TArgs>,
...args: TArgs
): void;
calledWithMatch<TArgs extends any[]>(spyOrSpyCall: SinonSpy<TArgs> | SinonSpyCall<TArgs>, ...args: TArgs): void;
/**
* Passes if spy was called once with matching arguments.
* This behaves the same way as calling both sinon.assert.calledOnce(spy) and
Expand Down Expand Up @@ -1500,15 +1459,15 @@ declare namespace Sinon {
* @template TType Object type being stubbed.
*/
type SinonStubbedInstance<TType> = {
[P in keyof TType]: SinonStubbedMember<TType[P]>
[P in keyof TType]: SinonStubbedMember<TType[P]>;
};

/**
* Replaces a type with a Sinon stub if it's a function.
*/
type SinonStubbedMember<T> = T extends (
...args: infer TArgs
) => infer TReturnValue ? SinonStub<TArgs, TReturnValue> : T;
type SinonStubbedMember<T> = T extends (...args: infer TArgs) => infer TReturnValue
? SinonStub<TArgs, TReturnValue>
: T;

interface SinonFake {
/**
Expand Down Expand Up @@ -1562,6 +1521,7 @@ declare namespace Sinon {
clock: SinonFakeTimers;
requests: SinonFakeXMLHttpRequest[];
server: SinonFakeServer;
match: SinonMatch;
/**
* Works exactly like sinon.spy
*/
Expand All @@ -1575,6 +1535,8 @@ declare namespace Sinon {
*/
mock: SinonMockStatic;

fake: SinonFake;

/**
* * No param : Causes Sinon to replace the global setTimeout, clearTimeout, setInterval, clearInterval, setImmediate, clearImmediate, process.hrtime, performance.now(when available)
* and Date with a custom implementation which is bound to the returned clock object.
Expand All @@ -1592,9 +1554,7 @@ declare namespace Sinon {
* You would have to call either clock.next(), clock.tick(), clock.runAll() or clock.runToLast() (see example below). Please refer to the lolex documentation for more information.
* @param config
*/
useFakeTimers(
config?: number | Date | Partial<SinonFakeTimersConfig>
): SinonFakeTimers;
useFakeTimers(config?: number | Date | Partial<SinonFakeTimersConfig>): SinonFakeTimers;
/**
* Causes Sinon to replace the native XMLHttpRequest object in browsers that support it with a custom implementation which does not send actual requests.
* In browsers that support ActiveXObject, this constructor is replaced, and fake objects are returned for XMLHTTP progIds.
Expand Down Expand Up @@ -1645,23 +1605,15 @@ declare namespace Sinon {
* replacement can be any value, including spies, stubs and fakes.
* This method only works on non-accessor properties, for replacing accessors, use sandbox.replaceGetter() and sandbox.replaceSetter().
*/
replace<T, TKey extends keyof T>(
obj: T,
prop: TKey,
replacement: T[TKey]
): T[TKey];
replace<T, TKey extends keyof T>(obj: T, prop: TKey, replacement: T[TKey]): T[TKey];
/**
* Replaces getter for property on object with replacement argument. Attempts to replace an already replaced getter cause an exception.
* replacement must be a Function, and can be instances of spies, stubs and fakes.
* @param obj
* @param prop
* @param replacement
*/
replaceGetter<T, TKey extends keyof T>(
obj: T,
prop: TKey,
replacement: () => T[TKey]
): () => T[TKey];
replaceGetter<T, TKey extends keyof T>(obj: T, prop: TKey, replacement: () => T[TKey]): () => T[TKey];
/**
* Replaces setter for property on object with replacement argument. Attempts to replace an already replaced setter cause an exception.
* replacement must be a Function, and can be instances of spies, stubs and fakes.
Expand All @@ -1672,7 +1624,7 @@ declare namespace Sinon {
replaceSetter<T, TKey extends keyof T>(
obj: T,
prop: TKey,
replacement: (val: T[TKey]) => void
replacement: (val: T[TKey]) => void,
): (val: T[TKey]) => void;

/**
Expand All @@ -1686,15 +1638,15 @@ declare namespace Sinon {
*/
createStubInstance<TType>(
constructor: StubbableType<TType>,
overrides?: { [K in keyof TType]?:
SinonStubbedMember<TType[K]> | (TType[K] extends (...args: any[]) => infer R ? R : TType[K]) }
overrides?: {
[K in keyof TType]?:
| SinonStubbedMember<TType[K]>
| (TType[K] extends (...args: any[]) => infer R ? R : TType[K]);
},
): SinonStubbedInstance<TType>;
}

interface SinonApi {
fake: SinonFake;
match: SinonMatch;
spyCall(...args: any[]): SinonSpyCall;
expectation: SinonExpectationStatic;

clock: {
Expand All @@ -1712,18 +1664,25 @@ declare namespace Sinon {
*/
createSandbox(config?: Partial<SinonSandboxConfig>): SinonSandbox;
defaultConfig: Partial<SinonSandboxConfig>;
}

interface LegacySandbox {
sandbox: {
/**
* @deprecated Since 5.0, use `sinon.createSandbox` instead
*/
create(config?: Partial<SinonSandboxConfig>): SinonSandbox;
};
/**
* Add a custom behavior.
* The name will be available as a function on stubs, and the chaining mechanism
* will be set up for you (e.g. no need to return anything from your function,
* its return value will be ignored). The fn will be passed the fake instance
* as its first argument, and then the user's arguments.
*/
addBehavior: (name: string, fn: (fake: SinonStub, ...userArgs: any[]) => void) => void;

/**
* Replace the default formatter used when formatting ECMAScript object
* An example converts a basic object, such as {id: 42 }, to a string
* on a format of your choosing, such as "{ id: 42 }"
*/
setFormatter: (customFormatter: (...args: any[]) => string) => void;
}

type SinonStatic = SinonSandbox & LegacySandbox & SinonApi;
type SinonStatic = SinonSandbox & SinonApi;
}

declare const Sinon: Sinon.SinonStatic;
Expand Down
Loading

0 comments on commit f19234f

Please sign in to comment.