1

Having a project that has grown a bit larger I have modularized the tests into separate files. Calling the test script works fine with running over all files. But I couldn't figure out or find anything on how to run tests partially.

Using this project as an example: https://github.com/tobealive/anchor-solana-twitter/

Being able to run a single - on the fly specified - test file is the nice-to-have I'm looking for here. Or running parts of a test when using a single file containing all unit tests.

A predigested solution would be top-notch. Also, every tip about resources one should look into to achieve this would be of great help.

2 Answers 2

6

You likely have something like this in your Anchor.toml file:

[scripts]  
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"

Change the wildcard (*) character to specify exactly what test file you want to run.

For example if you have multiple test files test1.ts, test2.ts, and test3.ts all located in your test folder that you want to run selectively at different times, then you would need to edit your Anchor.toml file to:

[scripts]  
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/**.ts"  
test1 = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/test1.ts"  
test2 = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/test2.ts"  
test3 = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/test3.ts"

This way you can use anchor run test to run all tests at once, anchor run test1 to run the test1.ts file, anchor run test2 to run the test2.ts file, and so on.

3
  • Thanks for the suggestion. Unfortunately, this would require always changing the Anchor.toml file to be able to test a single unit and also if one wants to run all tests again. I think handling it statically like this is too much of a bummer to be a real solution to the problem. But it certainly could be automated in a script that does a change temporary for a test run and then restores the wildcard.
    – tenxsoydev
    Sep 10, 2022 at 15:06
  • You would just need to hardcode different scripts and run them selectively. I'll update the answer to show how
    – Ademola
    Sep 10, 2022 at 20:08
  • This solution no longer works in Anchor 0.29.0 See my post solana.stackexchange.com/questions/8088/…
    – Russo
    Nov 15, 2023 at 5:06
1

Modify your Anchor.toml, exmaple:

[scripts]
create = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/Blockchain/Create.ts"
2
  • This comes close to a solution. It won't allow running it like anchor test, but when having an anchor localnet running, one would be able to quickly run single test files with e.g., anchor run create or other scripts added to the section
    – tenxsoydev
    Sep 10, 2022 at 15:12
  • 1
    you got it. that's how it is Sep 10, 2022 at 21:52

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.