npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jasmine-utils

v0.7.0

Published

jasmine-utils =============

Downloads

1,886

Readme

jasmine-utils

Greenkeeper badge Build Status GitHub version Npm version Bower version

Jasmine-Utils is a set of custom matchers that I used in my previous projects.

Jasmine-Utils is compatible with Jasmine 1.3 and Jasmine 2.0.

Utils Functions

  • jasmine.spyAll(object)
    • Spy all functions of given object.
    • Spies are configured with callThrough
it('should spy all methods', function() {
  var obj = {
    id: 1,
    method1: function() {
    },
    method2: function() {
    }
  };

  jasmine.spyAll(obj);

  expect(obj.method1).not.toHaveBeenCalled();
  expect(obj.method2).not.toHaveBeenCalled();
});
  • jasmine.spyIf(function)
    • Spy function if and only if function is not already a spy.
    • Spy is returned.
it('should spy method once', function() {
  var obj = {
    id: 1,
    method1: function() {
    },
    method2: function() {
    }
  };

  jasmine.spyIf(obj, 'method1');
  jasmine.spyIf(obj, 'method1');
});
  • jasmine.spyAllExcept(object, [excepts])
    • Spy all functions of given object, excepts function names given in array (second arguments).
    • Spies are configured with callThrough
it('should spy selected method', function() {
  var obj = {
    id: 1,
    method1: function() {
    },
    method2: function() {
    }
  };

  jasmine.spyAllExcept(obj, 'method2');
});
  • jasmine.spyEach(object, [only])
    • Spy all functions of given object that are specified in array as second arguments.
    • Spies are configured with callThrough
it('should spy selected method', function() {
  var obj = {
    id: 1,
    method1: function() {
    },
    method2: function() {
    },
    method3: function() {
    }
  };

  jasmine.spyEach(obj, ['method1', 'method2']);
});
  • jasmine.resetAll(object)
    • Reset all spy of given object.
it('should reset all methods', function() {
  var obj = {
    id: 1,
    method1: function() {
    },
    method2: function() {
    }
  };

  spyOn(obj, 'method1');
  spyOn(obj, 'method2');

  obj.method1();
  obj.method2();

  expect(obj.method1).toHaveBeenCalled();
  expect(obj.method2).toHaveBeenCalled();

  jasmine.resetAll(obj);

  expect(obj.method1).not.toHaveBeenCalled();
  expect(obj.method2).not.toHaveBeenCalled();
});
  • jasmine.resetEach(object, [methods])
    • Reset each specified spies of given object.
it('should reset all methods', function() {
  var obj = {
    id: 1,
    method1: function() {
    },
    method2: function() {
    }
  };

  spyOn(obj, 'method1');
  spyOn(obj, 'method2');

  obj.method1();
  obj.method2();

  expect(obj.method1).toHaveBeenCalled();
  expect(obj.method2).toHaveBeenCalled();

  jasmine.resetEach(obj, ['method1']);

  expect(obj.method1).not.toHaveBeenCalled();
  expect(obj.method2).toHaveBeenCalled();
});
  • jasmine.resetAllExcept(object, [methods])
    • Reset all spies of given object except specified methods.
it('should reset all methods', function() {
  var obj = {
    id: 1,
    method1: function() {
    },
    method2: function() {
    }
  };

  spyOn(obj, 'method1');
  spyOn(obj, 'method2');

  obj.method1();
  obj.method2();

  expect(obj.method1).toHaveBeenCalled();
  expect(obj.method2).toHaveBeenCalled();

  jasmine.resetAllExcept(obj, ['method1']);

  expect(obj.method1).toHaveBeenCalled();
  expect(obj.method2).not.toHaveBeenCalled();
});

Matchers

toBeABoolean

Check that the tested object is a boolean (a boolean is exactly true or false).

Since

0.1.0

Parameters

No parameters

Message

Expect [actual] (not) to be a boolean

Example:

expect(true).toBeABoolean();
expect(false).toBeABoolean();
expect(null).not.toBeABoolean();
expect(undefined).not.toBeABoolean();

toBeADate

Check that the tested object is an instance of Date.

Since

0.1.0

Parameters

No parameters

Message

Expect [actual] (not) to be a date

Example:

expect(new Date()).toBeADate();
expect(null).not.toBeADate();
expect(123).not.toBeADate();

toBeAFunction

Check that the tested object is a function.

Since

0.1.0

Parameters

No parameters

Message

Expect [actual] (not) to be a function

Example:

expect(() => {}).toBeAFunction();
expect(function() {}).toBeAFunction();
expect(undefined).not.toBeAFunction();
expect(null).not.toBeAFunction();

toBeAMap

Check that the tested object is a Map.

Since

0.3.0

Parameters

No parameters

Message

Expect [actual] (not) to be a Map

Example:

expect(new Map()).toBeAMap();
expect({}).not.toBeAMap();

toBeANumber

Check that the tested object is a number.

Since

0.1.0

Parameters

No parameters

Message

Expect [actual] (not) to be a number

Example:

expect(0).toBeANumber();
expect(1).toBeANumber();
expect(1.5).toBeANumber();
expect('0').not.toBeANumber();
expect('1').not.toBeANumber();

toBeASet

Check that the tested object is a Set.

Since

0.3.0

Parameters

No parameters

Message

Expect [actual] (not) to be a Set

Example:

expect(new Set()).toBeASet();
expect({}).not.toBeASet();
expect([]).not.toBeASet();

toBeAString

Check that the tested object is a string.

Since

0.1.0

Parameters

No parameters

Message

Expect [actual] (not) to be a string

Example:

expect('').toBeAString();
expect('foo').toBeAString();
expect(String('foo')).toBeAString();
expect(false).not.toBeAString();
expect(0).not.toBeAString();

toBeAnArray

Check that the tested object is an array (a real array, not an array-like object). This matcher will use Array.isArray or a fallback if it is not available.

Since

0.1.0

Parameters

No parameters

Message

Expect [actual] (not) to be an array

Example:

expect([]).toBeAnArray();
expect('123').not.toBeAnArray();
expect(1).not.toBeAnArray();
expect(false).not.toBeAnArray();
expect({}).not.toBeAnArray();
expect(null).not.toBeAnArray();
expect(undefined).not.toBeAnArray();

toBeAnEmptyString

Check that the tested object is an empty string (a string equals to '').

Since

0.1.0

Parameters

No parameters

Message

Expect [actual] (not) to be an empty string

Example:

expect('').toBeAnEmptyString();
expect('  ').not.toBeAnEmptyString();
expect('foo').not.toBeAnEmptyString();

toBeArguments

Check that the tested object is an arguments object.

Since

0.5.0

Parameters

No parameters

Message

Expect [actual] (not) to be arguments

Example:

expect((function() { return arguments; })()).toBeArguments();
expect([]).not.toBeArguments();

toBeDateAfterNow

Check that the tested date object is a date "after" now.

The tested date may be:

  • A date instance.
  • A timestamp.
  • A string that can be parsed with the Date constructor (i.e new Date('2016-01-01')).

Note: Using date strings should be avoided due to browser differences and inconsistencies.

Since

0.1.0

Parameters

No parameters

Message

Expect date [actual] (not) to be after now

Example:

expect(Date.now() + 1000).toBeDateAfterNow();
expect(new Date(Date.now() + 1000)).toBeDateAfterNow();
expect(new Date(Date.now() - 1000)).not.toBeDateAfterNow();

toBeDateAfter

Check that the tested date object is a date "after" an other date.

The tested date and the date to compare may be:

  • A date instance.
  • A timestamp.
  • A string that can be parsed with the Date constructor (i.e new Date('2016-01-01')).

Note: Using date strings should be avoided due to browser differences and inconsistencies.

Since

0.1.0

Parameters

| Name | Type | Description | |------|------|-------------| | lower | Date,number,string | The lower bound. |

Message

Expect date [actual] (not) to be after [lower]

Example:

expect(Date.now()).toBeDateAfter(Date.now() - 1000));
expect(Date.now() - 1000).toBeDateAfter(Date.now()));

toBeDateBeforeNow

Check that the tested date object is a date "before" now.

The tested date may be:

  • A date instance.
  • A timestamp.
  • A string that can be parsed with the Date constructor (i.e new Date('2016-01-01')).

Note: Using date strings should be avoided due to browser differences and inconsistencies.

Since

0.1.0

Parameters

No parameters

Message

Expect date [actual] (not) to be before now

Example:

expect(Date.now() - 1000).toBeDateBeforeNow();
expect(new Date(Date.now() - 1000)).toBeDateBeforeNow();
expect(new Date(Date.now() + 1000)).not.toBeDateBeforeNow();

toBeDateBefore

Check that the tested date object is a date "before" an other date.

The tested date and the date to compare may be:

  • A date instance.
  • A timestamp.
  • A string that can be parsed with the Date constructor (i.e new Date('2016-01-01')).

Note: Using date strings should be avoided due to browser differences and inconsistencies.

Since

0.1.0

Parameters

| Name | Type | Description | |------|------|-------------| | upper | Date,number,string | The upper bound. |

Message

Expect date [actual] (not) to be before [lower]

Example:

expect(Date.now()).toBeDateBefore(Date.now() + 1000));
expect(Date.now() + 1000).toBeDateBefore(Date.now()));

toBeDateCloseToNow

Check that the tested object is a date close to 'now'.

The tested date may be:

  • A date instance.
  • A timestamp.
  • A string that can be parsed with the Date constructor (i.e new Date('2016-01-01')).

Note: Using date strings should be avoided due to browser differences and inconsistencies.

Since

0.1.0

Parameters

| Name | Type | Description | |------|------|-------------| | max | number | The maximum difference (in milliseconds), defaults to 1000. |

Message

Expect date [actual] (not) to be close to now

Example:

expect(Date.now()).toBeDateCloseToNow();
expect(Date.now() + 1000).toBeDateCloseToNow(2000);
expect(Date.now() - 1000).toBeDateCloseToNow(2000);
expect(new Date()).toBeDateCloseToNow();
expect(new Date(Date.now() + 1000)).not.toBeDateCloseToNow(2000);
expect(new Date(Date.now() - 1000)).not.toBeDateCloseToNow(2000);

toBeDateCloseTo

Check that the tested date object and an actual date object are close.

By default, the difference in milliseconds between both dates must not exceed 1000ms, but the last parameter may be set to increase/decrease this value.

The tested date and the date to compare may be:

  • A date instance.
  • A timestamp.
  • A string that can be parsed with the Date constructor (i.e new Date('2016-01-01')).

Note: Using date strings should be avoided due to browser differences and inconsistencies.

Since

0.1.0

Parameters

| Name | Type | Description | |------|------|-------------| | date | Date,number,string | The second date to compare with. | | max | number | The maximum difference in milliseconds between both dates, default to 1000. |

Message

Expect date [actual] (not) to be close to [date]

Example:

expect(new Date(1995, 1, 1, 10, 0, 0, 0)).toBeDateCloseTo(new Date(1995, 1, 1, 10, 0, 0, 500));
expect(new Date(1995, 1, 1, 10, 0, 0, 0)).toBeDateCloseTo(new Date(1995, 1, 1, 10, 0, 0, 500), 1000);
expect(new Date(1995, 1, 1, 10, 0, 0, 0)).toBeDateCloseTo(new Date(1995, 1, 1, 10, 0, 0, 500), 100);

toBeDOMElementWithAttributes

Check that the tested object is a DOM element with expected attributes (using the getAttribute function).

Note that the attribute value can also be a jasmine matcher (jasmine.any(String) for example).

Since

0.1.0

Parameters

| Name | Type | Description | |------|------|-------------| | attributes | Object | Expected attributes. |

Message

Expect [actual] (not) to be a DOM element with attributes [attributes] but was [actualAttributes]

Example:

const span = document.createElement('span');
span.setAttribute('foo', 'foo');
span.setAttribute('bar', 'bar');

expect(span).toBeDOMElementWithAttributes({ foo: 'foo', bar: jasmine.any(String) });

toBeDOMElementWithClasses

Check that the tested object is a DOM element with expected class names.

Since

0.1.0

Parameters

| Name | Type | Description | |------|------|-------------| | classes | Array.<string>,string | Expected class names. |

Message

Expect [actual] (not) to be a DOM element with classes [classes] but was [actualClasses]

Example:

const span = document.createElement('span');
span.className = 'foo bar';

expect(span).toBeDOMElementWithClasses('foo');
expect(span).toBeDOMElementWithClasses('bar');
expect(span).toBeDOMElementWithClasses('foo bar');
expect(span).toBeDOMElementWithClasses(['foo', 'bar']);

toBeDOMElementWithId

Check that the tested object is a DOM element with an expected id (note that the id is retrieved using getAttribute('id')).

Since

0.1.0

Parameters

| Name | Type | Description | |------|------|-------------| | id | string | Expected id. |

Message

Expect [actual] (not) to be a DOM element with id [id] but was [actualId]

Example:

const span = document.createElement('span');
span.setAttribute('id', 'mySpan');
expect(span).toBeDOMElementWithId('mySpan');

toBeDOMElement

Check that the tested object is DOM element with an expected tag name. The tag name is optional, if not set this matcher will juste check that the actual object is a DOM element.

Since

0.1.0

Parameters

| Name | Type | Description | |------|------|-------------| | tagName | string | Expected tag name (optional). |

Message

Expect [actual] (not) to be a DOM element Expect [actual] (not) to be [tagName] element but was [actualTagName]

Example:

const span = document.createElement('span');
expect(span).toBeDOMElement();
expect(span).toBeDOMElement('span');
expect(span).toBeDOMElement('SPAN');
expect(span).not.toBeDOMElement('div');

toBeEmpty

Check that tested object is empty:

  • If it is an array or an array-like, check that the length is equal to zero.
  • If it is an object, check that it does not have any property.
  • If it is a map or a set, check that the size is equal to zero.

Since

0.1.0

Parameters

No parameters

Message

Expect [actual] (not) to be empty.

Example:

expect('').toBeEmpty();
expect([]).toBeEmpty();
expect({}).toBeEmpty();
expect(new Map()).toBeEmpty();
expect(new Set()).toBeEmpty();

toBeEvenNumber

Check that the tested object is an even number.

Since

0.1.0

Parameters

No parameters

Message

Expect [actual] to be an even number

Example:

expect(2).toBeEvenNumber();
expect(0).not.toBeEvenNumber();
expect(1).not.toBeEvenNumber();

toBeExtensible

Check that the tested object is extensible.

Objects are extensible by default: they can have new properties added to them, and can be modified. An object can be marked as non-extensible using:

  • Object.preventExtensions(),
  • Object.seal(),
  • Object.freeze()

This matcher use internally Object.isExtensible (supported in Chrome, Firefox, Safari and IE >= 9). If Object.isExtensible is not supported, this matcher treat the tested object as an extensible object.

Important: This matcher (as ES6 specification) treat primitive (null, undefined, numbers, strings and booleans) as non extensible object.

See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isExtensible

Since

0.5.0

Parameters

No parameters

Message

Expect [actual] (not) to be extensible

Example:

expect({}).toBeExtensible();
expect([]).toBeExtensible();
expect(null).not.toBeExtensible();
expect(undefined).not.toBeExtensible();
expect('').not.toBeExtensible();
expect(0).not.toBeExtensible();
expect(true).not.toBeExtensible();
expect(Object.freeze({})).not.toBeExtensible();
expect(Object.freeze([])).not.toBeExtensible();
expect(Object.seal({})).not.toBeExtensible();
expect(Object.seal([])).not.toBeExtensible();
expect(Object.preventExtensions({})).not.toBeExtensible();
expect(Object.preventExtensions([])).not.toBeExtensible();

toBeFalse

Check that the tested object is strictly equal false.

Since

0.1.0

Parameters

No parameters

Message

Expect [actual] (not) to be false

Example:

expect(false).toBeFalse();
expect(true).not.toBeFalse();
expect(0).not.toBeFalse();

toBeFiniteNumber

Check that the tested object is a finite number.

Important: This matcher does not convert the tested object to a number, but returns the equivalent of Number.isFinite function.

See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isFinite

Since

0.5.0

Parameters

No parameters

Message

Expect [actual] (not) to be finite number

Example:

expect(1).toBeFiniteNumber();
expect(1.0).toBeFiniteNumber();
expect('1').not.toBeFiniteNumber();
expect(NaN).not.toBeFiniteNumber();
expect(Infinity).not.toBeFiniteNumber();
expect(-Infinity).not.toBeFiniteNumber();
expect(null).not.toBeFiniteNumber();

toBeFloat

Check that the tested object is a float value.

Note that for this matcher, a float is a numeric value (see toBeNumeric matcher) that is not an integer (a numeric value may be a number or a string containing a number).

JavaScript makes no distinction between integers and floats so 1.0 is considered integer.

Since

0.1.0

Parameters

No parameters

Message

Expect [actual] (not) to be a float

Example:

expect(1.5).toBeFloat();
expect('1.5').toBeFloat();
expect(1).not.toBeFloat();
expect(1.0).not.toBeFloat();

toBeFrozen

Check that the tested object is frozen: an object is frozen if and only if it is not extensible, all its properties are non-configurable, and all its data properties (that is, properties which are not accessor properties with getter or setter components) are non-writable.

This function use internally Object.isFrozen (supported in Chrome, Firefox, Safari and IE >= 9). If Object.isFrozen is not supported, this function returns false.

Important: This function (as ES6 specification) treat primitive (null, undefined, numbers, strings and booleans) as frozen object.

See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isFrozen

Since

0.5.0

Parameters

No parameters

Message

Expect [actual] (not) to frozen

Example:

expect(null).toBeFrozen();
expect(undefined).toBeFrozen();
expect('').toBeFrozen();
expect(0).toBeFrozen();
expect(true).toBeFrozen();
expect(Object.freeze({})).toBeFrozen();
expect(Object.freeze([])).toBeFrozen();
expect({}).not.toBeFrozen();
expect([]).not.toBeFrozen();

toBeInRange

Check that the tested object is a number (strictly) greater than a lower bound and (strictly) less than a lower bound.

Since

0.1.0

Parameters

| Name | Type | Description | |------|------|-------------| | lower | number | The lower bound. | | upper | number | The upper bound. |

Message

Expect [actual] (not) to be between [lower] and [upper]

Example:

expect(2).toBeInRange(1, 3);
expect(1).not.toBeInRange(1, 3);
expect(3).not.toBeInRange(1, 3);

toBeInstanceOf

Check that the tested object is an instance of a given constructor.

Since

0.1.0

Parameters

| Name | Type | Description | |------|------|-------------| | ctor | * | Expected constructor. |

Message

Expect [actual] (not) to be an instance of [constructor]

Example:

expect(new Date()).toBeInstanceOf(Date);
expect('foo').toBeInstanceOf(String);

class Foo { }
expect(new Foo()).toBeInstanceOf(Foo);

toBeInteger

Check that the tested object is an integer value.

Note that for this matcher, an integer is a numeric value (see toBeNumeric matcher) that is not a float (a numeric value may be a number or a string containing a number).

JavaScript makes no distinction between integers and floats so both 1 and 1.0 are considered integers.

Since

0.1.0

Parameters

No parameters

Message

Expect [actual] (not) to be an integer

Example:

expect(1).toBeInteger();
expect(1.0).toBeInteger();
expect('1').toBeInteger();
expect('1.0').toBeInteger();
expect(1.5).not.toBeInteger();

toBeIterable

Check that the tested object is an iterable value.

An iterable value allows is an object that can define or customize its iteration behavior, such as what values are looped over in a for..of construct.

An iterable value may be:

  • An array.
  • A Map.
  • A Set.
  • An object that implement the @@iterator method.

Since

0.3.0

Parameters

| Name | Type | Description | |------|------|-------------| | Klass | * | Expected class. |

Message

Expect [actual] (not) to be iterable

Example:

expect([]).toBeIterable();
expect(new Map()).toBeIterable();
expect(new Set()).toBeIterable();

toBeNegative

Check that the tested object is a number strictly less than zero.

Since

0.1.0

Parameters

No parameters

Message

Expect [actual] (not) to be a negative number

Example:

expect(-1).toBeNegative();
expect(0).not.toBeNegative();
expect(1).not.toBeNegative();

toBeNil

Check that the tested object is nil (i.e null or undefined).

Since

0.3.0

Parameters

No parameters

Message

Expect [actual] (not) to be nil (null or undefined)

Example:

expect(null).toBeNil();
expect(undefined).toBeNil();
expect(void 0).toBeNil();
expect(false).not.toBeNil();
expect(0).not.toBeNil();
expect(NaN).not.toBeNil();

toBeNull

Check that tested object is null.

Since

0.1.0

Parameters

No parameters

Message

Expect [actual] (not) to be null

Example:

expect(null).toBeNull();
expect(undefined).not.toBeNull();
expect(false).not.toBeNull();

toBeNumeric

Check that the tested object is a numeric value. A numeric is something that contains a numeric value, regardless of its type (it can be a string containing a numeric value or a number).

Since

0.1.0

Parameters

No parameters

Message

Expect [actual] (not) to be a numeric value

Example:

expect(2).toBeNumeric();
expect(1.5).toBeNumeric();
expect('2').toBeNumeric();
expect('1.5').toBeNumeric();
expect('foo').not.toBeNumeric();

toBeOddNumber

Check that the tested object is an odd number.

Since

0.1.0

Parameters

No parameters

Message

Expect [actual] (not) to be an odd number

Example:

expect(1).toBeOddNumber();
expect(2).not.toBeOddNumber();
expect(0).not.toBeOddNumber();

toBeOk

Check that the tested object is not nil (i.e not null and not undefined).

Since

0.6.0

Parameters

No parameters

Message

Expect [actual] (not) to be ok (not null and not undefined)

Example:

expect(false).toBeOk();
expect(0).toBeOk();
expect(NaN).toBeOk();
expect(null).not.toBeOk();
expect(undefined).not.toBeOk();
expect(void 0).not.toBeOk();

toBeOneOf

Check that the tested object is strictly equal to one of the values in an array. Note that this matcher use the strict equality (===), please use toEqualOneOf for other equality check.

Since

0.5.0

Parameters

| Name | Type | Description | |------|------|-------------| | array | Array | The array that should contains the actual value. |

Message

Expect [actual] (not) to be one of [values]

Example:

expect(1).toBeOneOf([1, 2, 3]);
expect(10).not.toBeOneOf([1, 2, 3]);

toBePartiallyEqualTo

Check that the tested object is partially equals to a second one. Note that this matcher works fine with custom equality matchers.

Since

0.1.0

Parameters

| Name | Type | Description | |------|------|-------------| | other | Array.<*>,Object | The second object to use for equality. |

Message

Expect [actual] (not) to be partially equal to [other]

Example:

const a1 = { id: 1, foo: 'bar' };
const a2 = { id: 2, foo: 'bar' };

const b1 = { id: 1 };
const b2 = { id: 2 };

const c1 = { id: 2 };
const c2 = { id: 2 };

const array1 = [a1, a2];
const array2 = [b1, b2];
const array3 = [c1, c2];
expect(array1).toBePartiallyEqualTo(array2);
expect(array1).not.toBePartiallyEqualTo(array3);

toBePositive

Check that the tested object is a number strictly greater than zero.

Since

0.1.0

Parameters

No parameters

Message

Expect [actual] (not) to be a positive number

Example:

expect(1).toBePositive();
expect(0).not.toBePositive();
expect(-1).not.toBePositive();

toBeSameDay

Check that the tested object is the same day as an other date.

The tested date and the date to compare may be:

  • A date instance.
  • A timestamp.
  • A string that can be parsed with the Date constructor (i.e new Date('2016-01-01')).

Note: Using date strings should be avoided due to browser differences and inconsistencies.

Since

0.1.0

Parameters

| Name | Type | Description | |------|------|-------------| | day | Date,number,string | The other date. |

Message

Expect [actual] (not) the same day as [day]

Example:

const date1 = new Date(2014, 5, 5, 10, 0, 0, 0);
const date2 = new Date(2014, 5, 5, 15, 0, 0, 0);
const date3 = new Date(2014, 5, 6, 10, 0, 0, 0);

expect(date1).toBeSameDay(date2);
expect(date1).not.toBeSameDay(date3);

toBeSealed

Check that a given value is sealed: an object is sealed if it is not extensible and if all its properties are non-configurable and therefore not removable (but not necessarily non-writable).

This matcher use internally Object.isSealed (supported in Chrome, Firefox, Safari and IE >= 9). If Object.isSealed is not supported, the matcher will always treat objects and arrays as non sealed.

Important: This matcher (as ES6 specification) treat primitive (null, undefined, numbers, strings and booleans) as sealed object.

See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isSealed

Since

0.5.0

Parameters

No parameters

Message

Expect [actual] (not) to sealed

Example:

expect(null).toBeSealed();
expect(undefined).toBeSealed();
expect('').toBeSealed();
expect(0).toBeSealed();
expect(true).toBeSealed();
expect(Object.seal({})).toBeSealed();
expect(Object.seal([])).toBeSealed();
expect({}).not.toBeSealed();
expect([]).not.toBeSealed();

toBeSorted

Check that the tested object is an array and is sorted (i.e for each elements in the array, array[i - 1] <= array[i]).

A custom comparator can be specified as parameter:

  • Takes values to compare as arguments.
  • Must return a number:
    • Less than zero if first argument is less than the second.
    • Greater than zero if first argument is greater than the second.
    • Zero if both parameters are "equivalent".

Since

0.1.0

Parameters

| Name | Type | Description | |------|------|-------------| | comparator | function | Comparator function (optional). |

Message

Expect [actual] (not) to be sorted

Example:

expect([0, 1, 2, 3]).toBeSorted();
expect(['bar', 'foo']).toBeSorted();
expect([false, false, true, true]).toBeSorted();
expect([{ id: 1 }, { id: 2 }, { id: 3 }]).toBeSorted((a, b) => a.id - b.id);
expect([1, 0, 2, 3]).not.toBeSorted();

toBeToday

Check that the tested object is the same day as now (i.e Date.now()).

The tested date may be:

  • A date instance.
  • A timestamp.
  • A string that can be parsed with the Date constructor (i.e new Date('2016-01-01')).

Note: Using date strings should be avoided due to browser differences and inconsistencies.

Since

0.1.0

Parameters

No parameters

Message

Expect [actual] (not) to be today

Example:

const date1 = new Date();
const date2 = new Date();
date2.setDate(date1.getDate() - 1);

expect(date1).toBeToday();
expect(date2).not.toBeToday();

toBeTrue

Check that the tested object is strictly equal to true.

Since

0.1.0

Parameters

No parameters

Message

Expect [actual] (not) to be true

Example:

expect(true).toBeTrue();
expect(false).not.toBeTrue();
expect(1).not.toBeTrue();

toBeZero

Check that the tested object is a number strictly equal to zero.

Since

0.1.0

Parameters

No parameters

Message

Expect [actual] (not) to be zero

Example:

expect(0).toBeZero();
expect(1).not.toBeZero();
expect('0').not.toBeZero();

toContainsDistinctValues

Check that the tested object is an array containing only distinct values. The tested object may be an array or any iterable object (i.e that can be traversed with the for..of loop).

Note that this matcher works fine with custom equality matchers.

Since

0.1.0

Parameters

No parameters

Message

Expect [actual] (not) to contains only distinct values

Example:

expect([0, 1, 2, 3]).toContainsDistinctValues();
expect([0, 1, 2, 3, 0]).not.toContainsDistinctValues();

toContainsOnlyFalsyValues

Check that the tested object contains only falsy values. The tested object may be an array or any iterable object (i.e that can be traversed with the for..of loop).

Note that this matcher works fine with custom equality matchers.

Since

0.1.0

Parameters

No parameters

Message

Expect [actual] (not) to contains only falsy values

Example:

expect([0, false, null, undefined, '']).toContainsOnlyFalsyValues();

expect([1, false, null, undefined, '']).not.toContainsOnlyFalsyValues();
expect([0, true, null, undefined, '']).not.toContainsOnlyFalsyValues();
expect([0, false, {}, undefined, '']).not.toContainsOnlyFalsyValues();
expect([0, false, null, [], '']).not.toContainsOnlyFalsyValues();
expect([0, false, null, undefined, 'foo']).not.toContainsOnlyFalsyValues();

toContainsOnlyTruthyValues

Check that the tested object contains only truthy values. The tested object may be an array or any iterable object (i.e that can be traversed with the for..of loop).

Note that this matcher works fine with custom equality matchers.

Since

0.1.0

Parameters

No parameters

Message

Expect [actual] (not) to contains only truthy values.

Example:

expect([1, 2, true, 'foo', {}, []]).toContainsOnlyTruthyValues();

expect([1, 2, false, 'foo', {}, []]).not.toContainsOnlyTruthyValues();
expect([1, 2, true, '', {}, []]).not.toContainsOnlyTruthyValues();
expect([0, 2, true, 'foo', {}, []]).not.toContainsOnlyTruthyValues();

toEndWith

Check that the tested object is a string and end with an expected suffix.

Since

0.1.0

Parameters

| Name | Type | Description | |------|------|-------------| | suffix | string | The suffix to look for. |

Message

Expect [actual] (not) to end with [suffix]

Example:

expect('foo').toEndWith('o');
expect('foo').toEndWith('oo');
expect('foo').toEndWith('foo');
expect('foo').not.toEndWith('bar');

toEqualIgnoringCase

Check that the tested object is a string equal to an other string: comparison is case-insensitive.

Since

0.1.0

Parameters

| Name | Type | Description | |------|------|-------------| | other | string | Other string to compare. |

Message

Expect [actual] (not) to be equal to [other] (case insensitive)

Example:

expect('foo').toEqualIgnoringCase('foo');
expect('foo').toEqualIgnoringCase('FOO');
expect('foo').not.toEqualIgnoringCase('bar');

toEqualOneOf

Check that the tested object is equal to one of the values in an array. Note that this matcher use the deep equality check and also works with custom equality tester. For strict comparison (===) please use toBeOneOf.

Since

0.5.0

Parameters

| Name | Type | Description | |------|------|-------------| | array | Array | The array that should contains the actual value. |

Message

Expect [actual] (not) to equal one of [values]

Example:

expect(1).toEqualOneOf([1, 2, 3]);
expect({id: 1}).toEqualOneOf([{id: 1}]);
expect(10).not.toEqualOneOf([1, 2, 3]);

toHaveBeenCalledOnceWith

Check that the tested object is a spy that has been called once (and exactly once) with expected arguments.

Since

0.1.0

Parameters

| Name | Type | Description | |------|------|-------------| | args | ...* | Expected call arguments. |

Message

Expect [actual] to have been called once but was called [count] time(s) Expect [actual] to have been called once but was called [count] time(s) with different arguments

Example:

const spy = jasmine.createSpy('foo');
expect(spy).not.toHaveBeenCalledOnce();

spy('foo');
expect(spy).toHaveBeenCalledOnceWith('foo');
expect(spy).not.toHaveBeenCalledOnceWith('bar');

toHaveBeenCalledOnce

Check that the tested object is a spy that has been called once (and only once).

Since

0.1.0

Parameters

No parameters

Message

Expect [actual] (not) to have been called once but was called [count] time(s)

Example:

const spy = jasmine.createSpy('foo');
expect(spy).not.toHaveBeenCalledOnce();

spy();
expect(spy).toHaveBeenCalledOnce();

spy();
expect(spy).not.toHaveBeenCalledOnce();

toHaveFunctions

Check that actual object contains all given expected functions.

Since

0.1.0

Parameters

| Name | Type | Description | |------|------|-------------| | methods | ...string | List of methods to look for. |

Message

Expect [actual] (not) to contain functions [methods]

Example:

const foo = jasmine.createSpy('foo');
const bar = jasmine.createSpy('bar');
const obj = { id: 1, name: 'foo', foo, bar };

expect(obj).toHaveFunctions('foo', 'bar');
expect(obj).not.toHaveFunctions('id', 'name');

toHaveKeys

Check that actual object contains all given expected keys.

Since

0.1.0

Parameters

| Name | Type | Description | |------|------|-------------| | expectedKeys | ...string | Keys to look for in tested object. |

Message

Expect [actual] (not) to have keys [expectedKeys]

Example:

const obj = { id: 1, name: 'foo' };
expect(obj).toHaveKeys('id', 'name');
expect(obj).not.toHaveKeys('foo', 'bar');

toHaveLength

Check that tested object has a length property with expected value.

Since

0.1.0

Parameters

| Name | Type | Description | |------|------|-------------| | expectedLength | number | The expected length value. |

Message

Expect length of [actual] (not) to be [expectedLength]

Example:

expect([]).toHaveLength(0);
expect([0, 1, 2]).toHaveLength(3);
expect('').toHaveLength(0);
expect('foo').toHaveLength(3);

toHaveSameLengthAs

Check that tested object has the same length as an other value with length property.

Since

0.1.0

Parameters

| Name | Type | Description | |------|------|-------------| | expected | Array.<*> | The other array. |

Message

Expect [actual] (not) to have same length as [expected]

Example:

expect([]).toHaveSameLengthAs('');
expect(['f', 'o', 'o']).toHaveSameLengthAs('foo');
expect('').toHaveSameLengthAs([]);
expect('foo').toHaveSameLengthAs(['f', 'o', 'o']);

toHaveSameSizeAs

Check that tested object has the same size as an other one.

A size may be computed from:

  • An array (or an array-like object) with the length property.
  • A Map or a Set using its size property.
  • Any iterable object (using a for..of loop).
  • An object (number of own enumerable properties).

Since

0.1.0

Parameters

| Name | Type | Description | |------|------|-------------| | expected | * | The other object (or array, or array-like object). |

Message

Expect [actual] to have same size as [expected]

Example:

expect('foo').toHaveSameSizeAs('foo');
expect('bar').toHaveSameSizeAs({ b: 1, a: 1, r: 1 });
expect([ 'foo', 'bar' ]).toHaveSameSizeAs(new Set(['foo', 'bar']));

toHaveSize

Check that tested object has an expected size.

A size may be computed from:

  • An array (or an array-like object) with the length property.
  • A Map or a Set using its size property.
  • Any iterable object (using a for..of loop).
  • An object (number of own enumerable properties).

Since

0.1.0

Parameters

| Name | Type | Description | |------|------|-------------| | expectedSize | number | The expected size. |

Message

Expect size of [actual] (not) to be [expectedSize]

Example:

expect([1, 2, 3]).toHaveSize(3);
expect('foo bar').toHaveSize(7);
expect(new Set([0, 1, 2])).toHaveSize(3);
expect({ foo: 'bar' }).toHaveSize(1);

toHaveSome

Verifies that the tested object satisfies a predicate function for at at least one element in the collection.

The collection may be an array, a set, a map or any iterable object.

The predicate function is executed with three arguments:

  • The value being iterated.
  • The key of the value being iterated.
  • The collection being iterated.

Important: Note that the key may be different with arrays and map/set:

  • With an array or an iterable object, the key will be the index of the value being traversed.
  • With a map, the key will be the index value of the value being traversed.
  • With a set, the key will be the same value being traversed (since a Set does not have any keys).

See the documentation of the forEach functions of Map and Set structure:

  • https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Set/forEach
  • https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Map

This matcher may take an optional first argument: the error message that will be displayed if the matcher does not pass.

Since

0.1.0

Parameters

| Name | Type | Description | |------|------|-------------| | msg | string | Custom error message (optional). | | iterator | function | Predicate function. |

Message

Expect [actual] (not) to have at least one element that verify condition Expect [actual] (not) to have at least one element that verify "[message]"

Example:

expect([1, 2, 3]).toHaveSome(x => x % 2 === 0);
expect([1, 3, 5, 7]).not.toHaveSome(x => x % 2 === 0);

toHaveValues

Check that the tested object contains expected values (the key is not checked, only the value).

Since

0.1.0

Parameters

| Name | Type | Description | |------|------|-------------| | expectedValues | ...* | The values to look for. |

Message

Expect [actual] (not) to have values [values]

Example:

const obj = { id: 1, name: 'foo', array: [1, 2, 3] };
expect(obj).toHaveValues(1, 'foo', [1, 2, 3]);
expect(obj).not.toHaveValues(2, 'bar');
expect(obj).not.toHaveValues([ 1, 2, 3, 4 ]);

toStartWith

Check that the tested object is a string and start with an expected prefix.

Since

0.1.0

Parameters

| Name | Type | Description | |------|------|-------------| | prefix | string | The prefix to look for. |

Message

Expect [actual] (not) to start with [prefix]

Example:

expect('foo').toStartWith('f');
expect('foo').toStartWith('fo');
expect('foo').toStartWith('foo');
expect('foo').not.toStartWith('bar');

toVerify

Check that the tested object satisfies a given predicate. The collection may be an array, a set, a map or any iterable object.

The predicate function is executed with three arguments:

  • The value being iterated.
  • The key of the value being iterated.
  • The collection being iterated.

Important: Note that the key may be different with arrays and map/set:

  • With an array or an iterable object, the key will be the index of the value being traversed.
  • With a map, the key will be the index value of the value being traversed.
  • With a set, the key will be the same value being traversed (since a Set does not have any keys).

See the documentation of the forEach functions of Map and Set structure:

  • https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Set/forEach
  • https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Map

This matcher may take an optional first argument: the error message that will be displayed if the matcher does not pass.

Since

0.1.0

Parameters

| Name | Type | Description | |------|------|-------------| | msg | string | Error message thrown when the test fail (optional). | | iterator | function | Predicate function. |

Message

Expect [actual] (not) to verify condition Expect [actual] (not) to verify "[message]"

Example:

expect([2, 4, 6, 8]).toVerify(x => x % 2 === 0);
expect([2, 4, 6, 8, 9]).not.toVerify(x => x % 2 === 0);

Licence

MIT License (MIT)

Contributing

If you think some matchers are missing or error messages are not useful enough, feel free to contribute and submit an issue or a pull request.

Thanks to @geoffdutton for his contribution!