You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, many of the component unit tests in Elements take a snapshot testing approach; more specifically, they take a "DOM snapshot" which capture what we expect to see in the DOM when the component is used.
For example, the Avatar component implements snapshots in the following manner:
it('should match a snapshot',()=>{constwrapper=render(<Avatar>Some Content</Avatar>)expect(wrapper).toMatchSnapshot()})
Since wrapper is the full result object from React Testing Library's (RTL) render function, we are capturing its API instead of just the DOM content:
To fix this, we should avoid taking a snapshot of the whole render result object from RTL, and instead only capture the actual document fragment, which is available via the asFragment function provided in the render result. This approach was recently taken for the TextArea component's snapshots in #150 and #151.
describe('content field-sizing',()=>{test('default min/max rows are 3 -> Infinity',()=>{const{ asFragment }=render(<TextAreafieldSizing="content"/>)expect(asFragment()).toMatchSnapshot()})})
Why not just snapshot wrapper.baseElement or wrapper.container?
In both cases, these include DOM elements that are implementation details of RTL and how it renders components. Further, seeing extra DOM elements in the snapshot may also confuse readers, making them incorrectly think those elements are rendered by the component under test.
In contrast, using asFragment() means we only see an extra <DocumentFragment> element in the snapshot, which is easier to recognise as being unrelated to our component under test. DocumentFragment is also part of the DOM API, so its presence in the snapshot does not represent an RTL implementation detail.
Work to be done
Given the number of components that will be impacted by this change, I'm proposing we do this as a big-bang change:
Find expect(wrapper).toMatchSnapshot() and replace with expect(wrapper.asFragment()).toMatchSnapshot();
Run tests and update all snapshots;
PR and merge.
Thoughts?
The text was updated successfully, but these errors were encountered:
Context
Currently, many of the component unit tests in Elements take a snapshot testing approach; more specifically, they take a "DOM snapshot" which capture what we expect to see in the DOM when the component is used.
For example, the Avatar component implements snapshots in the following manner:
Since
wrapper
is the full result object from React Testing Library's (RTL)render
function, we are capturing its API instead of just the DOM content:To fix this, we should avoid taking a snapshot of the whole render result object from RTL, and instead only capture the actual document fragment, which is available via the
asFragment
function provided in the render result. This approach was recently taken for theTextArea
component's snapshots in #150 and #151.This results in the following snapshot content:
Why not just snapshot
wrapper.baseElement
orwrapper.container
?In both cases, these include DOM elements that are implementation details of RTL and how it renders components. Further, seeing extra DOM elements in the snapshot may also confuse readers, making them incorrectly think those elements are rendered by the component under test.
In contrast, using
asFragment()
means we only see an extra<DocumentFragment>
element in the snapshot, which is easier to recognise as being unrelated to our component under test. DocumentFragment is also part of the DOM API, so its presence in the snapshot does not represent an RTL implementation detail.Work to be done
Given the number of components that will be impacted by this change, I'm proposing we do this as a big-bang change:
expect(wrapper).toMatchSnapshot()
and replace withexpect(wrapper.asFragment()).toMatchSnapshot()
;Thoughts?
The text was updated successfully, but these errors were encountered: