Skip to content
Advertisement

Jest with vanilla JS – tesing generated elements on DOM

Searched a lot online but cant find the best way to get an element’s innerHTML and just compare it with the results that should be rendered (I’m trying to compare with string)

JavaScript

what I’m trying is adding some whitespace and test failed so I’m guessing there is better ways

Also, unable to query a specific element generated inside body, why?

Advertisement

Answer

You can use Snapshot Testing to test the DOM tree structure.

Snapshot tests are a very useful tool whenever you want to make sure your UI does not change unexpectedly.

E.g.

JavaScript

test result:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement