首页 > 解决方案 > 用于测试 graphql 错误的查询不再有模拟响应

问题描述

我遇到了问题:当我在模拟任何错误发生时使用 React 测试库时,查询没有更多的模拟响应。

我的查询是:

export const GET_AREA_COMPOSITION = gql`
  query getAreaComposition($areaId: ID!) {
    areaComposition(
        areaId: $areaId
        page: 1
        first: 999999
      ) {
      data {
        id
        areaCompositionRule
        usePostalCodeRange
        postalCodeFrom
        postalCodeTo
        postalCodeCountry
        geoArea {
          id
          type
          continent
          country
          state
          city
        }
      }
    }
  }
`;

我的期望是有错误的模拟数据并像这样运行它:

    const errorMocksData = [
      {
        request: {
          query: GET_AREA_COMPOSITION,
          variables: {  areaId: 2073, page: 1, first: 9999, }
        },
        error: new Error("An error occurred")
      }
    ];

     it("Should display error on failure if graphql query data failed", async () => {
   render(
    render(
      <MockedProvider>
         <MyComponent {...props} />
      </MockedProvider>
     ))
     
     expect(await screen.findByText("No more mocked")).toBeVisible();
  });

但是,我收到了这个错误:

 No more mocked responses for the query: query getAreaComposition($areaId: ID!) {
      areaComposition(areaId: $areaId, page: 1, first: 999999) {
        data {
          id
          areaCompositionRule
          usePostalCodeRange
          postalCodeFrom
          postalCodeTo
          postalCodeCountry
          geoArea {
            id
            type
            continent
            country
            state
            city
            __typename
          }
          __typename
        }
        __typename
      }
    }
    , variables: {"areaId":"2073"}

为什么我不再得到测试查询的模拟响应?如何修复错误?

标签: apollo

解决方案


推荐阅读