Basic Assertions - dylanparry/ceylon GitHub Wiki

Table of Contents

  1. toExist
  2. toNotExist
  3. toBe
  4. toNotBe
  5. toEqual
  6. toNotEqual
  7. toBeA
  8. toNotBeA

toExist

expect(item: any).toExist(message?: string): this;

Asserts that the tested item is not one of the following:

  • undefined
  • null
  • '' (empty string)
  • [] (empty array)
  • {} (empty object)

Outputs an optional message in case of a failed assertion.

Unlike some other assertion libraries, this will not throw an Assertion Error for:

  • 0
  • false
  • NaN

As all these are considered to be existant values.

Example

expect('I am a string').toExist();

expect('').toExist(); // Assertion Error

toNotExist

expect(item: any).toNotExist(message?: string): this;

Asserts that the tested item is one of the following:

  • undefined
  • null
  • '' (empty string)
  • [] (empty array)
  • {} (empty object)

Outputs an optional message in case of a failed assertion.

Unlike some other assertion libraries, this will throw an Assertion Error for:

  • 0
  • false
  • NaN

As all these are considered to be existant values.

Example

expect('').toNotExist();

expect(false).toNotExist(); // Assertion Error

toBe

expect(item: T).toBe(value: T, message?: string): this;

Asserts that the tested item is strictly equal to value.

Outputs an optional message in case of a failed assertion.

Example

expect(50 + 50).toBe(100);

expect('I am a string').toBe('I AM A STRING'); // Assertion Error

toNotBe

expect(item: T).toNotBe(value: T, message?: string): this;

Asserts that the tested item is not strictly equal to value.

Outputs an optional message in case of a failed assertion.

Example

expect('I am a string').toNotBe('I AM A STRING');

expect(50 + 50).toNotBe(100); // Assertion Error

toEqual

expect(item: T).toEqual(value: T, message?: string): this;

Asserts that the tested item is deep equal to value. This should be used for testing objects and arrays.

Outputs an optional message in case of a failed assertion.

Example

expect({ id: 1, name: 'Example' }).toEqual({ id: 1, name: 'Example' });

expect({ id: 1, name: 'Example' }).toEqual({ id: 2, name: 'Example' }); // Assertion Error

toNotEqual

expect(item: T).toNotEqual(value: T, message?: string): this;

Asserts that the tested item is not deep equal to value. This should be used for testing objects and arrays.

Outputs an optional message in case of a failed assertion.

Example

expect({ id: 1, name: 'Example' }).toNotEqual({ id: 2, name: 'Example' });

expect({ id: 1, name: 'Example' }).toNotEqual({ id: 1, name: 'Example' }); // Assertion Error

toBeA

expect(item: T).toBeA(constructor: string, message?: string): this;

Asserts that the type of the tested item is constructor.

Outputs an optional message in case of a failed assertion.

Example

expect(100).toBeA('number');

expect('100').toBeA('number'); // Assertion Error

Aliases

  • toBeAn

toNotBeA

expect(item: T).toNotBeA(constructor: string, message?: string): this;

Asserts that the type of the tested item is not constructor.

Outputs an optional message in case of a failed assertion.

Example

expect('100').toNotBeA('number');

expect(100).toNotBeA('number'); // Assertion Error

Aliases

  • toNotBeAn
⚠️ **GitHub.com Fallback** ⚠️