1

I usually write my tests and run them against a local validator using solana-test-validator in a terminal and then is use anchor test --skip-local-validator. Like this I can look at the transactions in the solana explorer and without the skip-local-validator i often got errors.

Problem is when I have PDAs in my test which have a static seed the second run of the test of course always fails because the PDA already exists.

I though of a few things, like restarting the validator with -r or adding some random seeds to my PDAs, but I didnt find a nice solution. Im sure other people ran into this before. Maybe i could close the accounts at the end of the tests. Or maybe there is some framework that can help with that.

Thanks

1 Answer 1

1

The ultimate goal should be to isolate all tests. I believe that Anchor forces you to use just one test-validator for the whole run of your test, which limits your options. You could:

  • use a static accounts file to load the account automatically at the start of your run. The downside here is that you have to remember to keep the file in sync, and if you don't update it, your tests may not catch regressions
  • create the PDA at the start and close it at the end. I don't recommend this approach, because it forces your tests to run serially. If they run concurrently, then your tests will compete to create the PDA, and potentially tear it down for each other!

Given all of that, I think your best option is to slightly redesign the program, so that no PDA is totally static. So rather than find_program_address("some string") for the entire program, you can have find_program_address("some string", some_pubkey), so your program effectively "namespaces" for some_pubkey. Then your tests can all use a different pubkey, and live completely independently from each other.

1
  • I like that, thank you. Its not nice to change code only for tests, but looks like the best solution
    – Jonas H.
    Apr 25, 2023 at 11:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.