Intended outcome:
I am able to close apollo-client gracefully.
Here is a sample of a code I'm using:
// Running under nodejs (not a browser)
require('isomorphic-fetch');
global.WebSocket = require('ws');
import gql from 'graphql-tag';
import ApolloClient from 'apollo-client';
import { InMemoryCache, NormalizedCacheObject } from 'apollo-cache-inmemory';
import { split } from 'apollo-link';
import { HttpLink } from 'apollo-link-http';
import { WebSocketLink } from 'apollo-link-ws';
import { getMainDefinition } from 'apollo-utilities';
const httpLink = new HttpLink({
uri: 'http://localhost:4000/',
});
const wsLink = new WebSocketLink({
uri: 'ws://localhost:4000/graphql',
options: {
reconnect: true,
},
});
const link = split(
({ query }) => {
const definition = getMainDefinition(query);
return (
definition.kind === 'OperationDefinition' &&
definition.operation === 'subscription'
);
},
wsLink,
httpLink,
);
let client!: ApolloClient<NormalizedCacheObject>;
const booksQuery = gql`
query books {
books {
title
author
}
}
`;
beforeAll(() => {
client = new ApolloClient({
link,
cache: new InMemoryCache(),
});
});
afterAll(async () => {
await client.clearStore();
client.stop();
});
test('books', async () => {
const resp = await client.query({
query: booksQuery,
});
console.log(resp.data.books);
});
Actual outcome:
When using apollo-client with jest as per example above I get the following warning:
Jest did not exit one second after the test run has completed.
This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.
How to reproduce the issue:
Run the code above.
Versions
$ npx envinfo@latest --preset apollo --clipboard
npx: installed 1 in 0.873s
System:
OS: macOS 10.15.3
Binaries:
Node: 12.16.2 - /opt/local/bin/node
npm: 6.14.4 - /opt/local/bin/npm
Browsers:
Chrome: 81.0.4044.122
Firefox: 74.0.1
Safari: 13.0.5
npmPackages:
apollo-boost: ^0.4.7 => 0.4.7
apollo-link-ws: ^1.0.20 => 1.0.20
npmGlobalPackages:
apollo: 2.27.3
Does anybody know how to properly close apollo client so I don't get this warning?
For those who hit the same issue
(wsLink as any).subscriptionClient.close();
For those using NestJs make sure to close the app last, only then the msg will disappear
afterAll(async () => {
await apolloClient.clearStore();
apolloClient.stop();
(wsLink as any).subscriptionClient.close();
await app.close(); // <---
});
Most helpful comment
For those who hit the same issue