Options
All
  • Public
  • Public/Protected
  • All
Menu

This class is the central point for writing assertions in the BDD style. Instances of this class can be generated with the expect method.

Then, a call of some method on the expectation instance will create a new assertion in the test.

To negate any assertion, you can use a special property not, that contains another expectation instance with the opposite meaning.

For example:

t.expect(1).toBe(1)
t.expect(1).not.toBe(2)

t.expect('Foo').toContain('oo')
t.expect('Foo').not.toContain('bar')

Hierarchy

Index

Constructors

constructor

Accessors

not

Methods

initialize

  • initialize<T>(props?: Partial<T>): void
  • This method applies its 1st argument (if any) to the current instance using Object.assign().

    Supposed to be overridden in the subclasses to customize the instance creation process.

    Type parameters

    Parameters

    • Optional props: Partial<T>

    Returns void

toBe

  • toBe(expectedValue: unknown): void
  • This assertion compares the value provided to the expect method with the expectedValue argument. Comparison is done with === operator, so it should be used only with the primitives - numbers, strings, booleans etc. However, fuzzy matchers, generated with the any family of methods are supported.

    To deeply compare objects, arrays, and JSON in general, use toEqual method.

    This method works correctly with the fuzzy matchers generated with the any method

    Parameters

    • expectedValue: unknown

      An expected value

    Returns void

toBeCloseTo

  • This assertion passes, when the number provided to the expect method is approximately equal the given number. The proximity can be defined as the precision argument

    Parameters

    • expectedValue: number

      The number to compare with

    • approx: Approximation = ...

      The number approximation

    Returns void

toBeDefined

  • toBeDefined(): void

toBeEqual

  • toBeEqual(expectedValue: unknown): void

toBeFalsy

  • toBeFalsy(): void

toBeGreaterOrEqualThan

  • toBeGreaterOrEqualThan(expectedValue: any): void

toBeGreaterThan

  • toBeGreaterThan(expectedValue: any): void

toBeLessOrEqualThan

  • toBeLessOrEqualThan(expectedValue: number): void

toBeLessThan

  • toBeLessThan(expectedValue: number): void

toBeNaN

  • toBeNaN(): void

toBeNull

  • toBeNull(): void

toBeTruthy

  • toBeTruthy(): void

toBeUndefined

  • toBeUndefined(): void

toContain

  • toContain(element: unknown): void
  • This assertion passes in 2 cases:

    1. When the value provided to the expect method is a string, and it contains a passed substring.
    2. When the value provided to the expect method is an array (or array-like), and it contains a passed element.

    Parameters

    • element: unknown

      The element of the array or a sub-string

    Returns void

toEqual

  • toEqual(expectedValue: unknown): void
  • This assertion compares the value provided to the expect method with the expectedValue argument.

    Comparison works for objects, arrays, and JSON in general. It is performed "deeply". Cyclic structures are properly handled.

    This method works correctly with the fuzzy matchers generated with the any method

    Parameters

    • expectedValue: unknown

      An expected value

    Returns void

toHaveBeenCalled

  • toHaveBeenCalled(expectedNumber?: string | number): void
  • This assertion passes, if a spy, provided to the expect method have been called expected number of times. The expected number of times can be provided as the 1st argument and by default is 1.

    One can also provide the function, spied on, to the expect method.

    Examples:

    const spy = t.spyOn(obj, 'process')

    // call the method 2 times
    obj.process()
    obj.process()

    // following 2 calls are equivalent
    t.expect(spy).toHaveBeenCalled();
    t.expect(obj.process).toHaveBeenCalled();

    // one can also use exact number of calls or comparison operators
    t.expect(spy).toHaveBeenCalled(2);
    t.expect(spy).toHaveBeenCalled('>1');
    t.expect(spy).toHaveBeenCalled('<=3');

    See also toHaveBeenCalledWith

    Parameters

    • expectedNumber: string | number = '>=1'

      Expected number of calls. Can be either a number, specifying the exact number of calls, or a string. In the latter case one should include a comparison operator in front of the number.

    Returns void

toHaveBeenCalledWith

  • toHaveBeenCalledWith(...args: unknown[]): void
  • This assertion passes, if a spy, provided to the expect method have been called at least once with the specified arguments.

    One can also provide the function, spied on, to the expect method.

    One can use fuzzy matchers, generated with the any method to verify the arguments.

    Example:

    const spy = t.spyOn(obj, 'process')

    // call the method 2 times with different arguments
    obj.build('development', '1.0.0')
    obj.build('release', '1.0.1')

    t.expect(spy).toHaveBeenCalledWith('development', '1.0.0');
    // or
    t.expect(obj.process).toHaveBeenCalledWith('development', t.any(String));

    See also toHaveBeenCalled

    Parameters

    • Rest ...args: unknown[]

    Returns void

toMatch

  • toMatch(regexp: RegExp): void
  • This assertion passes, when the string provided to the expect method matches the regular expression.

    Parameters

    • regexp: RegExp

      The regular expression to match the string against

    Returns void

toThrow

  • toThrow(pattern?: string | RegExp): Promise<void>
  • This assertion passes when the function provided to the expect method, throws an exception during its execution.

    t.expect(function(){
    throw "oopsie";
    }).toThrow());

    Parameters

    • Optional pattern: string | RegExp

    Returns Promise<void>

Static maybeNew

  • maybeNew<T>(props?: InstanceType<T> | Partial<InstanceType<T>>): InstanceType<T>
  • This is a type-safe static constructor method, accepting a single argument. If that argument is already an instance of this class - it is returned right away, otherwise the new constructor is used for instantiation.

    Type parameters

    Parameters

    • Optional props: InstanceType<T> | Partial<InstanceType<T>>

    Returns InstanceType<T>

Static new

  • new<T>(props?: Partial<InstanceType<T>>): InstanceType<T>
  • This is a type-safe static constructor method, accepting a single argument, with the object, corresponding to the class properties. It will generate a compilation error, if unknown property is provided.

    For example:

    class MyClass extends Base {
    prop : string
    }

    const instance : MyClass = MyClass.new({ prop : 'prop', wrong : 11 })

    will produce:

    TS2345: Argument of type '{ prop: string; wrong: number; }' is not assignable to parameter of type 'Partial<MyClass>'.
    Object literal may only specify known properties, and 'wrong' does not exist in type 'Partial<MyClass>'

    The only thing this constructor does is create an instance and call the initialize method on it, forwarding the first argument. The customization of instance is supposed to be performed in that method.

    Type parameters

    Parameters

    • Optional props: Partial<InstanceType<T>>

    Returns InstanceType<T>

Generated using TypeDoc