jQuery matchers and fixture loader for Jasmine framework
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Comprehensive set of custom Jasmine matchers specifically designed for testing jQuery DOM elements. These matchers provide expressive assertions for element states, content, attributes, CSS properties, and form interactions.
Checks if element is visible on the page.
/**
* Checks if element is visible (has width/height > 0 and not hidden)
* Elements are visible if they consume space in the document
*/
expect(element).toBeVisible();Usage Example:
expect($('#modal')).toBeVisible();
expect($('.notification')).not.toBeVisible();Checks if element is hidden from view.
/**
* Checks if element is hidden via CSS display:none, visibility:hidden,
* width/height of 0, or hidden form input type
*/
expect(element).toBeHidden();Checks if form element is checked (checkboxes, radio buttons).
/**
* Checks if form element has checked attribute/property
* Only applies to elements that can be checked
*/
expect(element).toBeChecked();Usage Example:
expect($('input[type="checkbox"]')).toBeChecked();
expect($('input[name="gender"][value="male"]')).not.toBeChecked();Checks if option element is selected.
/**
* Checks if option element has selected attribute/property
* Only applies to <option> elements
*/
expect(element).toBeSelected();Checks if form element is disabled.
/**
* Checks if element has disabled attribute/property
*/
expect(element).toBeDisabled();Checks if element currently has focus.
/**
* Checks if element is the currently active/focused element
*/
expect(element).toBeFocused();Usage Example:
$('#username').focus();
expect($('#username')).toBeFocused();Checks if element has no child elements or text content.
/**
* Checks if element is empty (no child DOM elements or text)
*/
expect(element).toBeEmpty();Checks if element is attached to the document DOM.
/**
* Checks if element is present in the document DOM tree
*/
expect(element).toBeInDOM();Checks if element exists (in or out of DOM).
/**
* Checks if jQuery selector matches any elements
*/
expect(element).toExist();Checks if element has specified CSS class.
/**
* Checks if element has the specified CSS class
* @param className - CSS class name to check for
*/
expect(element).toHaveClass(className);Usage Example:
expect($('#alert')).toHaveClass('error');
expect($('.button')).toHaveClass('btn-primary');Checks element attribute value.
/**
* Checks if element has specified attribute with optional value
* @param attributeName - HTML attribute name
* @param attributeValue - Expected attribute value (optional)
*/
expect(element).toHaveAttr(attributeName, attributeValue);Usage Examples:
// Check attribute exists
expect($('input')).toHaveAttr('required');
// Check attribute value
expect($('img')).toHaveAttr('src', '/images/logo.png');
expect($('a')).toHaveAttr('href', 'https://example.com');Checks element property value.
/**
* Checks if element has specified property with optional value
* @param propertyName - DOM property name
* @param propertyValue - Expected property value (optional)
*/
expect(element).toHaveProp(propertyName, propertyValue);Usage Example:
expect($('input[type="checkbox"]')).toHaveProp('checked', true);
expect($('select')).toHaveProp('selectedIndex', 2);Checks element ID attribute.
/**
* Checks if element has specified ID
* @param id - Expected ID value
*/
expect(element).toHaveId(id);Checks element text content.
/**
* Checks if element text matches expected value or regex pattern
* @param text - Expected text string or RegExp pattern
*/
expect(element).toHaveText(text);Usage Examples:
expect($('#message')).toHaveText('Welcome!');
expect($('.error')).toHaveText(/invalid/i);Checks element HTML content.
/**
* Checks if element innerHTML matches expected HTML
* @param html - Expected HTML string
*/
expect(element).toHaveHtml(html);Checks form element value.
/**
* Checks if form element has specified value
* @param value - Expected form value
*/
expect(element).toHaveValue(value);Usage Example:
expect($('input[name="email"]')).toHaveValue('user@example.com');
expect($('textarea')).toHaveValue('');Checks element data attribute or jQuery data.
/**
* Checks if element has specified data attribute with optional value
* @param key - Data attribute key
* @param value - Expected data value (optional)
*/
expect(element).toHaveData(key, value);Usage Example:
expect($('#user')).toHaveData('id', 123);
expect($('.widget')).toHaveData('config');Checks element CSS properties.
/**
* Checks if element has specified CSS properties and values
* @param cssProperties - Object with CSS property-value pairs
*/
expect(element).toHaveCss(cssProperties);Usage Examples:
expect($('#modal')).toHaveCss({display: 'block'});
expect($('.button')).toHaveCss({
'background-color': 'rgb(255, 0, 0)',
'font-size': '14px'
});Checks jQuery collection length.
/**
* Checks if jQuery collection has specified number of elements
* @param length - Expected number of elements
*/
expect(element).toHaveLength(length);Usage Example:
expect($('.menu-item')).toHaveLength(5);
expect($('ul li')).toHaveLength(3);Checks if element contains child matching selector.
/**
* Checks if element contains descendant matching selector
* @param selector - CSS selector to find within element
*/
expect(element).toContainElement(selector);Usage Example:
expect($('#form')).toContainElement('input[type="submit"]');
expect($('.modal')).toContainElement('.close-button');Checks if element contains specified text.
/**
* Checks if element text content contains specified string or matches regex
* @param text - Text string or RegExp pattern to find
*/
expect(element).toContainText(text);Checks if element contains specified HTML.
/**
* Checks if element HTML content contains specified HTML string
* @param html - HTML string to find within element
*/
expect(element).toContainHtml(html);Checks if element matches selector.
/**
* Checks if element matches the given CSS selector
* @param selector - CSS selector to match against
*/
expect(element).toBeMatchedBy(selector);Usage Example:
expect($('.button.primary')).toBeMatchedBy('.button');
expect($('#nav ul li')).toBeMatchedBy('li');Checks if element has event handler for specified event.
/**
* Checks if element has registered event handler for event type
* @param eventName - Event name (can include namespaces like 'click.myapp')
*/
expect(element).toHandle(eventName);Usage Examples:
expect($('#button')).toHandle('click');
expect($('#form')).toHandle('submit.validation');Checks if element has specific event handler function.
/**
* Checks if element has the exact event handler function registered
* @param eventName - Event name
* @param eventHandler - Specific handler function to check for
*/
expect(element).toHandleWith(eventName, eventHandler);Usage Example:
function myClickHandler() { /* ... */ }
$('#button').on('click', myClickHandler);
expect($('#button')).toHandleWith('click', myClickHandler);jasmine-jquery provides utility functions for cross-browser HTML processing:
Normalizes HTML for cross-browser compatibility.
/**
* Normalizes HTML tag case for cross-browser comparison
* Helps ensure consistent HTML comparisons across different browsers
* @param html - HTML string to normalize
* @returns Normalized HTML string
*/
function jasmine.jQuery.browserTagCaseIndependentHtml(html);Usage Example:
var normalizedHtml = jasmine.jQuery.browserTagCaseIndependentHtml('<DIV>content</DIV>');
// Returns: '<div>content</div>' (lowercase tags)Converts jQuery elements to string representation.
/**
* Converts jQuery element(s) to string representation using outerHTML
* Useful for debugging and custom matcher implementations
* @param element - jQuery element or collection
* @returns String representation of element(s)
*/
function jasmine.jQuery.elementToString(element);Usage Example:
var elementString = jasmine.jQuery.elementToString($('<div class="test">Hello</div>'));
// Returns: '<div class="test">Hello</div>'
var multipleElements = jasmine.jQuery.elementToString($('<span>A</span><span>B</span>'));
// Returns: '<span>A</span>, <span>B</span>'All matchers support negation using .not:
expect($('#hidden-element')).not.toBeVisible();
expect($('input')).not.toHaveClass('error');
expect($('.empty-list')).not.toContainElement('li');