我在React应用程序中将babel
升级到了版本7,还将jest
和babel-jest
升级到了版本24.8.0
。之后,我得到了错误:
TypeError: (0 , _reactEventListener.withOptions) is not a function
214 | <EventListener
215 | target="window"
216 | onClick={withOptions(this.hide, { passive: true })}
| ^
217 | />
在组件中,我正在使用EventListener
组件和withOptions
中的react-event-listener
函数。导入看起来像这样。
import EventListener, { withOptions } from "react-event-listener";
这是升级babel 7等之前的模拟外观。
import ReactEventListener from "react-event-listener";
jest.mock("react-event-listener", () => props => (
<div {...props}>ReactEventListener</div>
));
ReactEventListener.withOptions = () => {};
我也尝试了类似这样的互联网解决方案:
const mockReactEvenetListener = props => (
<div {...props}>ReactEventListener</div>
);
jest.mock('react-event-listener', () => ({
__esModule: true,
withOptions: () => {},
default: mockReactEvenetListener
}));
然后在测试中我得到了不同的错误:
Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
Check the render method of `AutoComplete`.
101 | it("should be working test", () => {
102 | const component = renderer
> 103 | .create(
| ^
104 | <AutoComplete
105 | placeholder="Text"
106 | headerStyle={{ foo1: "bar1" }}
有人知道我应该如何模拟这个模块吗?