Hi,
I'm running some unit tests with yoga and I'd like to close the server programatically but the http server isn't accessible, and express doesn't offer .close(). Would you be open to a pr that adds the httpServer as a property? or is there a better way to achieve this result?
This is how I have got access to the server instance in order to close it.
const serverInstance: any = server.start();
export default serverInstance;
Then the following would go in your test file (or something similar):
import { AfterAll, BeforeAll } from 'cucumber';
import serverInstance from './server';
AfterAll(async () => {
const server: any = await serverInstance;
server.close();
});
Thanks a lot for providing an answer here @douglaseggleton. Does this answer your question @hobochild or do you have a suggestion on how the API could be improved?
Ah interesting, I didn't see that .start returns the http instance. That works fine, thanks for the help! @douglaseggleton @schickling
Hey everyone,
Thank you for the information, but the suggested code from @douglaseggleton did not work for me. I either get TypeError: serverInstance.close is not a function when closing or TypeError: serverInstance.address is not a function from supertest. I am using the latest version of graphql-yoga
and my testing setup is supertest and jest. Can someone provide a complete example/snippet?
Appreciate it!
@JorgeCeja can you create a repo with a reproduction so we can look into your issue?
@JorgeCeja,
The http instance is returned from the server.start() method and not the server constructor. So check that you are invoking the method on the http instance and not the graphql server instance.
const server = new GraphQLServer({ typeDefs, resolvers })
server.start()
server.close() // wont work
const server = new GraphQLServer({ typeDefs, resolvers })
const httpServer = server.start()
httpServer.close() // will work
@hobochild
How do you clean the db after the unit test?
Do you connect the prisma server?
I am not sure how to test resolver in graphql-yoga.
Thanks
Is there any other way to get the http instance thant start ? Because for my tests with supertest i don't want to start my server explicitly.
In case anyone runs into this now, start returns a promise with the http server
let httpServer = await server.start(...)
httpServer.close() // this works