Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

svgElement.getBoundingClientRect() always returns zeros while testing with nodejs

I created some svg element and want to determine its size with

svgElement.getBoundingClientRect()

My code works if the svgElement is appended to the document with

document.body.appendChild(svgElement)

and if I run the code in the browser (google chrome).

However, while testing with nodejs and jest, the width and the height of the resulting bounding rect are always zero.

=>What can I do to correctly run the code with nodejs?

like image 888
Stefan Avatar asked Jul 30 '26 01:07

Stefan


1 Answers

Nodejs uses jsdom and getBoundingClientRect is only implemented to return the right properties, but not the right values.

Also see https://github.com/jsdom/jsdom/issues/653

For the tests the method getBoundingClientRect should be mocked, for example with:

spyOn(svgElement,'getBoundingClientRect').and.returnValue({
    width: 10,
    height: 10,
    top: 0,
    left: 0,
    right: 10,
    bottom: 10
});
like image 191
Stefan Avatar answered Aug 01 '26 15:08

Stefan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!