mobify-chai-assertions1.2.0 • Public • Published
Custom Assertions For Chai
For our integration testing, we have made our own Chai's custom assertions. This is where we define, collect, and present them in a plugin form.
How To Use the Plugin
To use the plugin, simply call Chai's
var chai = ;var customAssertions = ;chai;
New Assertion Methods
The plugin extends Chai by adding the following methods. Feel free to use either the
expect styles. They work with any of the chains listed in the Chai API documentation.
(Note: since Chai's API supports only the
expect style, we implemented these methods for the
expect style first and then the
assert style as a wrapper)
All of these assertions can take an optional
msg parameter to output a custom error message on test failure.
[ ] syntax denotes an optional parameter.
element([msg]) / elements([msg])
// Asserts that it is a Zepto/jQuery element.tobeanelement;// Takes an optional error message that is displayed on failure.// Default error message is "Must be a Zepto/jQuery object"tobeelements'failure: it is not a Zepto/jQuery object';//Can be chained with .present and .count (see below)
// Asserts that the length is at least (>=) than num.// Default is 1.// Can be used with all types of expressions.tobepresent;var items = 1 2 3;tobepresent;tobe;tobe;// Can be used in a chain:// Zepto/jQuery object has length at least 1tohaveelementspresent;// Can be negated// Length is 0tohaveelementsnotpresent;
// Asserts that it has a specified lengthvar items = 1 2 3;tohave;tohave;// Asserts that a jQuery/Zepto object has a specified lengthvar $images = ;tohaveelements;
// Asserts that there exists such element on pageasserttohave// Asserts that there are 4 or more such elementsasserttohave// The negationasserttonothave
// Asserts that there are exactly 3 of such elementsasserttohave// The negationasserttonothave
// Asserts that this object has the keys 'apple' and 'google'asserttohave
Works with a collection of things.
// Asserts that this collection (e.g. an array) has at least 1 item in ittohaveitems;tohave;
// Asserts that this collection (e.g. an array) has at least 1 item in itasserttonotbeempty