admin管理员组

文章数量:1403527

I am using Jest in my React module and encountering an error where it cannot find the classes for SVG elements like SVGPathElement and SVGPolylineElement. The specific error message is:

ReferenceError: SVGPathElement is not defined

Here is the sample code that reproduces the issue:

import '@testing-library/jest-dom';

class MyClass extends SVGPathElement {
}

it('first test', () => {
});

I have defined several prototypes for these SVG classes in my application, so I prefer not to redefine them in my Jest tests like this:

class SVGPathElement extends SVGElement {}
window.SVGPathElement = SVGPathElement as any

Is there a better way to configure Jest or handle this issue without manually redefining these SVG classes? Any advice or solutions would be greatly appreciated.

I am using Jest in my React module and encountering an error where it cannot find the classes for SVG elements like SVGPathElement and SVGPolylineElement. The specific error message is:

ReferenceError: SVGPathElement is not defined

Here is the sample code that reproduces the issue:

import '@testing-library/jest-dom';

class MyClass extends SVGPathElement {
}

it('first test', () => {
});

I have defined several prototypes for these SVG classes in my application, so I prefer not to redefine them in my Jest tests like this:

class SVGPathElement extends SVGElement {}
window.SVGPathElement = SVGPathElement as any

Is there a better way to configure Jest or handle this issue without manually redefining these SVG classes? Any advice or solutions would be greatly appreciated.

Share Improve this question edited Mar 22 at 8:23 Mohamad Mehdi Rajaei asked Mar 21 at 8:10 Mohamad Mehdi RajaeiMohamad Mehdi Rajaei 311 silver badge8 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

The error went away after migrating from jsdom to happy-dom.

本文标签: reactjsJest throws ReferenceError SVGPathElement is not definedStack Overflow