npm install --save-dev --save-exact pretty-format@24.0.0- copy
DOMElementNoClassIdSnapshotSerializer.jsto your project folder - add
'./DOMElementNoClassIdSnapshotSerializer.js'tosnapshotSerializersin your jest config
Angular does create HTML for components like this:
<h1 class="ng-tns-c1-0">
app works!
<app-inner-comp class="ng-tns-c1-0" />
<span class="ng-tns-c1-0">
Some Text
</span>
</h1>The ID in the class (c1-0) iterates with each component.
If you use snapshots in jest, the ids change depending on the number of tests you run in a test suite.
This serializer removes all ids in classes after ng-tsn- and replaces them with x.
This way seralized components stay consistent and independent of test number/order.
Example:
<h1 class="ng-tns-cx-x">
app works!
<app-inner-comp class="ng-tns-cx-x" />
<span class="ng-tns-cx-x">
Some Text
</span>
</h1>This serializer is copied from the DOMElement.js-Serializer from the package pretty-format which is located in the repository github.com/facebook/jest.
Some adjustments were made to handle the rendering of the class attribute.
To not copy everything, this serializer heavily depends on pretty-format v24.0.0. If you want to use it, please add "pretty-format": "24.0.0" (exact vesrion, no semver range) to your project.
The serializer depends on internals of pretty-format and therefore I do not know if another version than 24.0.0 is supported.