to be a

  • <any> [not] to be (a|an) <function>
  • <any> [not] to be (a|an) <type>
  • <any> [not] to be (a|an) <string>

Asserts that the subject is an instance of a given type.

This assertion makes use of the type system in Unexpected. That means you can assert that a value is an instance of a type specified by its name (as a string).

For more information abort the type system see: Types

expect(true, 'to be a', 'boolean');
expect(5, 'to be a', 'number');
expect('abc', 'to be a', 'string');
expect(expect, 'to be a', 'function');
expect({ foo: 123 }, 'to be an', 'object');
expect([123], 'to be an', 'array');
expect(/regex/, 'to be a', 'regexp');
expect(/regex/, 'to be a', 'regex');
expect(/regex/, 'to be a', 'regular expression');
expect(new Error(), 'to be an', 'Error');
expect(new Date(), 'to be a', 'date');

The assertions also respect the inheritance chain:

expect(/foo/, 'to be an', 'object');
expect(/foo/, 'to be an', 'any');

Aliases are provided for common types:

expect(true, 'to be a boolean');
expect(5, 'to be a number');
expect('abc', 'to be a string');
expect(expect, 'to be a function');
expect({ foo: 123 }, 'to be an object');
expect([123], 'to be an array');
expect(/regex/, 'to be a regexp');
expect(/regex/, 'to be a regex');
expect(/regex/, 'to be a regular expression');
expect(new Date(), 'to be a date');

If you provide a constructor as the type the assertion will use instanceof.

function Person(name) {
  this.name = name;
}
expect(new Person('John Doe'), 'to be a', Person);
expect(new Person('John Doe'), 'to be an', Object);

In case of a failing expectation you get the following output:

expect({ 0: 'foo', 1: 'bar', 2: 'baz' }, 'to be an array');
expected { 0'foo'1'bar'2'baz' } to be an array

This assertion can be negated using the not flag:

expect(true, 'not to be an object');
expect('5', 'not to be a', 'number');
expect('abc', 'not to be an', Object);

In case of a failing expectation you get the following output:

expect(
  function () {
    return 'wat';
  },
  'not to be an',
  Object
);
expected function () { return 'wat'; } not to be an Object