export type OperatorFunction> = (value: IMocked) => IMocked; export type MatchFunction = (passedValue: T) => boolean; export type FunctionsOnly = Pick[K] extends Function ? K : never }[keyof T]>; export type PropertiesOnly = Pick[K] extends Function ? never : K }[keyof T]>; /** * Allows custom logic to verify that a function parameter has the expected value. */ export interface IParameterMatcher { /** * function that takes the actual parameter value and returns true if it was the expected value */ readonly isExpectedValue: MatchFunction; /** * A string representation of the expected value to be used in failure messages */ readonly expectedDisplayValue: string; /** * Used to format a value into a string to display value of actual parameters passed in failure messages */ readonly parameterToString?: (value: T) => string; } export type ParameterMatcher = IParameterMatcher | MatchFunction | T; type FunctionOrConstructor = (new (...params: any[]) => any) | ((...params: any[]) => any); export type FunctionParameterMatchers = { [P in keyof T]: T[P] extends FunctionOrConstructor ? IParameterMatcher : ParameterMatcher; }; export type FunctionCallLookup, U extends LookupType> = { [P in FunctionName]?: LookupParams[]; }; export type LookupFunction< T, C extends ConstructorFunction, U extends LookupType, K extends FunctionName, > = VerifierTarget[K]; export type LookupParams< T, C extends ConstructorFunction, U extends LookupType, K extends FunctionName, > = U extends 'constructorFunction' ? ConstructorParams : U extends FunctionTypes ? FunctionParams> : U extends SetterTypes ? [LookupFunction] : []; export type FunctionParams = T extends (...args: infer P) => any ? P : never; export type ConstructorFunction = (abstract new (...args: any[]) => T) | (new (...args: any[]) => T); export type ConstructorParams> = T extends abstract new ( ...args: infer RAbstract ) => T ? RAbstract : T extends new (...args: infer R) => any ? R : never; export type StaticLookupTypes = 'staticFunction' | 'staticGetter' | 'staticSetter'; export type InstanceLookupTypes = 'function' | 'getter' | 'setter'; export type SetterTypes = 'staticSetter' | 'setter'; export type GetterTypes = 'staticGetter' | 'getter'; export type FunctionTypes = 'staticFunction' | 'function'; export type LookupType = StaticLookupTypes | InstanceLookupTypes | 'constructorFunction'; export type VerifierTarget, U extends LookupType> = U extends 'constructorFunction' ? { constructorFunction: C } : U extends StaticLookupTypes ? U extends FunctionTypes ? FunctionsOnly : C : U extends FunctionTypes ? FunctionsOnly : T; export type FunctionName, U extends LookupType> = keyof VerifierTarget; export interface IFunctionWithParametersVerification< P extends Array, T, U extends LookupType, C extends ConstructorFunction = never, > extends IFunctionVerifier { /** * Checks the parameters in a non-strict equality way. * defaults to the toEqual() matcher * Equivalent to expected == actual * * Expected parameter values can be passed that uses the default matcher (toEqual) * Other matchers, a comparison function or a custom IParameterMatcher can also be passed * * If your function accepts functions as parameters an IParameterMatcher must be used * * Note: if a matcher function ((value: T) => true) no information will be provided about what * value the parameter was expected to be in a test failure message. * To provide this expected value string implement IParameterMatcher instead * * @param args list of parameters to compare against */ withParameters(...args: FunctionParameterMatchers

): IStrictFunctionVerification; /** * Checks the parameters in a strict euqlity way. * defaults to the toBe() matcher * Equivalent to expected == actual * * Expected parameter values can be passed that uses the default matcher (toBe) * Other matchers, a comparison function or a custom IParameterMatcher can also be passed * * If your function accepts functions as parameters an IParameterMatcher must be used * * Note: if a matcher function ((value: T) => true) no information will be provided about what * value the parameter was expected to be in a test failure message. * To provide this expected value string implement IParameterMatcher instead * * @param args list of parameters to compare against */ withParametersEqualTo(...args: FunctionParameterMatchers

): IStrictFunctionVerification; } export interface IStrictFunctionVerification< T, U extends LookupType, C extends ConstructorFunction = never, > extends IFunctionVerifier { /** * verify that the function has been called ONLY with the specified parameters and never without */ strict(): IFunctionVerifier; } export interface IFunctionVerifier = never> { type: U; functionName: FunctionName; parameterMatchers?: (MatchFunction | IParameterMatcher)[]; strictCallCount: boolean; getMock(): IMocked; } export interface IMocked = never> { /** * The mocked object. This should be passed to your SUT. */ mock: T; /** * The mocked constructor (if statics have been mocked). * This can be used to return the mocked instance with new myMock.constructor() and for accessing statics. */ mockConstructor: C; constructorCallLookup: FunctionCallLookup; functionCallLookup: FunctionCallLookup; setterCallLookup: FunctionCallLookup; getterCallLookup: FunctionCallLookup; staticFunctionCallLookup: FunctionCallLookup; staticSetterCallLookup: FunctionCallLookup; staticGetterCallLookup: FunctionCallLookup; functionReplacementLookup: Partial any) | undefined>>>; /** * Used to setup the mock with multiple operators. * * Mock.create().setup( * setupFunction("functionName"), * setupFunction("otherFunction"), * setupProperty("propertyName"), * ); * * @param operators */ setup(...operators: OperatorFunction[]): IMocked; setupConstructor(): IFunctionWithParametersVerification, T, 'constructorFunction', C>; /** * Sets up a single function and returns a function verifier to verify calls made and parameters passed. * * @param functionName * @param mockFunction */ setupFunction>( functionName: K, mockFunction?: T[K], ): IFunctionWithParametersVerification, T, 'function', C>; /** * Sets up a single property and returns a function verifier to verify value get or set operations. * * @param propertyname * @param value */ setupProperty( propertyname: K, value?: T[K], ): { getter: IFunctionVerifier; setter: IFunctionVerifier }; /** * Defines a single property and allows getters and setters to be defined. * Returns a function verifier to verify get and set operations * * @param propertyname * @param getter * @param setter */ defineProperty( propertyname: K, getter?: () => T[K], setter?: (value: T[K]) => void, ): { getter: IFunctionVerifier; setter: IFunctionVerifier }; /** * Sets up a single static function and returns a function verifier to verify calls made and parameters passed. * * @param functionName * @param mockFunction */ setupStaticFunction>( functionName: K, mockFunction?: C[K], ): IFunctionWithParametersVerification, T, 'staticFunction', C>; /** * Sets up a single static property and returns a function verifier to verify value get or set operations. * * @param propertyname * @param value */ setupStaticProperty( propertyname: K, value?: C[K], ): { getter: IFunctionVerifier; setter: IFunctionVerifier }; /** * Defines a single static property and allows getters and setters to be defined. * Returns a function verifier to verify get and set operations * * @param propertyname * @param getter * @param setter */ defineStaticProperty( propertyname: K, getter?: () => C[K], setter?: (value: C[K]) => void, ): { getter: IFunctionVerifier; setter: IFunctionVerifier }; /** * Verifies calls to constructor. * expect(myMock.withFunction("functionName")).wasNotCalled(): * expect(myMock.withFunction("functionName")).wasCalledOnce(): * expect(myMock.withFunction("functionName").withParameters("one", 2)).wasCalledOnce(): */ withConstructor(): IFunctionWithParametersVerification, T, 'constructorFunction', C>; /** * Verifies calls to a previously setup function. * expect(myMock.withFunction("functionName")).wasNotCalled(): * expect(myMock.withFunction("functionName")).wasCalledOnce(): * expect(myMock.withFunction("functionName").withParameters("one", 2)).wasCalledOnce(): * * @param functionName */ withFunction>( functionName: K, ): IFunctionWithParametersVerification, T, 'function', C>; /** * Verifies calls to a previously setup getter. * expect(myMock.withGetter("propertyName")).wasNotCalled(): * expect(myMock.withGetter("propertyName")).wasCalledOnce(): * * @param functionName */ withGetter(propertyname: K): IFunctionVerifier; /** * Verifies calls to a previously setup setter. * expect(myMock.withSetter("propertyName")).wasNotCalled(): * expect(myMock.withSetter("propertyName")).wasCalledOnce(): * expect(myMock.withSetter("propertyName").withParameters("one")).wasCalledOnce(): * * @param functionName */ withSetter(propertyname: K): IFunctionWithParametersVerification<[T[K]], T, 'setter', C>; /** * Verifies calls to a previously setup static function. * expect(myMock.withStaticFunction("functionName")).wasNotCalled(): * expect(myMock.withStaticFunction("functionName")).wasCalledOnce(): * expect(myMock.withStaticFunction("functionName").withParameters("one", 2)).wasCalledOnce(): * * @param functionName */ withStaticFunction>( functionName: K, ): IFunctionWithParametersVerification, T, 'staticFunction', C>; /** * Verifies calls to a previously setup static getter. * expect(myMock.withStaticGetter("functionName")).wasNotCalled(): * expect(myMock.withStaticGetter("functionName")).wasCalledOnce(): * * @param functionName */ withStaticGetter(propertyname: K): IFunctionVerifier; /** * Verifies calls to a previously setup static setter. * expect(myMock.withStaticSetter("functionName")).wasNotCalled(): * expect(myMock.withStaticSetter("functionName")).wasCalledOnce(): * expect(myMock.withStaticSetter("functionName").withParameters("one")).wasCalledOnce(): * * @param functionName */ withStaticSetter( propertyname: K, ): IFunctionWithParametersVerification<[C[K]], T, 'staticSetter', C>; }