单元测试 Apollo,在 mutation mock 上出错

Unit testing Apollo, errorring on mutation mock

我正在尝试为我的单元测试模拟 graphql 突变。我正在使用 MockedProvider 但出现错误:'UnhandledPromiseRejectionWarning: Error: No more mocked responses for the query: mutation LoginMutation'

我进行了大量的谷歌搜索,并将所有内容复制粘贴到一个文件中,这样导入就不会出现问题

const LOGIN_MUTATION = gql`
    mutation LoginMutation($email: String!, $password: String!) {
    login(email: $email, password: $password) {
      token
      user {
        name
      }
    }
  }
`;

const mocks = [
  {
    request: {
      query: LOGIN_MUTATION,
      variables: { email: 'test@test.com', password: 'password' },
    },
    result: {
      data: {
        login: {
          token: 'fakeToken',
          user: {
            name: 'Testy McTestface'
          }
        }
      }
    }
  }
]

describe('LoginForm', () => {
  let wrapper;
  let store;

  beforeEach(() => {
    store = mockStore(initialState);
  });

  it('triggers the doLogin action creator after the graphql call', () => {
    const mockDoLogin = jest.fn()
    let mockedWrapper = mount(
      <MockedProvider mocks={mocks} addTypename={false}>
        <Provider store={store}>
          <LoginForm doLogin={mockDoLogin} />
        </Provider>
      </MockedProvider>,
    );
    mockedWrapper.find('#loginButton').simulate('click')
    expect(mockDoLogin).toHaveBeenCalledTimes(1)
  })
});

我希望这个测试通过(在包装器上搜索的标签是正确的)但是我得到了上述错误,所以我假设问题出在我的模拟上,但它看起来不错?

抱歉,如果这太明显了,但我已经盯着它看了好几个小时了。

你想试试 easygraphql-tester 吗?它会为你模拟查询,而且,如果你想设置一些固定装置,你也可以设置它们!

您可能会传递架构以便使用它!

import EasyGraphQLTester from 'easygraphql-tester'

const tester = new EasyGraphQLTester(schema)

const LOGIN_MUTATION = gql`
    mutation LoginMutation($email: String!, $password: String!) {
    login(email: $email, password: $password) {
      token
      user {
        name
      }
    }
  }
`;

const mocks = [
  {
    request: {
      query: LOGIN_MUTATION,
      variables: { email: 'test@test.com', password: 'password' },
    },
    result: tester.mock({
      query: LOGIN_MUTATION,
      variables: { email: 'test@test.com', password: 'password' }
    })
  }
]