Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MockedProvider does not validate GraphQL queries #7591

Open
richardwu opened this issue Jan 19, 2021 · 0 comments
Open

MockedProvider does not validate GraphQL queries #7591

richardwu opened this issue Jan 19, 2021 · 0 comments

Comments

@richardwu
Copy link

Intended outcome:

I am testing my React Native app which includes Apollo queries with Jest, and I noticed that the MockedProvider does not validate the GraphQL query, such as for undefined references. Here is a reproduce-able test file using react-testing-library and MockedProvider:

import React from "react";

import { render } from "@testing-library/react-native";
import "@testing-library/jest-dom";

import { MockedProvider } from "@apollo/client/testing";
import { View, Text } from "react-native";

import { gql, useQuery } from "@apollo/client";

const BAD_QUERY = gql`
  query bob($foo1: String!, $foo2: String!) {
    bob(foo1: $undefined_foo1, foo2: $undefined_foo2) {
      bar
    }
  }
`;

const mocks = [
  {
    request: {
      query: BAD_QUERY,
      variables: {
        foo1: "abc",
        foo2: "abc",
      },
    },
    result: {
      data: {
        bob: { bar: 1 },
      },
    },
  },
];

const TestComp = () => {
  const { data } = useQuery(BAD_QUERY, {
    variables: { foo1: "abc", foo2: "abc" },
  });

  return (
    <View>
      <Text>{data?.bob.bar}</Text>
    </View>
  );
};

describe("TestComp", () => {
  test("test comp", async () => {
    const { findByText } = render(
      <MockedProvider mocks={mocks} addTypename={false}>
        <TestComp />
      </MockedProvider>
    );

    expect(await findByText("1")).toBeTruthy();
  });
});

Actual outcome:
Even though the above BAD_QUERY includes undefined references $undefined_foo1, $undefined_foo2, the test still passes. However, if rendering TestComp with a non-mocked Apollo client the correct undefined GraphQL error is thrown:

[GraphQL error]: Message: Variable "$undefined_foo1" is not defined by operation "bob".

How to reproduce the issue:

See the above code block.

Versions

  System:
    OS: Linux 4.19 Ubuntu 20.04.1 LTS (Focal Fossa)
  Binaries:
    Node: 15.0.1 - ~/.nvm/versions/node/v15.0.1/bin/node
    Yarn: 1.22.5 - /usr/bin/yarn
    npm: 7.0.3 - ~/.nvm/versions/node/v15.0.1/bin/npm
  Browsers:
    Firefox: 84.0
  npmPackages:
    @apollo/client: ^3.2.5 => 3.2.5

Using SDK 40.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants