Any documentation on what's the purpose of adapter
in enzyme
testing library.
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
configure({ adapter: new Adapter() });
Any documentation on what's the purpose of adapter
in enzyme
testing library.
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
configure({ adapter: new Adapter() });
The closest it gets is to say "You will need to install enzyme along with an Adapter corresponding to the version of react (or other UI Component library) you are using".
The docs mostly just explain how to configure an
adapter
and don't really talk about its purpose.Short version
The
enzyme
API is the same regardless of the version ofReact
you are using, but howReact
renders and interacts with what is rendered changes depending on theReact
version.The
adapter
abstracts away anything that changes based on theReact
version so the coreenzyme
code can stay the same.Detailed version
mount
andshallow
are both exported fromenzyme
. Let's focus onmount
.mount
is a function that just returns a newReactWrapper
.ReactWrapper
provides the familiar wrapper object withinstance
,setState
,find
, etc.The implementation of all of those functions is the same regardless of which version of
React
you are using......but because
React
itself has changed over the years any implementation details that change based on theReact
version are abstracted away with an adapter.The adapter is retrieved by calling
getAdapter
and the first time it is used is to validate the nodes passed tomount
, and then to create therenderer
to actually render the nodes.For
enzyme-adapter-react-16.3
that call tocreateRenderer
gets routed tothis.createMountRenderer
and withincreateMountRenderer
you can see the familiarReactDOM.render
call where what you passed is actually rendered usingReact
v16 syntax.Searching for
getAdapter
within ReactWrapper.js shows everywhere that theadapter
is used to abstract away functionality that changes based on theReact
version while usingmount
......and searching for
getAdapter
within ShallowWrapper.js shows everywhere thatadapter
is used to abstract away functionality that changes based on theReact
version while usingshallow
.