Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jasmine-expect

Package Overview
Dependencies
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jasmine-expect

Additional matchers for the Jasmine BDD JavaScript testing library

  • 3.5.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
38K
decreased by-18.73%
Maintainers
1
Weekly downloads
 
Created
Source

Jasmine-Matchers

NPM version NPM downloads Build Status Dependency Status Join the chat at https://gitter.im/JamieMason/Jasmine-Matchers Code Climate Analytics

WhatA huge library of test matchers for a range of common use-cases, compatible with all versions of Jasmine and Jest.
WhyCustom Matchers make tests easier to read and produce relevant and useful messages when they fail.
HowBy avoiding vague messages such as "expected false to be true" in favour of useful cues such as "expected 3 to be even number" and avoiding implementation noise such as expect(cycleWheels % 2 === 0).toEqual(true) in favour of simply stating that you expect(cycleWheels).toBeEvenNumber().

Jasmine Matchers is written using the add-matchers library. If you have some useful matchers of your own that you could share with other Jest and Jasmine users, please give it a try.

Sponsors

Sponsored by BrowserStack

Contents

Installation

PlatformInstructions
npmnpm install jasmine-expect --save-dev
Bowerbower install jasmine-expect --save-dev
ManualDownloads are available on the releases page.

Matchers

Jasmine's Default Matchers

The Jasmine testing framework from Pivotal Labs comes with this default set of matchers;

expect(array).toContain(member);
expect(fn).toThrow(string);
expect(fn).toThrowError(string);
expect(instance).toBe(instance);
expect(mixed).toBeDefined();
expect(mixed).toBeFalsy();
expect(mixed).toBeNull();
expect(mixed).toBeTruthy();
expect(mixed).toBeUndefined();
expect(mixed).toEqual(mixed);
expect(mixed).toMatch(pattern);
expect(number).toBeCloseTo(number, decimalPlaces);
expect(number).toBeGreaterThan(number);
expect(number).toBeLessThan(number);
expect(number).toBeNaN();
expect(spy).toHaveBeenCalled();
expect(spy).toHaveBeenCalledTimes(number);
expect(spy).toHaveBeenCalledWith(...arguments);

Arrays

MatcherExample
toBeArrayexpect(array).toBeArray();
toBeArrayOfBooleansexpect(array).toBeArrayOfBooleans();
toBeArrayOfNumbersexpect(array).toBeArrayOfNumbers();
toBeArrayOfObjectsexpect(array).toBeArrayOfObjects();
toBeArrayOfSizeexpect(array).toBeArrayOfSize(number);
toBeArrayOfStringsexpect(array).toBeArrayOfStrings();
toBeEmptyArrayexpect(array).toBeEmptyArray();
toBeNonEmptyArrayexpect(array).toBeNonEmptyArray();

Booleans

MatcherExample
toBeBooleanexpect(boolean).toBeBoolean();
toBeFalseexpect(boolean).toBeFalse();
toBeTrueexpect(boolean).toBeTrue();

Dates

MatcherExample
toBeAfterexpect(date).toBeAfter(date);
toBeBeforeexpect(date).toBeBefore(date);
toBeDateexpect(date).toBeDate();
toBeValidDateexpect(date).toBeValidDate()

Functions and Errors

MatcherExample
toBeFunctionexpect(fn).toBeFunction();
toThrowAnyErrorexpect(fn).toThrowAnyError();
toThrowErrorOfTypeexpect(fn).toThrowErrorOfType(constructorName);

Numbers

MatcherExample
toBeCalculableexpect(mixed).toBeCalculable();
toBeEvenNumberexpect(number).toBeEvenNumber();
toBeGreaterThanOrEqualToexpect(number).toBeGreaterThanOrEqualTo(number);
toBeLessThanOrEqualToexpect(number).toBeLessThanOrEqualTo(number);
toBeNearexpect(number).toBeNear(value, epsilon)
toBeNumberexpect(number).toBeNumber();
toBeOddNumberexpect(number).toBeOddNumber();
toBeWholeNumberexpect(number).toBeWholeNumber();
toBeWithinRangeexpect(number).toBeWithinRange(floor, ceiling);

Strings

MatcherExample
toBeEmptyStringexpect(string).toBeEmptyString();
toBeHtmlStringexpect(string).toBeHtmlString();
toBeIso8601expect(string).toBeIso8601();
toBeJsonStringexpect(string).toBeJsonString();
toBeLongerThanexpect(string).toBeLongerThan();
toBeNonEmptyStringexpect(string).toBeNonEmptyString();
toBeSameLengthAsexpect(string).toBeSameLengthAs();
toBeShorterThanexpect(string).toBeShorterThan();
toBeStringexpect(string).toBeString();
toBeWhitespaceexpect(string).toBeWhitespace();
toEndWithexpect(string).toEndWith(string);
toStartWithexpect(string).toStartWith(string);

Objects

MatcherExample
toBeEmptyObjectexpect(object).toBeEmptyObject();
toBeNonEmptyObjectexpect(object).toBeNonEmptyObject();
toBeObjectexpect(object).toBeObject();

Regular Expressions

MatcherExample
toBeRegExpexpect(regexp).toBeRegExp();

Members, Properties, Methods

MatcherExample
toHaveArrayexpect(object).toHaveArray(memberName);
toHaveArrayOfBooleansexpect(object).toHaveArrayOfBooleans(memberName);
toHaveArrayOfNumbersexpect(object).toHaveArrayOfNumbers(memberName);
toHaveArrayOfObjectsexpect(object).toHaveArrayOfObjects(memberName);
toHaveArrayOfSizeexpect(object).toHaveArrayOfSize(memberName, size);
toHaveArrayOfStringsexpect(object).toHaveArrayOfStrings(memberName);
toHaveBooleanexpect(object).toHaveBoolean(memberName);
toHaveCalculableexpect(object).toHaveCalculable(memberName);
toHaveDateexpect(object).toHaveDate(memberName);
toHaveDateAfterexpect(object).toHaveDateAfter(memberName, date);
toHaveDateBeforeexpect(object).toHaveDateBefore(memberName, date);
toHaveEmptyArrayexpect(object).toHaveEmptyArray(memberName);
toHaveEmptyObjectexpect(object).toHaveEmptyObject(memberName);
toHaveEmptyStringexpect(object).toHaveEmptyString(memberName);
toHaveEvenNumberexpect(object).toHaveEvenNumber(memberName);
toHaveFalseexpect(object).toHaveFalse(memberName);
toHaveHtmlStringexpect(object).toHaveHtmlString(memberName);
toHaveIso8601expect(object).toHaveIso8601(memberName);
toHaveJsonStringexpect(object).toHaveJsonString(memberName);
toHaveMemberexpect(object).toHaveMember(memberName);
toHaveMethodexpect(object).toHaveMethod(memberName);
toHaveNonEmptyArrayexpect(object).toHaveNonEmptyArray(memberName);
toHaveNonEmptyObjectexpect(object).toHaveNonEmptyObject(memberName);
toHaveNonEmptyStringexpect(object).toHaveNonEmptyString(memberName);
toHaveNumberexpect(object).toHaveNumber(memberName);
toHaveNumberWithinRangeexpect(object).toHaveNumberWithinRange(memberName, floor, ceiling);
toHaveObjectexpect(object).toHaveObject(memberName);
toHaveOddNumberexpect(object).toHaveOddNumber(memberName);
toHaveStringexpect(object).toHaveString(memberName);
toHaveStringLongerThanexpect(object).toHaveStringLongerThan(memberName, string);
toHaveStringSameLengthAsexpect(object).toHaveStringSameLengthAs(memberName, string);
toHaveStringShorterThanexpect(object).toHaveStringShorterThan(memberName, string);
toHaveTrueexpect(object).toHaveTrue(memberName);
toHaveWhitespaceStringexpect(object).toHaveWhitespaceString(memberName);
toHaveWholeNumberexpect(object).toHaveWholeNumber(memberName);

Asymmetric Matchers

Jasmine's Default Asymmetric Matchers

The Jasmine testing framework from Pivotal Labs comes with this default set of asymmetric matchers;

jasmine.any(Constructor);
jasmine.anything(mixed);
jasmine.arrayContaining(mixed);
jasmine.objectContaining(mixed);
jasmine.stringMatching(pattern);

Arrays

MatcherExample
any.arrayOfBooleansexpect(spy).toHaveBeenCalledWith(any.arrayOfBooleans());
any.arrayOfNumbersexpect(spy).toHaveBeenCalledWith(any.arrayOfNumbers());
any.arrayOfObjectsexpect(spy).toHaveBeenCalledWith(any.arrayOfObjects());
any.arrayOfSizeexpect(spy).toHaveBeenCalledWith(any.arrayOfSize(number));
any.arrayOfStringsexpect(spy).toHaveBeenCalledWith(any.arrayOfStrings());
any.emptyArrayexpect(spy).toHaveBeenCalledWith(any.emptyArray());
any.nonEmptyArrayexpect(spy).toHaveBeenCalledWith(any.nonEmptyArray());

Dates

MatcherExample
any.afterexpect(spy).toHaveBeenCalledWith(any.after(date));
any.beforeexpect(spy).toHaveBeenCalledWith(any.before(date));

Numbers

MatcherExample
any.calculableexpect(spy).toHaveBeenCalledWith(any.calculable());
any.evenNumberexpect(spy).toHaveBeenCalledWith(any.evenNumber());
any.greaterThanOrEqualToexpect(spy).toHaveBeenCalledWith(any.greaterThanOrEqualTo(number));
any.lessThanOrEqualToexpect(spy).toHaveBeenCalledWith(any.lessThanOrEqualTo(number));
any.oddNumberexpect(spy).toHaveBeenCalledWith(any.oddNumber());
any.wholeNumberexpect(spy).toHaveBeenCalledWith(any.wholeNumber());
any.withinRangeexpect(spy).toHaveBeenCalledWith(any.withinRange(floor, ceiling));

Strings

MatcherExample
any.endingWithexpect(spy).toHaveBeenCalledWith(any.endingWith(string));
any.iso8601expect(spy).toHaveBeenCalledWith(any.iso8601());
any.jsonStringexpect(spy).toHaveBeenCalledWith(any.jsonString());
any.longerThanexpect(spy).toHaveBeenCalledWith(any.longerThan(string));
any.nonEmptyStringexpect(spy).toHaveBeenCalledWith(any.nonEmptyString());
any.sameLengthAsexpect(spy).toHaveBeenCalledWith(any.sameLengthAs(string));
any.shorterThanexpect(spy).toHaveBeenCalledWith(any.shorterThan(string));
any.startingWithexpect(spy).toHaveBeenCalledWith(any.startingWith(string));
any.whitespaceexpect(spy).toHaveBeenCalledWith(any.whitespace());

Objects

MatcherExample
any.emptyObjectexpect(spy).toHaveBeenCalledWith(any.emptyObject());
any.nonEmptyObjectexpect(spy).toHaveBeenCalledWith(any.nonEmptyObject());

Regular Expressions

MatcherExample
any.regExpexpect(spy).toHaveBeenCalledWith(any.regExp());

Integration

Browser

Embed jasmine-matchers.js after Jasmine but before your tests.

Jest

Include the following in your package.json;

"unmockedModulePathPatterns": ["jasmine-expect"]

And the following at the top of your test suite;

import JasmineExpect from 'jasmine-expect';

Karma

Integration is easy with the karma-jasmine-matchers plugin.

Node.js

Use the Jasmine CLI and include the path to where Jasmine Matchers is installed in the helpers array of your spec/support/jasmine.json.

{
  "spec_dir": "spec",
  "spec_files": ["../src/**/*.spec.js"],
  "helpers": ["../node_modules/jasmine-expect/index.js"],
  "stopSpecOnExpectationFailure": false,
  "random": false
}

Sublime Text

Jasmine-Matchers-Snippets or Jasmine-Matchers-ES6-Snippets can be installed with Package Control to ease development with Jasmine Matchers in Sublime Text.

Tern

There is a Plugin for Tern to auto-complete matchers in your Text Editor.

Browser Support

Jasmine-Matchers is tested on Travis CI and BrowserStack against the following environments.

BrowserVersion Range
Android4.0 - 5.1
Chrome26 - 52
Firefox4 - 48
Internet Explorer9 - Edge
iOS6.0 - 9.3*
Opera11 - 12
Safari6 - 9*

* Safari 5.1 and iOS 5.1 are actually fully supported except for toBeIso8601.

Keywords

FAQs

Package last updated on 18 Jan 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc