Matchers
Matchers let you test values and patterns, either simple or complex, in a succint way.
The examples below are using Jest matchers, but the same approach can be used for other testing frameworks with a bit of work.
toBeChecked()
#
toBeChecked(): boolean
Check that an element has a truthy checked
or defaultChecked
prop. This is most commonly used
with form fields.
toBeDisabled()
#
toBeChecked(): boolean
Check that an element has a truthy disabled
prop. This is most commonly used with form fields.
toBeElementType()
#
toBeElementType(type: React.ElementType): boolean
Check that an element is a valid React element type. Accepts either a class or function component, or the name of a host component (HTML tag).
toContainNode()
#
toContainNode(node: React.ReactNode): boolean
Check that an element contains a node (string, element, etc) within its children. A caveat exists for this matcher.
Will also check for the node at any depth in the current tree.
And when checking elements, it will compare the component type and props for deep equality.
toHaveClassName()
#
toHaveClassName(name: string): boolean
Check that an element has a className
prop that matches the defined value.
toHaveKey()
#
toHaveKey(name: string): boolean
Check that an element has a React key
that matches the provided value.
toHaveProp()
#
toHaveProp(name: string, value?: unknown): boolean
Check that an element has a prop that matches the provided name, with optional matching value. Arrays and objects will be matched using deep equality.
toHaveProps()
#
toHaveProps(props: { [key: string]: unknown }): boolean
Check that an element's props match all the provided props and their values. Arrays and objects will be matched using deep equality.
toHaveRendered()
#
toHaveRendered(): boolean
Check that a component has rendered children. If a component returns null
, this will evaluate to
false.
toHaveValue()
#
toHaveValue(value: string): boolean
Check that an element has a value
or defaultValue
prop that matches the provided value. This is
most commonly used with form fields.