Options
All
  • Public
  • Public/Protected
  • All
Menu

@morgan-stanley/ts-mocking-bird

Index

Namespaces

Classes

Interfaces

Type aliases

Variables

Functions

Object literals

Type aliases

ConstructorFunction

ConstructorFunction<T>: {} | {}

Type parameters

  • T

FunctionCallLookup

FunctionCallLookup<T, C, U>: {}

Type parameters

Type declaration

FunctionName

FunctionName<T, C, U>: keyof VerifierTarget<T, C, U>

Type parameters

FunctionOrConstructor

FunctionOrConstructor: {} | ((...params: any[]) => any)

FunctionParameterMatchers

FunctionParameterMatchers<T>: {}

Type parameters

  • T: any[]

Type declaration

FunctionParams

FunctionParams<T>: T extends (...args: infer P) => any ? P : never

Type parameters

  • T

FunctionTypes

FunctionTypes: "staticFunction" | "function"

FunctionsOnly

FunctionsOnly<T>: Pick<T, {}[keyof T]>

Type parameters

  • T

GetterTypes

GetterTypes: "staticGetter" | "getter"

InstanceLookupTypes

InstanceLookupTypes: "function" | "getter" | "setter"

LookupParams

LookupParams<T, C, U, K>: U extends FunctionTypes ? FunctionParams<VerifierTarget<T, C, U>[K]> : U extends SetterTypes ? [] : []

Type parameters

LookupType

MatchFunction

MatchFunction<T>: (passedValue: T) => boolean

Type parameters

  • T

Type declaration

    • (passedValue: T): boolean
    • Parameters

      • passedValue: T

      Returns boolean

OperatorFunction

OperatorFunction<T, C>: (value: IMocked<T, C>) => IMocked<T, C>

Type parameters

Type declaration

ParameterMatcher

ParameterMatcher<T>: IParameterMatcher<T> | MatchFunction<T> | T

Type parameters

  • T

PropertiesOnly

PropertiesOnly<T>: Pick<T, {}[keyof T]>

Type parameters

  • T

PropertyDescriptors

PropertyDescriptors<T>: {}

Type parameters

  • T

Type declaration

SetterTypes

SetterTypes: "staticSetter" | "setter"

StaticLookupTypes

StaticLookupTypes: "staticFunction" | "staticGetter" | "staticSetter"

VerifierParams

VerifierParams<T, C, U, K>: U extends SetterTypes ? [] : FunctionParams<VerifierTarget<T, C, U>[K]>

Type parameters

VerifierTarget

VerifierTarget<T, C, U>: U extends StaticLookupTypes ? U extends FunctionTypes ? FunctionsOnly<C> : C : U extends FunctionTypes ? FunctionsOnly<T> : T

Type parameters

WrappedModule

WrappedModule: { ___moduleId: string }

Type declaration

  • ___moduleId: string

Variables

Const MockMap

MockMap: Map<string, undefined | any[]> = new Map<string, any[] | undefined>()

Const ReplaceUndefinedRegExp

ReplaceUndefinedRegExp: RegExp = new RegExp(`"${UndefinedReplacementString}"`, 'g')

Const UndefinedReplacementString

UndefinedReplacementString: "___@morgan-stanley/ts-mocking-bird_undefined_@morgan-stanley/ts-mocking-bird___" = `___@morgan-stanley/ts-mocking-bird_undefined_@morgan-stanley/ts-mocking-bird___`

string value that is VERY unlikely to be a genuine value

Const classReturnValue

classReturnValue: "sampleClassImport" = "sampleClassImport"

Const context

context: any = (require as any).context('./', true, /.spec.ts$/)

Const functionOneReturnValue

functionOneReturnValue: "sampleFunctionOneImport" = "sampleFunctionOneImport"

Const functionStringRegExp

functionStringRegExp: RegExp = /function\s*\([^)]*\)/

Const functionThreeReturnValue

functionThreeReturnValue: "sampleFunctionThreeImport" = "sampleFunctionThreeImport"

Const functionTwoReturnValue

functionTwoReturnValue: "sampleFunctionTwoImport" = "sampleFunctionTwoImport"

Const mockImports

mockImports: replaceProperties = replaceProperties

Const mockImportsBeforeEach

mockImportsBeforeEach: replacePropertiesBeforeEach = replacePropertiesBeforeEach

Functions

addMatchers

  • addMatchers(): void

any

buildAllCallsString

  • buildAllCallsString(functionCalls: any[][], parameterMatchers: (((passedValue: T) => boolean) | IParameterMatcher<any>)[] | undefined): string

buildWithParamsString

  • buildWithParamsString(parameterMatchers: (((passedValue: T) => boolean) | IParameterMatcher<any>)[] | undefined, strict: boolean): string

createCustomMatcherFailResult

  • createCustomMatcherFailResult(message: string): { pass: boolean; message: any } | { message: string; pass: boolean }
  • Parameters

    • message: string

    Returns { pass: boolean; message: any } | { message: string; pass: boolean }

createFunctionParameterVerifier

createFunctionVerifier

createStrictFunctionVerifier

defineProperty

  • defineProperty<T, C, U>(propertyName: U, getter?: undefined | (() => T[U]), setter?: undefined | ((value: T[U]) => void)): OperatorFunction<T, C>
  • Sets up a property on an existing Mock. Allows getter and setter functions to be set. Enables get and set function call verification to be performed.

    Type parameters

    Parameters

    • propertyName: U
    • Optional getter: undefined | (() => T[U])
    • Optional setter: undefined | ((value: T[U]) => void)

    Returns OperatorFunction<T, C>

definePropertyImpl

  • definePropertyImpl<T, C, U, K>(mocked: IMocked<T, C>, lookupType: U, propertyName: K, getter?: undefined | (() => any), setter?: undefined | ((value: any) => void)): IMocked<T, C>

defineStaticProperty

  • defineStaticProperty<T, C, U>(propertyName: U, getter?: undefined | (() => C[U]), setter?: undefined | ((value: C[U]) => void)): OperatorFunction<T, C>
  • Sets up a static property on an existing Mock with constructor. Allows getter and setter functions to be set. Enables get and set function call verification to be performed.

    Type parameters

    Parameters

    • propertyName: U
    • Optional getter: undefined | (() => C[U])
    • Optional setter: undefined | ((value: C[U]) => void)

    Returns OperatorFunction<T, C>

evaluateParameterMatcher

expectedParametersToString

  • expectedParametersToString(parameterMatchers: ParameterMatcher<any>[]): string

functionCallToString

  • functionCallToString(call: any[], parameterMatchers?: ParameterMatcher<any>[]): string

getDescriptor

  • getDescriptor<T, K>(target: object, key: K): TypedPropertyDescriptor<T[K]>

getDescriptors

getLookup

getMocked

  • getMocked(mockModule: WrappedModule, key: string): ((...args: any[]) => any) | undefined

hasValue

isMatcherResult

isMatcherResultArray

isParameterMatcher

  • isParameterMatcher(matcher: unknown): matcher is IParameterMatcher<any>

isWrappedModule

  • isWrappedModule(value: any): value is WrappedModule

lookupMocksForModule

mapItemToString

  • mapItemToString(item: any): string

mapParameterToMatcher

matchParameters

proxyJestModule

  • Wraps an existing module and prepares it for use in jest.mock

    jest.mock('../../relativeImportPath', () => require('@morgan-stanley/ts-mocking-bird').proxyJestModule( require.resolve('../../relativeImportPath'), ), );

    Parameters

    • absolutePath: string

    Returns WrappedModule

proxyModule

  • Takes an existing function and wraps all functions and constructors. This allows us to replace the function or constructor after it has been imported into the system under test This will not replace statics on classes or replace constant values in the module

    Type parameters

    • T

    Parameters

    • originalModule: T

    Returns WrappedModule & T

registerMock

  • registerMock<T>(moduleProxy: T, mock: Partial<T>): void
  • Replaces functions or constructors in a previously wrapped module. If a module is not wrapped a warning is logged and no replacements occur

    Type parameters

    • T

    Parameters

    • moduleProxy: T
    • mock: Partial<T>

    Returns void

replaceImports

  • replaceImports<T>(mocks: Partial<T>, originalPackage: T): void

replaceProperties

  • replaceProperties<T>(target: T, mocks: Partial<T>): void
  • Replaces properties (or functions) in the provided object Replacement is done in a call to 'beforeAll'. Properties are reverted to the original value in a call to 'afterAll'

    Type parameters

    • T

    Parameters

    • target: T

      The original object. Could be an import: 'import * as myImport from "./importLocation"'

    • mocks: Partial<T>

      Object containing functions or properties to replace

    Returns void

replacePropertiesBeforeEach

  • Replaces functions, classes or simple values in objects. This runs before each test allowing us to create a new mock object before each test and assert function calls

    Example:

    // SUT: import { sampleFunction, SampleClass} from "someImport";

    export MyClass{ public getValue(){ return new SampleClass(); }

     public getOtherValue(){
         return sampleFunction();
     }

    }

    // Test:

    describe("test", () => {

    import * as importToMock from "someImport";

    replacePropertiesBeforeEach(() => { const mockedClass = Mock.create().setupFunction("getValue"); const mockedFunction = () => "mockedValue";

    return [{package: importToMock, mocks: {SampleClass: mockedClass.mockConstructor, sampleFunction: mockedFunction}}]; });

    })

    Parameters

    Returns void

replaceValue

  • replaceValue(_key: any, value: any): any

reset

  • reset<T>(moduleProxy: T): void

revertImports

  • revertImports<T>(packageWithReplacements: T, descriptors: PropertyDescriptors<Partial<T>>): void

runningInJest

  • runningInJest(): boolean

sampleFunctionOne

  • sampleFunctionOne(): string

sampleFunctionThree

  • sampleFunctionThree(): string

sampleFunctionTwo

  • sampleFunctionTwo(): string

setupFunction

  • setupFunction<T, C, U>(functionName: U, mockFunction?: T[U]): OperatorFunction<T, C>
  • Mocks a function on an existing Mock. Allows function call verification to be performed later in the test. You can optionally set a mock function implementation that will be called.

    Type parameters

    Parameters

    • functionName: U
    • Optional mockFunction: T[U]

    Returns OperatorFunction<T, C>

setupProperty

setupStaticFunction

  • setupStaticFunction<T, C, U>(functionName: U, mockFunction?: C[U]): OperatorFunction<T, C>
  • Mocks a staticfunction on an existing Mock. Allows function call verification to be performed later in the test. You can optionally set a mock function implementation that will be called.

    Type parameters

    Parameters

    • functionName: U
    • Optional mockFunction: C[U]

    Returns OperatorFunction<T, C>

setupStaticProperty

  • setupStaticProperty<T, C, U>(propertyName: U, value?: C[U]): OperatorFunction<T, C>

someFunction

  • someFunction(): string

someOtherFunction

  • someOtherFunction(): string

toBe

toBeDefined

toEqual

trackCall

trackFunctionCall

  • trackFunctionCall<T, C, U, K>(mock: IMocked<T, C>, lookupType: U, functionName: K, params: LookupParams<T, C, U, K>): void

trackGetterCall

  • trackGetterCall<T, C, U, K>(mock: IMocked<T, C>, lookupType: U, propertyName: K): void

trackSetterCall

  • trackSetterCall<T, C, U, K>(mock: IMocked<T, C>, lookupType: U, propertyName: K, param: LookupParams<T, C, U, K>): void

verifyFailure

  • verifyFailure(verifier: IFunctionVerifier<any, any, any>, matcher: CustomMatcher, message: string, ...params: any[]): void

verifyFunctionCalled

verifyJestFailure

  • verifyJestFailure(verifier: IFunctionVerifier<any, any, any>, matcher: CustomMatcher, message: string, ...params: any[]): void

verifyParameters

wasCalled

wasCalledAtLeastOnce

wasCalledOnce

wasNotCalled

wrapFunction

  • wrapFunction(this: any, mockModule: WrappedModule, key: string, func: Function): wrapped

Object literals

Const matchers

matchers: object

wasCalled

wasCalled: wasCalled

wasCalledAtLeastOnce

wasCalledAtLeastOnce: wasCalledAtLeastOnce

wasCalledOnce

wasCalledOnce: wasCalledOnce

wasNotCalled

wasNotCalled: wasNotCalled

Generated using TypeDoc