Skip to content

MockedLink expects optional variables with default values to be defined in mocked queries #8023

@dannycochran

Description

@dannycochran

Intended outcome:

Given this query:

query GetFoo($id: ID!, $includeFooBar: Boolean = false) {
  ...
}

Be able to provide a mock which does not include a value for $includeFooBar if my actual invocation isn't passing it, e.g:

Actual invocation in my app:

// getFoo.ts

// Never passes `includeFooBar
const response = useQuery(GetFooDocument, { variables: { id: 5 } });

Mocks setup:

// testGetFoo.spec.ts

const mocks: ApolloMockedResponse<any>[] = [{
  response: { ... }
  request: {
    query: GetFooDocument,
    variables: { id: 5 }
  }
}]

This mock should work correctly since my actual invocation is not passing includeFooBar

Actual outcome:

The mock fails because includeFooBar is expected to be explicitly passed in the mock above. The comparison of variables here flushes out the default values and does not account for them being optional:

if (equal(requestVariables, mockedResponseVariables)) {

How to reproduce the issue:

The code snippet above.

Versions

System:
OS: macOS 11.2.2
Binaries:
Node: 12.18.4 - /usr/local/bin/node
Yarn: 1.22.4 - ~/npm-global/bin/yarn
npm: 6.14.6 - /usr/local/bin/npm
Browsers:
Chrome: 90.0.4430.85
Safari: 14.0.3
npmPackages:
@apollo/client: ^3.3.15 => 3.3.15
apollo-upload-client: 14.1.3 => 14.1.3
apollo3-cache-persist: 0.9.1 => 0.9.1
npmGlobalPackages:
apollo: 2.27.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions