Hello! Does anyone know how to reset data from tes...
# prisma-whats-new
j
Hello! Does anyone know how to reset data from tests with Jest? I am calling to my Bash script with
prisma reset --env-file ./.env --force
but Prisma returns an error saying "Error: Couldn’t find
prisma.yml
file. Are you in the right directory?". When my script is executed, it is on the root path of the project but I am executing tests under
/tests
. Any idea how to solve this? I have trying with many workarounds but nothing seems to work.
p
Hello! Try as I may, I’m unable to understand your question exactly, I’m sorry. May you try and rephrase? I may be able to help. Thanks!
j
Hi @picosam! Sure. My Bash script is this:
Copy code
#!/bin/bash

prisma reset --env-file ./.env --force
And everything is fine if I run it from the root path:
bash script.sh
. My issue comes when I execute this script from my test file located on
/tests/auth.test.js
.
p
Ok; may I ask why you’re trying to reset the data using a script?
vs. for instance using jest’s
globalSetup
or
testEnvironment
?
I’m guessing that the reason you’re using
prisma reset
is to restore the state of the data to what was previously, for instance, `seed`ed, no?
j
Because I can run my script at any point in time and play with it. I am not sure if it is also possible with
globalSetup
or
testEnvironment
. I really appreciate if you could share with me some examples.
Yes, you are right.
p
I’m working on an example as we’re speaking, to demonstrate setup and teardown to test Auth0 signups/logins; it should be read by Sunday and I will post it on the forum.
👍 1
Are you using an IDE or
yarn test
?
j
I am using
yarn test
and
yarn test --watch
.
p
Ok. I’m afraid the only way I know how to make this work is the way I’ll demonstrate in my example. Let’s see if @nilan may be able to help with the “couldn’t find
prisma.yml
error” you stated.
j
Ok. Let's see. I think Prisma is throwing that error because it reads the path where I execute the command from and not where the script file is placed in.
n
@jferrettiboke
prisma
checks
.graphqlconfig.yml
to find the
prisma.yml
file. try to change the working directory to the directory where
.graphqlconfig.yml
is located 🙂
j
@nilan Thanks for your support. But this is still an issue for me because I am testing and I don't want to place all my test files to the root path...
n
that's not what I'm suggesting. In your bash script, you can change the working directory.
p
@jferrettiboke I think Nilan means this: https://stackoverflow.com/a/16349776/243302
j
Ahh Ok! But my Bash script is located on the root path, the same like
graphqlconfig.yml
. Should I still need to change the working directory?
Ok. I will try that. I will let you know soon.
p
I believe it has more to do with where you’re executing it from (the current working directory) and not where it’s located.
👍 1
j
Directory structure
Copy code
+ /
    + /tests
        - auth.test.js
    - graphqlconfig.yml
    - script.sh
graphqlconfig.yml
Copy code
projects:
  # ...
  database:
    schemaPath: src/generated/prisma.graphql
    includes: ["database/seed.graphql"]
    extensions:
      prisma: database/prisma.yml
script.sh
Copy code
#!/bin/bash

# Reset current stage for current cluster
prisma reset --env-file ./.env --force
auth.test.js
Copy code
//...

beforeAll(done => {
  shell.exec("bash script.sh", function(code, stdout, stderr) {
    console.log("Exit code:", code);
    console.log("Program output:", stdout);
    console.log("Program stderr:", stderr);
    done();
  });
});

//...
- I am on
/
- I execute
yarn test
- I get "Couldn’t find
prisma.yml
file. Are you in the right directory?" Any specific example to help me with this? I don't know exactly what's wrong here.
p
So,
shell.exec
actually allows you to determine what working directory you want to execute from. Can you try
shell.exec("bash script.sh, , {cwd: PUT_PATH_TO_GRAPHQLCONFIG.YML_HERE}, ...
j
No luck. I tried with this:
shell.exec("bash script.sh", { cwd: path.resolve(__dirname, "..") }, ...
.
Still the same message about
prisma.yml
.
p
When you
console.log(path.resolve(__dirname, "..")
do you get the correct path?
j
Yeah. Exactly the same, the root of the project.
I created a new file
script.js
with just this and everything works!
Copy code
const shell = require("shelljs");
const path = require("path");

shell.exec("bash script.sh", { cwd: path.resolve(__dirname, "..") }, function(
  code,
  stdout,
  stderr
) {
  console.log("Exit code:", code);
  console.log("Program output:", stdout);
  console.log("Program stderr:", stderr);
});
So, is this a Jest issue? :S
p
I’m unable to find reference to a similar issue online to be honest…
j
I changed `shelljs`for
child_process
and still the same but it works on
script.js
without Jest. I think it is something related with Jest. Pretty weird...
Thank you anyway to both of you. 💚
👍 2