7
votes

I write integration tests using apollo-server-testing package follow this official docs. But when I pass the instance of the ApolloServer class to createTestClient method, tsc throw a type incompatible error.

Argument of type 'ApolloServer' is not assignable to parameter of type 'ApolloServerBase'. Types of property 'requestOptions' are incompatible. Type 'Partial>' is not assignable to type 'Partial>'. Types of property 'documentStore' are incompatible. Type 'import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/apollo-server-caching/dist/InMemoryLRUCache").InMemoryLRUCache | undefined' is not assignable to type 'import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/apollo-server-testing/node_modules/apollo-server-caching/dist/InMemoryLRUCache").InMemoryLRUCache ...'. Type 'import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/apollo-server-caching/dist/InMemoryLRUCache").InMemoryLRUCache' is not assignable to type 'import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/apollo-server-testing/node_modules/apollo-server-caching/dist/InMemoryLRUCache").InMemoryLRUCache'.

Here are the minimal reproducing code:

topic.integration.test.ts:

import { constructTestServer } from '../../../__utils';
import { createTestClient } from 'apollo-server-testing';

describe('topic integration test suites', () => {
  it('should ', () => {
    const { server, cnodeAPI } = constructTestServer();
    const { query } = createTestClient(server); // tsc throw an error here
  });
});

__util.ts:

import { ApolloServer } from 'apollo-server';
import { contextFunction as defaultContext } from '../graphql/context';
import { schema } from '../graphql/schema';
import { CnodeAPI } from '../graphql/api';

const constructTestServer = ({ context = defaultContext } = {}) => {
  const cnodeAPI = new CnodeAPI();

  const server = new ApolloServer({
    schema,
    dataSources: () => ({ cnodeAPI }),
    context,
  });

  return { server, cnodeAPI };
};

export { constructTestServer };

Dependencies versions:

"apollo-datasource-rest": "^0.6.10",
"apollo-server": "^2.9.14",
"apollo-server-express": "^2.9.14",
"apollo-server-testing": "^2.9.15",
"typescript": "^3.7.4"

The interface of createTestClient function is:

import { ApolloServerBase } from 'apollo-server-core';
// ...
(server: ApolloServerBase): ApolloServerTestClient

So when I try to do the type assertions like this:

import { ApolloServerBase } from 'apollo-server-core';
// ...
createTestClient(server as ApolloServerBase);

tsc throw a new type error:

Argument of type 'import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/apollo-server-core/dist/ApolloServer").ApolloServerBase' is not assignable to parameter of type 'import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/apollo-server-testing/node_modules/apollo-server-core/dist/ApolloServer").ApolloServerBase'. Types of property 'requestOptions' are incompatible. Type 'Partial>' is not assignable to type 'Partial>'. Types of property 'documentStore' are incompatible. Type 'import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/apollo-server-caching/dist/InMemoryLRUCache").InMemoryLRUCache | undefined' is not assignable to type 'import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/apollo-server-testing/node_modules/apollo-server-caching/dist/InMemoryLRUCache").InMemoryLRUCache ...'. Type 'import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/apollo-server-caching/dist/InMemoryLRUCache").InMemoryLRUCache' is not assignable to type 'import("/Users/ldu020/workspace/github.com/mrdulin/cnodejs-graphql-api-apollo/node_modules/apollo-server-testing/node_modules/apollo-server-caching/dist/InMemoryLRUCache").InMemoryLRUCache'.

I don't want to do type assertions like createTestClient(server as any);. It works, but I want to make the type correctly.

1

1 Answers

10
votes

I think this problem caused by different versions between apollo-server-express and apollo-server-testing. I've updated to apollo-server-express and apollo-server-testing to 2.9.16. Then, this type error has disappeared. Please try it.