我认为您收到的错误消息表明Apollo无法完全渲染,因为它无法从上下文中找到任何Apollo客户端。如果调用的代码没有
ApolloProvider
和
client
作为它的父母。即使您正在测试一个钩子,您仍然需要将钩子包装在Apollo的提供者中,并提供
客户
根据阿波罗公司的文件。看看你的React钩子测试库是如何建议你这么做的。
下面是我建议使用的示例
@testing-library/react-hooks
:
import { ApolloProvider } from '@apollo/client'
import { renderHook } from '@testing-library/react-hooks'
import { client } from './apollo-client'
import { myHook } from './myHook'
it('runs the hook as expected', () => {
renderHook(() => myHook(), {
wrapper({ children }) {
return <ApolloProvider client={client}>{children}</ApolloProvider>
}
})
// actions and assertions here
})
我知道这不会直接转化为Vue,但我确信在Vue中测试挂钩有一个替代方案。希望这能让你朝着正确的方向前进。