DEV Community

loading...

Jest JSDOM data-attribute

paulinhapenedo profile image Paula Penedo (she/her) ・1 min read

When using Jest with JSDOM to test HTML elements, it's common to try to use or access the data-attribute. The problem is that it doesn't work.

The solution is quite simple: getAttribute / setAttribute.

element.setAttribute('data-food', 'taco');

element.getAttribute('data-food');
// returns 'taco'
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

pic
Editor guide