Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing tutorial with jest: feedback and questions #770

Open
0xsven opened this issue Jan 4, 2021 · 1 comment
Open

Testing tutorial with jest: feedback and questions #770

0xsven opened this issue Jan 4, 2021 · 1 comment

Comments

@0xsven
Copy link

0xsven commented Jan 4, 2021

I like turtles! And nexus. I just went through most of the tutorial and am very impressed. The tutorial is great. Only the testing section is problematic for me.

FirstI am going to describe an issue that I solved already, and second another issue I couldn't solve. I would appreciate some help on this. I am also willing to send a PR for the documentation once I got it running.

1. (Solved) Error once I copy-pasted the tutorial code into my project:

Screenshot 2021-01-04 at 18 55 21

This was solved by passing the prisma client aka db into graphqlTestContext.before as described below (look at the lines with the green checkmarks ✅):

export function createTestContext(): TestContext {

  let ctx = {} as TestContext;
  const graphqlCtx = graphqlTestContext();
  const prismaCtx = prismaTestContext();

  beforeEach(async () => {

    const db = await prismaCtx.before();
    const client = await graphqlCtx.before(db); // ✅ Here... 

    Object.assign(ctx, {
      client,
      db,
    });
  });

  afterEach(async () => {
    await graphqlCtx.after();
    await prismaCtx.after();
  });

  return ctx;
}

function graphqlTestContext() {

  let serverInstance: ServerInfo | null = null;

  return {
    async before(db: PrismaClient) {  // ✅ ...and here
      const port = await getPort({ port: makeRange(4000, 6000) });
      serverInstance = await server.listen({ port });
     // Close the Prisma Client connection when the Apollo Server is closed
     serverInstance.server.on("close", async () => {
       db.$disconnect() 
     });
      return new GraphQLClient(`http://localhost:${port}`);
    },
    async after() {
      serverInstance?.server.close();
    },
  };
}

I am actually not sure why we cant just close the db connection in prismaTestContext.after ?

2. Port 4000 already in use.

The tutorial works fine for just one test. Once you add more than one test, those fail with the following error listen EADDRINUSE address already in use :::4000

Screenshot 2021-01-04 at 19 37 29

The tutorial code uses get-port to get an available code. After reading through get-code's github issues I tried the solution described here. Unfortunately without success. Can you give me a hint on how to run this setup with multiple test files and tests.

@dreilacadin
Copy link

I have the same question. Anyone who has an insight regarding this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants