You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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...
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
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 thePrismaClient
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 whateverprocess.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 createdPrismaClient
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.
The text was updated successfully, but these errors were encountered: