Function Assertions - dylanparry/ceylon GitHub Wiki

Table of Contents

  1. toThrow
  2. toNotThrow
  3. toIncludeKey
  4. toExcludeKey
  5. toIncludeKeys
  6. toExcludeKeys

toThrow

expect(item: Function).toThrow(error?: string, message?: string): this;
expect(item: Function).toThrow(error?: RegExp, message?: string): this;
expect(item: Function).toThrow(error?: Function, message?: string): this;

Asserts that the function item throws an error. Depending on the type of the optional error argument, one of the following assertions will be performed:

  • If error is a string, it asserts that the message property of the thrown Error is equal to error
  • If error is a regular expression, it asserts that the message property of the the thrown Error matches the pattern in error
  • If error is a function, it asserts that the thrown Error is an instance of the function specified in error

Outputs an optional message in case of a failed assertion.

Example

const myFunction = (num: number): void => {
    if (num > 10) {
        throw new RangeError('Number is out of range');
    }
}

expect(() => myFunction(11)).toThrow();

expect(() => myFunction(11)).toThrow('Number is out of range');

expect(() => myFunction(11)).toThrow(/^.*range$/);

expect(() => myFunction(11)).toThrow(RangeError);

expect(() => myFunction(10)).toThrow(); // Assertion Error

toNotThrow

expect(item: Function).toNotThrow(message?: string): this;

Asserts that the function item does not throw an error.

Outputs an optional message in case of a failed assertion.

Example

const myFunction = (num: number): void => {
    if (num > 10) {
        throw new RangeError('Number is out of range');
    }
}

expect(() => myFunction(10)).toNotThrow();

expect(() => myFunction(11)).toNotThrow(); // Assertion Error

toIncludeKey

expect(item: Function).toIncludeKey(key: string, message?: string): this;

Asserts that the function item includes the property with name key.

Outputs an optional message in case of a failed assertion.

Example

class MyClass {
    public name: string;

    public constructor(name: string) {
        this.name = name;
    }
}

expect(MyClass).toIncludeKey('name');

expect(MyClass).toIncludeKey('foo'); // Assertion Error

Aliases

  • toContainKey

toExcludeKey

expect(item: Function).toExcludeKey(key: string, message?: string): this;

Asserts that the function item does not include the property with name key.

Outputs an optional message in case of a failed assertion.

Example

class MyClass {
    public name: string;

    public constructor(name: string) {
        this.name = name;
    }
}

expect(MyClass).toExcludeKey('foo');

expect(MyClass).toExcludeKey('name'); // Assertion Error

Aliases

  • toNotIncludeKey
  • toNotContainKey

toIncludeKeys

expect(item: Function).toIncludeKeys(key: string[], message?: string): this;

Asserts that the function item includes all the properties in keys.

Outputs an optional message in case of a failed assertion.

Example

class MyClass {
    public name: string;
    public age: number;

    public constructor(name: string, age: number) {
        this.name = name;
        this.age = age;
    }
}

expect(MyClass).toIncludeKeys(['name', 'age']);

expect(MyClass).toIncludeKeys(['foo', 'bar']); // Assertion Error

Aliases

  • toContainKeys

toExcludeKeys

expect(item: Function).toExcludeKeys(key: string[], message?: string): this;

Asserts that the function item does not include any of the properties in keys.

Outputs an optional message in case of a failed assertion.

Example

class MyClass {
    public name: string;
    public age: number;

    public constructor(name: string, age: number) {
        this.name = name;
        this.age = age;
    }
}

expect(MyClass).toExcludeKeys(['foo', 'bar']);

expect(MyClass).toExcludeKeys(['name', 'age', 'baz']); // Assertion Error

Aliases

  • toNotIncludeKeys
  • toNotContainKeys
⚠️ **GitHub.com Fallback** ⚠️