Jasmine-Matchers
A huge library of test assertion matchers for a range of common use-cases, to improve the readability of tests written using the Jasmine testing framework from Pivotal Labs.
- Make failing tests easier to debug by avoiding vague messages such as "expected false to be true" in favour of useful cues such as "expected 3 to be even number".
- Make tests easier to read by avoiding implementation noise such as
expect(cycleWheels % 2 === 0).toEqual(true)
in favour of simply stating that you expect(cycleWheels).toBeEvenNumber()
.
Contents
Installation
Platform | Instructions |
---|
npm | npm install jasmine-expect --save-dev |
Bower | bower install jasmine-expect --save-dev |
Manual | Downloads 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
Booleans
Dates
Functions and Errors
Numbers
Strings
Objects
Members, Properties, Methods
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
Dates
Matcher | Example |
---|
any.after | expect(spy).toHaveBeenCalledWith(any.after(date)); |
any.before | expect(spy).toHaveBeenCalledWith(any.before(date)); |
Numbers
Strings
Objects
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.
Browser | Version Range |
---|
Android | 4.0 - 5.1 |
Chrome | 26 - 52 |
Firefox | 4 - 48 |
Internet Explorer | 9 - Edge |
iOS | 6.0 - 9.3* |
Opera | 11 - 12 |
Safari | 6 - 9* |
* Safari 5.1 and iOS 5.1 are actually fully supported except for toBeIso8601
.