Reactjs 测试Apollo客户端
我使用两个GraphQLAPI,一个用于数据,一个用于内容。因此,使用下面的代码基于 我正在犯错误Reactjs 测试Apollo客户端,reactjs,jestjs,apollo,react-apollo,apollo-client,Reactjs,Jestjs,Apollo,React Apollo,Apollo Client,我使用两个GraphQLAPI,一个用于数据,一个用于内容。因此,使用下面的代码基于 我正在犯错误 ● Apollo provider › client › is configured just the way we want it expect(jest.fn()).toBeCalledWith(...expected) Expected: {"cache": "mockInMemoryCache", "link&quo
● Apollo provider › client › is configured just the way we want it
expect(jest.fn()).toBeCalledWith(...expected)
Expected: {"cache": "mockInMemoryCache", "link": undefined}
Number of calls: 0
67 | expect(ApolloClient).not.toBeCalled();
68 | client({});
> 69 | expect(ApolloClient).toBeCalledWith({
| ^
70 | link: link,
71 | cache: 'mockInMemoryCache'
72 | })
我无法找出问题所在,我可以看到
的“链接”:未定义,但仍然不确定为什么会出现这种情况。非常感谢请为每种产品提供导入/需求代码module@slideshowp2-补充感谢:-)
/**
* @jest-environment node
*/
import React from 'react';
jest.mock('next-with-apollo');
jest.mock('apollo-boost');
jest.mock('@apollo/react-hooks');
jest.mock('isomorphic-unfetch');
import ApolloClient, {
ApolloLink,
HttpLink,
InMemoryCache
} from 'apollo-boost';
import nextWithApollo from 'next-with-apollo';
import fetch from 'isomorphic-unfetch';
const mockInMemoryCache = {
restore: jest.fn().mockReturnValue('mockInMemoryCache')
};
(InMemoryCache as jest.Mock).mockReturnValue(mockInMemoryCache);
jest.requireActual('../../shared/graphql/withApollo');
describe('client', () => {
const client = (nextWithApollo as jest.Mock).mock.calls[0][0];
it('is configured just the way we want it', () => {
const gqlAPI1 = new HttpLink({
uri: 'https://xxxxxxxxxxxx.com/graphql',
credentials: 'include',
fetch,
headers: { 'key': '1234' }
});
const gqlAPI2 = new HttpLink({
uri: 'https://xxxxxxxxxxx.com/',
fetch,
headers: {
authorization: `Bearer sadsdasdasdsadsa`,
'Content-Language': 'en-us'
}
});
const link = ApolloLink.split(
(operation) => operation.getContext().clientName === 'cms',
gqlAPI2,
gqlAPI1
);
expect(ApolloClient).not.toBeCalled();
client({});
expect(ApolloClient).toBeCalledWith({
link: link,
cache: 'mockInMemoryCache'
});
});
});
● Apollo provider › client › is configured just the way we want it
expect(jest.fn()).toBeCalledWith(...expected)
Expected: {"cache": "mockInMemoryCache", "link": undefined}
Number of calls: 0
67 | expect(ApolloClient).not.toBeCalled();
68 | client({});
> 69 | expect(ApolloClient).toBeCalledWith({
| ^
70 | link: link,
71 | cache: 'mockInMemoryCache'
72 | })