Test Utilities
Importing
Overview
ReactTestUtils
makes it easy to test React components in the testing framework of your choice. At Facebook we use Jest for painless JavaScript testing. Learn how to get started with Jest through the Jest website's React Tutorial.
Note:
We recommend using React Testing Library which is designed to enable and encourage writing tests that use your components as the end users do.
For React versions <= 16, the Enzyme library makes it easy to assert, manipulate, and traverse your React Components' output.
act()
mockComponent()
isElement()
isElementOfType()
isDOMComponent()
isCompositeComponent()
isCompositeComponentWithType()
findAllInRenderedTree()
scryRenderedDOMComponentsWithClass()
findRenderedDOMComponentWithClass()
scryRenderedDOMComponentsWithTag()
findRenderedDOMComponentWithTag()
scryRenderedComponentsWithType()
findRenderedComponentWithType()
renderIntoDocument()
Simulate
Reference
act()
act()
To prepare a component for assertions, wrap the code rendering it and performing updates inside an act()
call. This makes your test run closer to how React works in the browser.
Note
If you use
react-test-renderer
, it also provides anact
export that behaves the same way.
For example, let's say we have this Counter
component:
Here is how we can test it:
Don't forget that dispatching DOM events only works when the DOM container is added to the
document
. You can use a library like React Testing Library to reduce the boilerplate code.The
recipes
document contains more details on howact()
behaves, with examples and usage.
mockComponent()
mockComponent()
Pass a mocked component module to this method to augment it with useful methods that allow it to be used as a dummy React component. Instead of rendering as usual, the component will become a simple <div>
(or other tag if mockTagName
is provided) containing any provided children.
Note:
mockComponent()
is a legacy API. We recommend usingjest.mock()
instead.
isElement()
isElement()
Returns true
if element
is any React element.
isElementOfType()
isElementOfType()
Returns true
if element
is a React element whose type is of a React componentClass
.
isDOMComponent()
isDOMComponent()
Returns true
if instance
is a DOM component (such as a <div>
or <span>
).
isCompositeComponent()
isCompositeComponent()
Returns true
if instance
is a user-defined component, such as a class or a function.
isCompositeComponentWithType()
isCompositeComponentWithType()
Returns true
if instance
is a component whose type is of a React componentClass
.
findAllInRenderedTree()
findAllInRenderedTree()
Traverse all components in tree
and accumulate all components where test(component)
is true
. This is not that useful on its own, but it's used as a primitive for other test utils.
scryRenderedDOMComponentsWithClass()
scryRenderedDOMComponentsWithClass()
Finds all DOM elements of components in the rendered tree that are DOM components with the class name matching className
.
findRenderedDOMComponentWithClass()
findRenderedDOMComponentWithClass()
Like scryRenderedDOMComponentsWithClass()
but expects there to be one result, and returns that one result, or throws exception if there is any other number of matches besides one.
scryRenderedDOMComponentsWithTag()
scryRenderedDOMComponentsWithTag()
Finds all DOM elements of components in the rendered tree that are DOM components with the tag name matching tagName
.
findRenderedDOMComponentWithTag()
findRenderedDOMComponentWithTag()
Like scryRenderedDOMComponentsWithTag()
but expects there to be one result, and returns that one result, or throws exception if there is any other number of matches besides one.
scryRenderedComponentsWithType()
scryRenderedComponentsWithType()
Finds all instances of components with type equal to componentClass
.
findRenderedComponentWithType()
findRenderedComponentWithType()
Same as scryRenderedComponentsWithType()
but expects there to be one result and returns that one result, or throws exception if there is any other number of matches besides one.
renderIntoDocument()
renderIntoDocument()
Render a React element into a detached DOM node in the document. This function requires a DOM. It is effectively equivalent to:
Note:
You will need to have
window
,window.document
andwindow.document.createElement
globally available before you importReact
. Otherwise React will think it can't access the DOM and methods likesetState
won't work.
Other Utilities
Simulate
Simulate
Simulate an event dispatch on a DOM node with optional eventData
event data.
Simulate
has a method for every event that React understands.
Clicking an element
Changing the value of an input field and then pressing ENTER.
Note
You will have to provide any event property that you're using in your component (e.g. keyCode, which, etc...) as React is not creating any of these for you.
Last updated