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

Documented test setup example problem #556

Open
wfischer42 opened this issue Oct 15, 2020 · 1 comment
Open

Documented test setup example problem #556

wfischer42 opened this issue Oct 15, 2020 · 1 comment

Comments

@wfischer42
Copy link

wfischer42 commented Oct 15, 2020

The setup provided in the docs will only work for the first test in a suite. All subsequent tests fail. It took me days to track down why, but it’s basically because this line

serverInstance = await server.listen({ port });

invokes a server implementation that uses a generic PrismaClient instance from your main app server, not the PrismaClient you just created. I'm guessing that the PrismaClient executes once per process because of how node handles the exports, and Jest bundles test-suites per process, so the extra client will be created with whatever process.env.DATABASE_URL is saved at the time of the first test setup. (All of this means that if you import or define your server or database differently in your project setup, you may experience different symptoms.)

My solution is basically to create a server generator function (instead of exporting the app's server) that takes an optional PrismaClient instance as an argument and use that as the database. Then in the test setup you can pass the newly created PrismaClient to the server. Without that on the server definition, every test will attempt to refer to the client created in the first test block, even after it’s been torn down.

I'd love to see an "official" docs update that accounts for this, but is maybe a little less sloppy than mine. If I manage to refactor to something with a modicum of elegance, I'll submit a PR on the docs.

@jk21
Copy link

jk21 commented Nov 4, 2020

hi, can you elaborate your setup? i am also struggeling with setting up the tests.
running it with apollo-server-express but this makes it even harder...

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