SVGTextElement.getComputedTextLength not supported by jsdom

Viewed 166

In a Create React App project I have a component that outputs a svg element (Label for a recharts ReferenceLine component. It takes a rendered SVGTextElement as a MutableRef and calculates the computedWidth of the element. It works fine except when I run my jest/jsdom tests where I get this error

TypeError: textRef.current.getComputedTextLength is not a function

I tried different ways to mock the function:

In setupTests:

const globalAny: any = global
globalAny.window.SVGTextElement.prototype.getComputedTextLength = jest.fn()

In a beforeEach:

beforeEach(() => {
  window.SVGTextElement.prototype.getComputedTextLength = () => 100
})

Nothing works. I found this old thread about the problem but it seems to have been closed and forgotten.

How do I get past this error?

0 Answers
Related