react-lazy-load-image-component
react-lazy-load-image-component copied to clipboard
Error when test rendering component using React Test Library
I run the test (RTL) getting error TypeError: Cannot read properties of undefined (reading 'prototype') while rendering component.
I've tried looking everywhere but can't find a solution. I post here to get enlightenment on how to test this component. The screenshot above shows that I immediately called the component, although not directly, the result was still an error. Thanks 🙏
I had the same error, found solution in src/utils/intersection-observer.spec.js: Adding
window.IntersectionObserver = {};
window.IntersectionObserverEntry = {
prototype: {
isIntersecting: () => null,
},
};
at the beginning of your test or in setupTests fixes the problem.
@wisnuvb one other idea is that you can actually mock it in your setupTests.
jest.mock("react-lazy-load-image-component", () => ({
LazyLoadImage: ({ src, alt }) => <img alt={alt} src={src} />
}));