-2
let server;

export const startServer = () => {

  return createConnection()
    .then(async () => {
      log('Connection to DB established');
      const schema = await buildSchema({
        resolvers: [
          ...
        ],
        globalMiddlewares: [ResolveTime],
        authChecker: authChecker,
      });

      server = new ApolloServer({
        schema,
      });

      log('Server created');

 
      await server.listen(process.env.PORT);

      log('Server has started! on PORT:', process.env.PORT);

      return server;
    })
    .catch((error) => console.log(error));
};

export const stopServer = () => {
  console.log('Stopping');
  server && server.stop();
};

// This prints 'Getting!! false'
export const getServer = () => {
  console.log('Getting!!', !!server);
  return server;
};

The output

Connection to DB established
Schema built
Server created
Server has started! on PORT: 8080

And then after that it prints

'Getting!! false'

So it is super odd. I definitely await the startServer command before executing the test.

I can't figure out why server is undefined.

stopServer works perfectly fine and I know I am calling getServer before stopServer and after startServer.

bezzoon
  • 1,755
  • 4
  • 24
  • 52

0 Answers0