我有一个用例,我想从开发中的模拟开发服务器中获得相同的数据,也在测试中。
为此,我使用了 graphql-mesh 及其getMeshSDK
方法。这将返回一个解析为查询列表的承诺。解析它们中的每一个都会返回数据。
然后我想使用这些数据来填充 graphql-client - GQless。
beforeEach(async () => {
const sdk = await getMeshSDK();
const mockCertificates = await sdk.certificates_query;
jest.mock('../../../gqless', async () => {
return {
query: { certificates: mockCertificates },
graphql: jest.fn((Component) => () => Component()),
useMetaState: jest.fn(() => ({ isFetching: false })),
};
});
})
GQless 在组件中的使用方式如下:
import {query, graphql, useMetaState} from '../../../gqless';
export const MyComponent = graphql(() => {
console.log(query.certificates); // returns an array directly
return ...
})
但是,无论我采用什么方法,都会jest.mock
在 promise 解决之前执行模拟,从而query
导致undefined
.