Has anyone setup a testing environment with its ow...
# orm-help
d
Has anyone setup a testing environment with its own prisma / mongo container initialization?
u
What do you mean more specifically? On a server? Locally? Are you after the docker-compose configuration?
d
Locally yes
I would like to be able to run tests, with a test server, but each time i run all the tests, there should be a fresh DB
or maybe just a DB that gets cleared out at the start of the tests?
Just curious if someone had a cool setup, was going to model the resolver testing after this:
so my idea was either spin up a new container for mongo and prisma, run prisma deploy which hooks the prisma generate command, then starts yoga server, then runs test
d
im working atm on something like this, but i dont create a server on each test, i only reset the database, thats already working with prisma cli. https://www.prisma.io/docs/prisma-cli-and-configuration/cli-command-reference/prisma-seed-xcv8/#$-prisma-seed
with lib like execa, its easy to do with the beforeEach or whatever call you like
https://github.com/entria/graphql-dataloader-boilerplate/blob/master/test/helper.js Got the idea from there just replace all mongoose stuff with the prisma cli commands
d
ahh
checking this out now. Thank you @domko
@domko do you have a separate docker compose for the testing servers?
d
no its a stage for me
endpoint variable, you can change this, if you set ${env:ENDPOINT} there and create a .env.test with an endpoint http://localhost:4466/project/service/stage
then simple call all prisma commands with --env-file .env.test
d
Before all that, I need to create separate containers for prisma and mongo locally correct?
d
yeah ive only 1 prisma and 1 mongo docker-container
d
so a total of 4 containers locally, 2 for dev and 2 for testing
d
stages create their own database
d
ah
d
no ive only 2 in total
d
so you share the servers, just swap out he database
d
yes correct
d
gotcha
d
so im sure the logic on server is also same and only data is diffrent
d
What do you mean?
Can you share your docker compose file by chance?
d
Here you have example with env files
only thing you need to do twice is the deploy command
d
taking a look now, thank you so much btw
d
i.E as an npm command: "deploy": "prisma deploy --env-file .env && prisma deploy --env-file .env.test"
d
gotcha, so where in this is the second DB created?
d
with the deployment
d
I see the 2 different prisma DATABASE_URIs
d
yes and now look at the deploy command i just send, they've 2 deployments with this diffrent .env files
d
and that is what you are suppling new Prisma endpoint
so prisma is scoping based on that
/db/dev
and
/db/test
you can simple just change the prisma endpoint to anything i want and it will from that point scope the database?
full disclosure, im new to mongo, i come from the PG world
d
I would also like to add this is not only super useful to myself, this absolutely should be included on the Prisma website as a tutorial. Super helpful
ah, missed that. okay let me give this a try
d
yeah think the testing stuff will also implemented, there is already an "spec" issue, they want to do testing out of the box with fake data from graphql-faker
d
basically im using
/default/default
by default
that would be super cool
d
yeah if you dont set /project/service/stage, you always use the default/default
d
I have struggled finding resources to accomplish testing with prisma
now this is making sense
d
just dont mess up, the /default/default is only /project/service
no stage there
d
gotcha, so can i do this:
DATABASE_URI=<http://localhost:4466/newco/db/dev>
DATABASE_URI=<http://localhost:4466/newco/db/test>
and in my prisma.yml files just set enpoint to this
d
yeah the endpoint on client side would be "http://localhost:4466/db/dev" and "http://localhost:4466/db/test" project name is gone in the client url
d
Copy code
endpoint: ${env:ENDPOINT}
interesting
curious, why does project name drop?
d
i dont know, was kinda searching for the reason.. but couldnt find
d
kinda odd
d
there is also a bug in there, cause the end result of prisma instance looks like this:
Copy code
export const Prisma = makePrismaClientClass<ClientConstructor<Prisma>>({
  typeDefs,
  models,
  endpoint: '${process.env['DATABASE_URI']}'
})
what is wrong, cause like i said, project name is gone
d
damn so we need 2 ENV vars
one for deploy and one for the prisma client
d
yeah sadly for now yes, but will create an issue later on today for this
d
interesting. okay trying this now…
so i have the following
prisma.yml
Copy code
endpoint: ${env:PRISMA_SERVER_ENDPOINT}
server.js
Copy code
const db = new Prisma({
	endpoint: process.env.PRISMA_CLIENT_ENDPOINT,
	secret: process.env.PRISMA_SERVER_SECRET,
	debug: true,
})
Copy code
PRISMA_SERVER_ENDPOINT=<http://localhost:4466/newco/db/dev>
PRISMA_CLIENT_ENDPOINT=<http://localhost:4466/db/dev>
d
yes
d
deploy worked 🙂
got two playgrounds running now
wow, this was SO helpful
this is very hidden if you ask me
d
yeah thats correct
d
and pretty abstract
d
took me like 3 days, to get this information
d
haha wow
i believe it
you really should do a tutorial on this, it would help so many people
it think by them doing the
default/default
is that causes too much magic and makes it hard to understand
now if your issue gets fixed, it will be even better