Skip to main content

String

.toBeString()

Use .toBeString when checking if a value is a String.

test('passes when value is a string', () => {
  expect('').toBeString();
  expect('hello').toBeString();
  expect(new String('hello')).toBeString();
  expect(true).not.toBeString();
});

Open browser consoleTests

.toBeHexadecimal(string)

Use .toBeHexadecimal when checking if a value is a valid HTML hexadecimal color.

test('passes when value is a valid hexadecimal', () => {
  expect('#abc123').toBeHexadecimal();
  expect('#FFF').toBeHexadecimal();
  expect('#000000').toBeHexadecimal();
  expect('#123ffg').not.toBeHexadecimal();
});

Open browser consoleTests

.toBeDateString(string)

Use .toBeDateString when checking if a value is a valid date string.

test('passes when value is a valid toBeDateString', () => {
  expect('2019-11-27T14:05:07.520Z').toBeDateString();
  expect('11/12/21').toBeDateString();
  expect('not a date').not.toBeDateString();
});

Open browser consoleTests

.toEqualCaseInsensitive(string)

Use .toEqualCaseInsensitive when checking if a string is equal (===) to another ignoring the casing of both strings.

test('passes when strings are equal ignoring case', () => {
  expect('hello world').toEqualCaseInsensitive('hello world');
  expect('hello WORLD').toEqualCaseInsensitive('HELLO world');
  expect('HELLO WORLD').toEqualCaseInsensitive('hello world');
  expect('hello world').toEqualCaseInsensitive('HELLO WORLD');
  expect('hello world').not.toEqualCaseInsensitive('hello');
});

Open browser consoleTests

.toStartWith(prefix)

Use .toStartWith when checking if a String starts with a given String prefix.

test('passes when value is starts with given string', () => {
  expect('hello world').toStartWith('hello');
  expect('hello world').not.toStartWith('world');
});

Open browser consoleTests

.toEndWith(suffix)

Use .toEndWith when checking if a String ends with a given String suffix.

test('passes when value is ends with given string', () => {
  expect('hello world').toEndWith('world');
  expect('hello world').not.toEndWith('hello');
});

Open browser consoleTests

.toInclude(substring)

Use .toInclude when checking if a String includes the given String substring.

test('passes when value includes substring', () => {
  expect('hello world').toInclude('ell');
  expect('hello world').not.toInclude('bob');
});

Open browser consoleTests

.toIncludeRepeated(substring, times)

Use .toIncludeRepeated when checking if a String includes the given String substring the correct number of times.

test('passes when value includes substring n times', () => {
  expect('hello hello world').toIncludeRepeated('hello', 2);
  expect('hello hello world').not.toIncludeRepeated('hello', 1);
});

Open browser consoleTests

.toIncludeMultiple([substring])

Use .toIncludeMultiple when checking if a String includes all of the given substrings.

test('passes when value includes all substrings', () => {
  expect('hello world').toIncludeMultiple(['world', 'hello']);
  expect('hello world').not.toIncludeMultiple(['world', 'hello', 'bob']);
});

Open browser consoleTests

.toEqualIgnoringWhitespace(string)

Use .toEqualIgnoringWhitespace when checking if a String is equal to another String ignoring white-space.

test('passes if strings are equal ignoring white-space', () => {
  expect('hello world').toEqualIgnoringWhitespace(`
      hello
      world
  `);
  expect('SELECT * FROM TABLE WHERE CONDITION').toEqualIgnoringWhitespace(`
      SELECT * FROM TABLE
      WHERE CONDITION
  `);
  expect('.class { cssRule: value }').not.toEqualIgnoringWhitespace(`
      #id {
          cssRule: value
      }
    `);
});

Open browser consoleTests