DEV Community

Paula Penedo (she/her)
Paula Penedo (she/her)

Posted on

Jest JSDOM data-attribute

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');

// returns 'taco'
Enter fullscreen mode Exit fullscreen mode

Top comments (0)