A set of custom matchers to be used in your Jasmine specs
Heavily inspired by jasmine-jquery it provides a simple, lean alternative for a smaller set of features
Either:
- Simply download jasmineMatchers.min.js from here and include it in your Jasmine's test runner file. Remember to also include jQuery
- Use Bower
bower install jasmineMatchers
-
toBeExtensible()
-
Check that the tested object is extensible
Objects are extensible by default. An object can be marked as non-extensible using:
Object.preventExtensions()
,Object.seal()
,Object.freeze()
Important: This matcher consider primitives (
null
,undefined
, numbers, strings and booleans) as non extensible objects
-
-
toBeFalse()
- Check that the tested object is strictly equal to
false
- Check that the tested object is strictly equal to
-
toBeFrozen()
-
Check that the tested object is frozen
Important: This matcher consider primitives (
null
,undefined
, numbers, strings and booleans) as non frozen objects
-
-
toBeInstanceOf(object)
- Check that the prototype property of the tested object appears anywhere in the prototype chain of the given object
var Car = function(make, model, year){ this.make = make; this.model = model; this.year = year; }; var auto = new Car("NSU", "Prinz", 1958); expect(auto).toBeInstanceOf(Car); expect(auto).toBeInstanceOf(Object);
- Check that the prototype property of the tested object appears anywhere in the prototype chain of the given object
-
toBeSealed()
-
Check that the tested object is sealed
Important: This matcher consider primitives (
null
,undefined
, numbers, strings and booleans) as non sealed objects
-
-
toBeTrue()
- Check that the tested object is strictly equal to
true
- Check that the tested object is strictly equal to
-
toHaveProperty(propertyName, [expectedValue])
- Check an object for the given property, expectedValue is optional, if omitted it will check only if property exists
-
toHaveReadonlyProperty(propertyName)
- Check if the given property of the object is read-only
var myObj = {}; Object.defineProperty(myObj, "name", { enumerable: true, configurable: false, writable: false, value: "Ciccio" }); expect(myObj).toHaveReadonlyProperty("name"); var frozenObj = {name: "Ciccio"}; Object.freeze(frozenObj); expect(frozenObj).toHaveReadonlyProperty("name"); var sealedObj = {name: "Ciccio"}; Object.seal(sealedObj); expect(sealedObj).not.toHaveReadonlyProperty("name");
- Check if the given property of the object is read-only
-
toMatchDuckType(duckType, [matchType])
- Use duck typing for type checking
- matchType is an optional boolean argument, set it to false to not compare type of properties using $.type()
expect({name: "Ciccio"}).toMatchDuckType({name: "Duck"})
expect({name: "Ciccio"}).toMatchDuckType({})
expect({name: "Ciccio"}).not.toMatchDuckType({code: "007"})
expect({name: "Ciccio"}).not.toMatchDuckType({name: []})
expect({name: "Ciccio"}).toMatchDuckType({name: []}, false)
All the matchers below accept either a jQuery object or an HTMLElement
-
toBeChecked()
expect($('<input type="checkbox" checked="checked">')).toBeChecked()
expect(document.getElementById("checked")).toBeChecked()
-
toBeDisabled()
expect('<input type="text" disabled="disabled">').toBeDisabled()
expect(document.getElementById("disable")).toBeChecked()
-
toBeEmpty()
- Pass if the tested object contains no child DOM element/s or text
-
toContainElement(element)
- Pass if the tested object contains the DOM element provided
-
toContainElementsMatching(selector, [cardinality])
- Pass if the tested object contains n elements matching the given selector, where n is exactly the provided cardinality.
- If the cardinality is not defined, pass if the tested object contains at least one element matching the given selector.
-
toBeMatchedBy(selector)
- Check to see if the tested object matches the given selector
expect($('<div class='test'></div>')).toBeMatchedBy('.test')
-
toBeSelected()
expect($('<option selected="selected"></option>')).toBeSelected()
-
toBeVisible()
- Elements are considered visible if they consume space in the document. Even if they have both width and height set to zero
- In order for this matcher to work, the tested object must be part of the current document DOM
-
toHaveAttr(attributeName, [expectedValue])
- expectedValue is optional, if omitted it will check only if attribute exists
-
toHaveClass(className)
expect($('<div class="test"></div>')).toHaveClass("test")
Important: unlike the matcher introduced in Jasmine 3.x, this works both on a jQuery object or an HTMLElement
-
toHaveCss(propertyName, [expectedValue])
- expectedValue is optional, if omitted it will check only if the computed style property exists
-
toHaveProp(propertyName, [expectedValue])
- Use jQuery.prop() to check for boolean attributes expectedValue is optional, if omitted it will check only if attribute exists (attributes vs. properties)
Take a look at jasmineFixtures