Graphql-yoga: Provide graphQlServer.stop() functionality

Created on 26 Jul 2019  路  5Comments  路  Source: dotansimha/graphql-yoga

When testing, it is useful to call graphQlServer.start({ port: 0 }) within the beforeAll / beforeEach sections of tests. It would be great to be able to stop the server in an afterAll / afterEach by calling graphQlServer.stop().

The function should kill any running process initiated by start().

In the meantime, is there any existing way to kill the server after calling start()?

Most helpful comment

It is possible to directly access the http server instance which is resolved in the promise returned by server.start like this:

const httpServer = await graphqlServer.start(({ port }) => console.log(`Server is running on http://localhost:${port}`));
// ... your code
httpServer.close();

Edit: This approach works for me w/ Jest and setting up the server in beforeAll and closing it in afterAll:

  let server;

  beforeAll(await () => server = await startGraphQLServer());
  afterAll(() => server.close());

All 5 comments

This is specifically referring to integration testing. Happy to hear any other ideas on how to get integration testing up and running!

Can't believe this is not present. I also need to stop the server in integration tests.

It is possible to directly access the http server instance which is resolved in the promise returned by server.start like this:

const httpServer = await graphqlServer.start(({ port }) => console.log(`Server is running on http://localhost:${port}`));
// ... your code
httpServer.close();

Edit: This approach works for me w/ Jest and setting up the server in beforeAll and closing it in afterAll:

  let server;

  beforeAll(await () => server = await startGraphQLServer());
  afterAll(() => server.close());

@phikes I thought the return type was void

Screen Shot 2021-02-23 at 7 19 47 PM

EDIT:

@phikes thanks! but confirming that it was my fault. I abstracted it in my own class but forgot to return 馃ぃ

It might be that the API changed in the meantime, I don't currently use this anymore.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ahmedosama5200 picture ahmedosama5200  路  4Comments

ramonmulia picture ramonmulia  路  3Comments

joshhopkins picture joshhopkins  路  3Comments

frederikhors picture frederikhors  路  4Comments

asci picture asci  路  3Comments