Skip to content
This repository was archived by the owner on Apr 13, 2023. It is now read-only.
This repository was archived by the owner on Apr 13, 2023. It is now read-only.

MockedProvider's mocks prop need not be mutable #2283

@amacleay

Description

@amacleay

Intended outcome:

I have a jest test that looks like the following:

import * as React from 'react';
import { MockedProvider } from 'react-apollo/test-utils';
import { MockedResponse } from 'react-apollo/test-links';
import { mount } from 'enzyme';

const mocks: ReadonlyArray<MockedResponse> = [];

describe('myComponent', () => {
  it('works', () => {
    mount(
      <MockedProvider mocks={mocks} addTypename={false}>
        <h1>Yo</h1>
      </MockedProvider>,
    );
  });
});

mocks is defensively coded as a ReadonlyArray. This should work fine as mocks is read in and not mutated: https://github.com/apollographql/react-apollo/blob/master/src/test-links.ts#L39

Expect this to compile.

Actual outcome:
Typescript fails to compile

test/src/utils/queryComponentFactory.test.tsx:11:23 - error TS2326: Types of property 'mocks' are incompatible.
  Type 'ReadonlyArray<MockedResponse>' is not assignable to type 'MockedResponse[] | undefined'.
    Type 'ReadonlyArray<MockedResponse>' is not assignable to type 'MockedResponse[]'.

11       <MockedProvider mocks={mocks} addTypename={false}>
                         ~~~~~~~~~~~~~


test/src/utils/queryComponentFactory.test.tsx:11:23 - error TS2326: Types of property 'mocks' are incompatible.
  Type 'ReadonlyArray<MockedResponse>' is not assignable to type 'MockedResponse[] | undefined'.
    Type 'ReadonlyArray<MockedResponse>' is not assignable to type 'MockedResponse[]'.
      Property 'push' is missing in type 'ReadonlyArray<MockedResponse>'.

11       <MockedProvider mocks={mocks} addTypename={false}>

How to reproduce the issue:
Can actually be reproduced in the repo's unit tests by making mocks readonly in https://github.com/apollographql/react-apollo/blob/master/test/test-utils.test.tsx#L56

diff --git a/test/test-utils.test.tsx b/test/test-utils.test.tsx
index c0ef7c6..0241d9e 100644
--- a/test/test-utils.test.tsx
+++ b/test/test-utils.test.tsx
@@ -6,6 +6,7 @@ import gql from 'graphql-tag';

 import { graphql, ChildProps } from '../src';
 import { MockedProvider, mockSingleLink } from '../src/test-utils';
+import { MockedResponse } from '../src/test-links';
 import { DocumentNode } from 'graphql';

 const variables = {
@@ -53,7 +54,7 @@ const withUser = graphql<Variables, Data, Variables>(query, {
   }),
 });

-const mocks = [
+const mocks: ReadonlyArray<MockedResponse> = [
   {
     request: {
       query,

Version

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueIssues that are suitable for first-time contributors.has-reproduction❤ Has a reproduction in a codesandbox or single minimal repository

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions