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() });
Copyright © 2021 Jogjafile Inc.
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
adapterand don't really talk about its purpose.Short version
The
enzymeAPI is the same regardless of the version ofReactyou are using, but howReactrenders and interacts with what is rendered changes depending on theReactversion.The
adapterabstracts away anything that changes based on theReactversion so the coreenzymecode can stay the same.Detailed version
mountandshalloware both exported fromenzyme. Let's focus onmount.mountis a function that just returns a newReactWrapper.ReactWrapperprovides the familiar wrapper object withinstance,setState,find, etc.The implementation of all of those functions is the same regardless of which version of
Reactyou are using......but because
Reactitself has changed over the years any implementation details that change based on theReactversion are abstracted away with an adapter.The adapter is retrieved by calling
getAdapterand the first time it is used is to validate the nodes passed tomount, and then to create therendererto actually render the nodes.For
enzyme-adapter-react-16.3that call tocreateRenderergets routed tothis.createMountRendererand withincreateMountRendereryou can see the familiarReactDOM.rendercall where what you passed is actually rendered usingReactv16 syntax.Searching for
getAdapterwithin ReactWrapper.js shows everywhere that theadapteris used to abstract away functionality that changes based on theReactversion while usingmount......and searching for
getAdapterwithin ShallowWrapper.js shows everywhere thatadapteris used to abstract away functionality that changes based on theReactversion while usingshallow.