Skip to content

String Assertions

Dylan Parry edited this page Oct 5, 2016 · 10 revisions

Table of Contents

  1. toMatch
  2. toNotMatch
  3. toInclude
  4. toExclude
  5. toHaveLength

toMatch

expect(item: string).toMatch(pattern: RegExp, message?: string): this;

Asserts that the string item matches the regular expression pattern.

Outputs an optional message in case of a failed assertion.

Example

expect('I am a string').toMatch(/^.*string$/);

expect('I am a string').toMatch(/^.*number$/); // Assertion Error

toNotMatch

expect(item: string).toNotMatch(pattern: RegExp, message?: string): this;

Asserts that the string item does not match the regular expression pattern.

Outputs an optional message in case of a failed assertion.

expect('I am a string').toNotMatch(/^.*number$/);

expect('I am a string').toNotMatch(/^.*string$/); // Assertion Error

toInclude

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

Asserts that the string item contains the string value.

Outputs an optional message in case of a failed assertion.

Example

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

expect('I am a string').toInclude('not'); // Assertion Error

Aliases

  • toContain

toExclude

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

Asserts that the string item does not include the string value.

Outputs an optional message in case of a failed assertion.

Example

expect('I am a string').toExclude('not');

expect('I am a string').toExclude('am'); // Assertion Error

Aliases

  • toNotInclude
  • toNotContain

toHaveLength

expect(item: string).toHaveLength(value: number, message?: string): this;

Asserts that the length of the string item is equal to value.

Example

expect('I am a string!').toHaveLength(14);

expect('I am a string!').toHaveLength(10); // Assertion Error