如何模拟 Auth.currentSession()?

How to mock Auth.currentSession()?

我尝试模拟 Auth.currentSession() 进行开玩笑测试。

我试着按照这篇文章写了代码
https://github.com/aws-amplify/amplify-js/issues/3605

但它不起作用。它会导致编译错误。

    services/ui/wait-step-check/index.spec.ts:10:3 - error TS2345: Argument of type '{ getAccessToken: () => { getJwtToken: () => string; }; }' is not assignable to parameter of type 'Promise<CognitoUserSession>'.
      Object literal may only specify known properties, and 'getAccessToken' does not exist in type 'Promise<CognitoUserSession>'.

    10   getAccessToken: () => ({
         ~~~~~~~~~~~~~~~~~~~~~~~~
    11     getJwtToken: () => ("Secret-Token")
       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    12   })
       ~~~~

请给我使这个 Auth 模拟工作的解决方案。

index.ts

export const waitDoneCheckProcessing = async (
  operationId: number,
  watchStatusSelector: WatchSelector,
  timeoutId?: any,
  options?: { intervalSec?: number },
): Promise<EnumCheckStatus | null> => {

// (omitted).

  const auth = await Authorization();
  const res = await client.getV1OperationsIdProgress(operationId, auth);

// (omitted).

};

index.spec.ts

import {waitDoneCheckProcessing} from "~/services/ui/wait-step-check/index";
import axios from "axios";
import {EnumCheckStatus} from "~/services/openapi";
import Auth from '@aws-amplify/auth'

jest.mock('axios');
const mockAxios = axios as jest.Mocked<typeof axios>;

// I want to make this mock work.
jest.spyOn(Auth, 'currentSession').mockReturnValue({
  getAccessToken: () => ({
    getJwtToken: () => ("Secret-Token")
  })
});

describe('wait-step-check', () => {
  beforeEach(() => {
    mockAxios.request.mockClear();
    let counter = 0;
    mockAxios.request.mockImplementation(() => {
      counter++;
      return Promise.resolve<any>({
        data: {
          impairment1: {
            checkStatus: counter >= 3 ? EnumCheckStatus.Done : EnumCheckStatus.Processing,
          },
        },
      })
    })
  });
  it('check', async () => {
    await waitDoneCheckProcessing(
      1,
      (res) => res.impairment1.checkStatus,
      {
        intervalSec: 100,
      });
    expect(mockAxios.request).toBeCalledTimes(3);
  });
})

我只是可以通过添加@ts-ignore

来避免编译错误
  it('check', async () => {
    // @ts-ignore
    jest.spyOn(Auth, 'currentSession').mockImplementation(() => ({
      getIdToken: () => ({
        getJwtToken: () => 'mock',
      }),
    }));
    await waitDoneCheckProcessing(
      1,
      (res) => res.impairment1.checkStatus,
      {
        intervalSec: 100,
      });
    expect(mockAxios.request).toBeCalledTimes(3);
  }