javascript - svgElement.getBoundingClientRect() always returns zeros while testing with nodejs - Stack Overflow

I created some svg element and want to determine its size withsvgElement.getBoundingClientRect()My co

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?

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?

Share Improve this question edited Mar 23, 2020 at 16:55 skyboyer 23.8k7 gold badges62 silver badges71 bronze badges asked Mar 23, 2020 at 15:22 StefanStefan 12.4k9 gold badges77 silver badges139 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

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

Also see https://github./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
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744847574a4596935.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信