Function Assertions - dylanparry/ceylon GitHub Wiki
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 astring
, it asserts that themessage
property of the thrownError
is equal toerror
- If
error
is a regular expression, it asserts that themessage
property of the the thrownError
matches the pattern inerror
- If
error
is afunction
, it asserts that the thrownError
is an instance of the function specified inerror
Outputs an optional message
in case of a failed assertion.
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
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.
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
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.
class MyClass {
public name: string;
public constructor(name: string) {
this.name = name;
}
}
expect(MyClass).toIncludeKey('name');
expect(MyClass).toIncludeKey('foo'); // Assertion Error
- toContainKey
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.
class MyClass {
public name: string;
public constructor(name: string) {
this.name = name;
}
}
expect(MyClass).toExcludeKey('foo');
expect(MyClass).toExcludeKey('name'); // Assertion Error
- toNotIncludeKey
- toNotContainKey
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.
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
- toContainKeys
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.
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
- toNotIncludeKeys
- toNotContainKeys