简体   繁体   中英

enzyme test, context not rendering elements

I am using enzyme to write my tests, before adding a context to my component which is the consumer component in this case, the snapshot rendered is not showing any elements and every time i add a dive() or shallow() I got the same error how can i access the elements while using context

 const wrapper = shallow( <MyComponent {...initialProps} store={mockStore(initialState)} />, {context}, ); const contents = wrapper .dive() // dive in connect .dive() // dive in withLogss .dive() // dive in Motion .find("MyComponent") .dive(); // dive in Host expect(contents.context()).toEqual({myBoolean: true}); it("should render properly", () => { expect(contents).toMatchSnapshot(); }); Result: <ContextConsumer> <Component /> </ContextConsumer>

Enzyme prior to v3.10.0 lacked support of React's new Context API .

From the v3.10.0 Changelog :

shallow: Support rendering and dive()ing createContext() providers and consumers (#1966)

Make sure you are using enzyme v3.10.0 or later and enzyme-adapter-react-16 v1.15.1 or later.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM