Matchers
To use a matcher, you need to call expect
, which will return an instance of Expect
, which, in turn, will make it possible to call the corresponding matcher
on the specified value.
You can use the matcher in two ways:
expect(value).toBe(expectedValue)
expect(value).not.toBe(expectedValue)
Positive Tests
By default, the matcher checks if the value is equal to the expected value (positive test). If it is, the test will pass; otherwise, it will fail.
If in expect
you pass the value to be tested, then in matcher
you specify a control value that must either match the value being tested or not.
Also, in the matcher, you can pass your custom error message.
test(`Common tests suite`, () => { expect(1 + 1).toBe(2, `1 + 1 should be 2`)})
Negative Tests
If you want to check that the value is not equal to the expected value, you can use the not
modifier:
expect(value).not.toBe(expectedValue)
Chaining Matchers
You can chain multiple matchers together to create more complex assertions. For example:
expect(value) .toBeDefined() .toBeType('string') .toBe(expectedValue)
Base
toBe()
toBe(expected, msg?: string)
Asserts that the actual value is equal to the expected value. Matcher uses the Object.is()
method to compare values.
toBeStrictEqual()
toBeStrictEqual(expected, msg?: string)
Asserts that the actual value is strict equal (uses ===
) to the expected value.
toBeEqual()
toBeEqual(expected, msg?: string)
Asserts that the actual value is equal (uses ==
) to the expected value.
Array
toBeArrayEqual()
toBeArrayEqual(expected, msg?: string)
Asserts that the actual array is equal to the expected array.
toBeArray()
toBeArray(msg?: string)
Asserts that the actual value is an array.
toBeArraySorted()
toBeArraySorted(msg?: string)
Asserts that the actual value is sorted.
toBeArrayUnique()
toBeArrayUnique(msg?: string)
Asserts that values in the array are unique.
toContain()
toContain(expected, msg?: string)
Asserts that the actual value contains the expected value.
toBeEmpty()
toBeEmpty(msg?: string)
Asserts that the actual value is empty.
hasLength()
hasLength(expected, msg?: string)
Asserts that the array-like object has the expected length.
toBeArrayBuffer()
toBeArrayBuffer(msg?: string)
Asserts that the actual value is an ArrayBuffer.
Async
toBeResolvedWith()
toBeResolvedWith(expected, msg?: string)
Asserts that the actual promise resolves with the expected value.
toBeRejectedWith()
toBeRejectedWith(expected, msg?: string)
Asserts that the actual promise rejects with the expected value.
Color
toBeHEXColor()
toBeHEXColor(msg?: string)
Asserts that the actual value is a HEX color.
toBeRGBColor()
toBeRGBColor(msg?: string)
Asserts that the actual value is an RGB color.
toBeRGBAColor()
toBeRGBAColor(msg?: string)
Asserts that the actual value is an RGBA color.
toBeHSVColor()
toBeHSVColor(msg?: string)
Asserts that the actual value is an HSL color.
toBeHSLColor()
toBeHSLColor(msg?: string)
Asserts that the actual value is an HSL color.
toBeHSLAColor()
toBeHSLAColor(msg?: string)
Asserts that the actual value is an HSLA color.
toBeCMYKColor()
toBeCMYKColor(msg?: string)
Asserts that the actual value is a CMYK color.
toBeColor()
toBeColor(msg?: string)
Asserts that the actual value is a valid color (HEX, RGB, RGBA, HSV, HSL, HSLA, or CMYK).
Html
toBeHtmlElement()
toBeHtmlElement(msg?: string)
Asserts that the actual value is an HTML element.
toBeNode()
toBeNode(msg?: string)
Asserts that the actual value is an HTML node.
toBeDocument()
toBeDocument(msg?: string)
Asserts that the actual value is an HTML document.
toBeHtmlCollection()
toBeHtmlCollection(msg?: string)
Asserts that the actual value is an HTML collection.
toBeWindow()
toBeWindow(msg?: string)
Asserts that the actual value is a Window object.
toBeTextNode()
toBeTextNode(msg?: string)
Asserts that the actual value is a Text node.
hasClass()
hasClass(expected, msg?: string)
Asserts that the HTML element has the specified class.
hasAttribute()
hasAttribute(expected, msg?: string)
Asserts that the HTML element has the specified attribute.
hasChildren()
hasChildren(msg?: string)
Asserts that the HTML element has children.
hasParent()
hasParent(msg?: string)
Asserts that the HTML element has a parent.
hasStyleProperty()
hasStyleProperty(expected, msg?: string)
Asserts that the HTML element has the specified style in style property.
hasStyle()
hasStyle(prop, val, msg?: string)
Asserts that the HTML element has the specified style property.
hasStyles()
hasStyles(styles: object, msg?: string)
Asserts that the HTML element has the specified styles.
hasSiblings()
hasSiblings(msg?: string)
Asserts that the HTML element has siblings.
hasSibling()
hasSibling(expected: HTMLElement, msg?: string)
Asserts that the HTML element has a sibling with the specified value.
hasPrev()
hasPrev(msg?: string)
Asserts that the HTML element has a previous sibling.
hasNext()
hasNext(msg?: string)
Asserts that the HTML element has a next sibling.
hasText()
hasText(expected, msg?: string)
Asserts that the HTML element has the specified text.
containsElement()
containsElement(expected, msg?: string)
Asserts that the HTML element contains the specified element.
containsElementDeep()
containsElementDeep(expected, msg?: string)
Asserts that the HTML element contains the specified element deeply.
hasId()
hasId(expected, msg?: string)
Asserts that the HTML element has the specified id.
hasName()
hasName(expected, msg?: string)
Asserts that the HTML element has the specified name.
hasHref()
hasHref(expected, msg?: string)
Asserts that the HTML element has the specified href.
hasSrc()
hasSrc(expected, msg?: string)
Asserts that the HTML element has the specified src.
Mock
toHaveBeenCalled()
toHaveBeenCalled(msg?: string)
Asserts the mock function was called at least once
toHaveBeenCalledTimes()
toHaveBeenCalledTimes(expected, msg?: string)
Asserts the mock function was called at least once
toHaveBeenCalledWith()
toHaveBeenCalledWith(expected, msg?: string)
Asserts that the mock function was called with specified arguments.
toHaveBeenLastCalledWith()
toHaveBeenLastCalledWith(expected, msg?: string)
Asserts that the mock function was called last with specified arguments.
Object
toBeObject()
toBeObject(expected, msg?: string)
Asserts that the actual object is equal to the expected object. Checks only own level properties and values.
toBeDeepEqual()
toBeDeepEqual(expected, msg?: string)
Asserts that the actual value is deeply equal to the expected value.
toBeDeepEqualSafe()
toBeDeepEqualSafe(expected, msg?: string)
Asserts that the actual value is deeply equal to the expected value using a safe comparison.
toBeObjectStructureEqual()
toBeObjectStructureEqual(expected, msg?: string)
Asserts that the actual structure is equal to the expected structure.
hasProperty()
hasProperty(expected, msg?: string)
Asserts that the actual value has the specified property. You can use dot notation to check for nested properties.
hasPropertyValue()
hasPropertyValue(expected, value, msg?: string)
Asserts that the actual value has the specified property with the expected value. You can use dot notation to check for nested properties.
Throw
toThrow()
toThrow(msg?: string)
Asserts that the actual function throws an error.
toThrowError()
toThrowError(expected, msg?: string)
Asserts that the actual function throws an error matching the expected value.
Type
toBeType()
toBeType(type, msg?: string)
Asserts that the actual value is of the specified type.
toBeInstanceOf()
toBeInstanceOf(type, msg?: string)
Asserts that the actual value is an instance of the specified type.
toBeString()
toBeString(msg?: string)
Asserts that the actual value is a string.
toBeFunction()
toBeFunction(msg?: string)
Asserts that the actual value is a function.
toBeAsyncFunction()
toBeAsyncFunction(msg?: string)
Asserts that the actual value is an async function.
toBeDate()
toBeDate(msg?: string)
Asserts that the actual value is a date.
toBeDateObject()
toBeDateObject(msg?: string)
Asserts that the actual value is a date.
toBeRegExp()
toBeRegExp(msg?: string)
Asserts that the actual value is a regular expression.
toBeSymbol()
toBeSymbol(msg?: string)
Asserts that the actual value is a symbol.
toBeBigInt()
toBeBigInt(msg?: string)
Asserts that the actual value is a BigInt.
toBeMap()
toBeMap(msg?: string)
Asserts that the actual value is a Map.
toBeSet()
toBeSet(msg?: string)
Asserts that the actual value is a Set.
toBeWeakMap()
toBeWeakMap(msg?: string)
Asserts that the actual value is a WeakMap.
toBeWeakSet()
toBeWeakSet(msg?: string)
Asserts that the actual value is a WeakSet.
toBeNumber()
toBeNumber(msg?: string)
Asserts that the actual value is a number and not is NaN.
toBeNaN()
toBeNaN(msg?: string)
Asserts that the actual value is NaN.
toBeInteger()
toBeInteger(msg?: string)
Asserts that the actual value is an integer.
toBeSafeInteger()
toBeSafeInteger(msg?: string)
Asserts that the actual value is a safe integer.
toBeFloat()
toBeFloat(msg?: string)
Asserts that the actual value is a float.
toBeBoolean()
toBeBoolean(msg?: string)
Asserts that the actual value is a boolean
.
toBeDefined()
toBeDefined(msg?: string)
Asserts that the actual value is defined.
toBeUndefined()
toBeUndefined(msg?: string)
Asserts that the actual value is undefined.
toBeNull()
toBeNull(msg?: string)
Asserts that the actual value is null.
toBePromise()
toBePromise(msg?: string)
Asserts that the actual value is a Promise.
toBeJson()
toBeJson(msg?: string)
Asserts that the actual value is a JSON string.
toBeXml()
toBeXml(msg?: string)
Asserts that the actual value is an XML string.
Validator
toBeBase64()
toBeBase64(msg?: string)
Asserts that the actual value is a Base64 encoded string.
toBeIP()
toBeIP(msg?: string)
Asserts that the actual value is a valid IP address.
toBeIPv4()
toBeIPv4(msg?: string)
Asserts that the actual value is a valid IPv4 address.
toBeIPv6()
toBeIPv6(msg?: string)
Asserts that the actual value is a valid IPv6 address.
toBeEmail()
toBeEmail(msg?: string)
Asserts that the actual value is a valid email address.
toBeUrl()
toBeUrl(msg?: string)
Asserts that the actual value is a valid URL.
toBeCloseTo()
toBeCloseTo(expected, precision = 2, msg?: string)
Asserts that the actual value is close to the expected value within a certain precision.
toBePositive()
toBePositive(msg?: string)
Asserts that the actual value is positive.
toBeNegative()
toBeNegative(msg?: string)
Asserts that the actual value is negative.
toBeFinite()
toBeFinite(msg?: string)
Asserts that the actual value is finite.
toBeGreaterThan()
toBeGreaterThan(expected, msg?: string)
Asserts that the actual value is greater than the expected value.
toBeGreaterThanOrEqual()
toBeGreaterThanOrEqual(expected, msg?: string)
Asserts that the actual value is greater than or equal to the expected value.
toBeLessThan()
toBeLessThan(expected, msg?: string)
Asserts that the actual value is less than the expected value.
toBeLessThanOrEqual()
toBeLessThanOrEqual(expected, msg?: string)
Asserts that the actual value is less than or equal to the expected value.
toBetween()
toBetween(min, max, msg?: string)
Asserts that the actual value is between the specified minimum and maximum values.
toBeMatch()
toMatch(expected, msg?: string)
Asserts that the actual value matches the expected RegExp
pattern.
toBeTrue()
toBeTrue(msg?: string)
Asserts that the actual value is true
.
toBeFalse()
toBeFalse(msg?: string)
Asserts that the actual value is false
.
Accessibility (a11y)
hasAriaAttribute()
hasAriaAttribute(expected, msg?: string)
Asserts that the HTML element has the specified ARIA attribute.
hasAriaAttributes()
hasAriaAttributes(expected, msg?: string)
Asserts that the HTML element has the specified ARIA attributes. expected
is a space-separated string of attributes.
hasAriaRole()
hasAriaRole(expected, msg?: string)
Asserts that the HTML element has the specified ARIA role.
hasAriaLabel()
hasAriaLabel(expected, msg?: string)
Asserts that the HTML element has the specified ARIA label.
hasAltText()
hasAltText(msg?: string)
Asserts that the HTML element has the specified alt text.
toBeKeyboardAccessible()
toBeKeyboardAccessible(msg?: string)
React
toRenderWithoutError()
toRenderWithoutError(msg?: string)
Asserts that the React component renders without throwing an error.
toRenderText()
toRenderText(expected, msg?: string)
Asserts that the React component renders with the specified text.
toContainElement()
toContainElement(expected, msg?: string)
Asserts that the React component contains the specified element.
toHaveElementCount()
toHaveElementCount(expected, msg?: string)
Asserts that the React component has the specified number of elements.
toTriggerEvent()
toTriggerEvent(selector, event, callback, data, msg?: string)
Asserts that the React component triggers the specified event.
toEventuallyContain
toEventuallyContain(selector, timeout, msg?: string)
Asserts that the React component eventually contains the specified element within the given timeout.