我使用 apollo-server-testing
包编写集成测试,遵循官方 docs 。但是当我将 ApolloServer
类的实例传递给 createTestClient
方法时,tsc
抛出类型不兼容的错误。
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'.
以下是最小的复制代码:
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 };
依赖版本:
"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"
createTestClient
函数的接口(interface)为:
import { ApolloServerBase } from 'apollo-server-core';
// ...
(server: ApolloServerBase): ApolloServerTestClient
所以当我尝试执行这样的类型断言时:
import { ApolloServerBase } from 'apollo-server-core';
// ...
createTestClient(server as ApolloServerBase);
tsc
抛出新类型错误:
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'.
我不想执行像 createTestClient(server as any);
这样的类型断言。它有效,但我想正确设置类型。
最佳答案
我认为这个问题是由apollo-server-express
和apollo-server-testing
之间的版本不同引起的。
我已将 apollo-server-express 和 apollo-server-testing 更新至 2.9.16。然后,这种类型的错误就消失了。
请尝试一下。
关于typescript - 类型 'ApolloServer' 的参数不可分配给类型 'ApolloServerBase' 的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59608947/