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

fix: jest exits after tests run #13

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion api/context.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { PrismaClient } from '@prisma/client'
import { db } from './db'

export interface Context {
db: PrismaClient
}

export function createContext(): Context {
return {
db: new PrismaClient(),
db,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason we're passing a function as the context instead of just passing a regular Object?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because a function is what Apollo Server expects. It is called on every request. Usually work is done here to expose request scoped details to resolvers, like current user.

This needs to be explained better in the tutorial.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually work is done here to expose request scoped details to resolvers, like current user.

Yup, I've actually done that in an app I'm building.

Because a function is what Apollo Server expects.

Apollo Server actually expects either a function or a plain javascript object. It's not really a big deal, but I thought it might've been simpler just passing a context object in this case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know, in the spirit of the tutorial that incrementally builds up on an as-needed basis I would happily accept a PR to make this change!

Copy link
Contributor Author

@shreyas44 shreyas44 Dec 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"dev": "ts-node-dev --transpile-only --no-notify api/app",
"build": "tsc",
"generate": "ts-node --transpile-only api/schema",
"test": "npm run generate && jest --forceExit"
"test": "npm run generate && jest"
},
"jest": {
"preset": "ts-jest",
Expand Down
5 changes: 5 additions & 0 deletions tests/__helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { GraphQLClient } from 'graphql-request'
import { nanoid } from 'nanoid'
import { join } from 'path'
import { Client } from 'pg'
import { db } from '../api/db'
import { server } from '../api/server'

type TestContext = {
Expand Down Expand Up @@ -42,7 +43,11 @@ function graphqlTestContext() {
return {
async before() {
const port = await getPort({ port: makeRange(4000, 6000) })

serverInstance = await server.listen({ port })
serverInstance.server.on('close', async () => {
await db.$disconnect()
})

return new GraphQLClient(`http://localhost:${port}`)
},
Expand Down