0

我有以下模块,我想在其中模拟ApolloClient.query method

模块文件

import { ApolloClient } from 'apollo-boost';

const client = new ApolloClient({
  uri: requestUrl,
});

export async function getUsers(): Promise<UsersData[]> {
  try {
    const response = await client.query({});
    // some logic
}

测试文件

我想嘲笑拨打的电话getUsers,这是我的尝试

import * as apolloBoost from 'apollo-boost';
import { mocked } from 'ts-jest/utils';
import { getUsers } from '../src/api/users.ts';


const apolloClientQueryMock: jest.Mock = jest.fn();

jest.mock('apollo-boost', () => ({
  ApolloClient: jest.fn(),
}));
// @ts-ignore
mocked(apolloBoost.ApolloClient).mockImplementation(() => ({ query: apolloClientQueryMock } as unknown) as apolloBoost.ApolloClient);

describe('users access api', () => {
    afterEach(() => {
      jest.resetAllMocks();
    });

    it('successful get users', async () => {

      apolloClientQueryMock.mockResolvedValue({
        data: {}
      });

      const result = await getUsers();
    });
  });

我不断收到此错误:TypeError: apollo_boost_1.default is not a constructor

请指教。

4

0 回答 0