我正在为我的nodejs apollo graphql项目编写订阅的集成测试。我正在使用mock-socket-with-protocol
模拟websocket网络层。
下面是我在jest
中的测试代码。当我运行单元测试时,永远不会调用测试用例中的next()
部分。测试用例由于超时而失败。我的代码有什么错误吗?
const pubsub = new PubSub();
const resolvers = {
Subscription: {
somethingChanged: {
subscribe: () => {
return pubsub.asyncIterator('update_event');
},
},
},
};
const typeDefs = gql`
type Topic {
topic: String
}
type Subscription {
somethingChanged: Topic
}
`;
const gqClient = () => {
const customServer = new Server('ws://localhost:8080');
const graphqlSchema = makeExecutableSchema({ typeDefs, resolvers });
SubscriptionServer.create(
{
schema: graphqlSchema,
execute,
subscribe,
},
customServer,
);
const wsLink = new WebSocketLink({
uri: 'ws://localhost:8080',
webSocketImpl: WebSocket,
});
return new ApolloClient({
link: wsLink,
cache: new InMemoryCache(),
});
};
test('test', (done) => {
gqClient()
.subscribe({
query: gql`
subscription {
update {
topic
}
}
`,
})
.subscribe({
next(data) {
// Notify your application with the new arrived data
console.log('get update ', data);
done();
},
error(err) {
console.error(err);
done();
},
});
setTimeout(() => {
pubsub.publish('update_event', {
somethingChanged: { topic: 'hello' },
});
}, 1000);
});
我对这些测试的依赖性:
"devDependencies": {
"@babel/core": "^7.7.4",
"@babel/plugin-proposal-class-properties": "^7.7.4",
"@babel/preset-env": "^7.7.4",
"apollo-cache-inmemory": "^1.6.3",
"apollo-client": "^2.6.4",
"apollo-link-ws": "^1.0.19",
"babel-eslint": "^10.0.3",
"babel-jest": "^24.9.0",
"eslint": "^6.7.1",
"eslint-config-airbnb": "^18.0.1",
"eslint-config-airbnb-base": "^14.0.0",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-react": "^7.14.3",
"eslint-plugin-react-hooks": "^1.7.0",
"husky": "^3.1.0",
"jest": "^24.9.0",
"jest-html-reporter": "^2.7.0",
"mock-socket-with-protocol": "^7.1.1"
}
"dependencies": {
"apollo-server-express": "^2.9.12",
"apollo-server-testing": "^2.9.12",
"express": "^4.17.1",
"graphql": "^14.5.8",
"graphql-subscriptions": "^1.1.0",
"graphql-tag": "^2.10.1",
"graphql-tools": "^4.0.6",
"node-cache": "^5.0.2",
"retry": "^0.12.0",
"subscriptions-transport-ws": "^0.9.16",
"uuid": "^3.3.3",
"ws": "^7.2.0"
},