DEV Community

Cover image for Create a Github Action to run Solana Anchor tests
Lazar Lukic
Lazar Lukic

Posted on

Create a Github Action to run Solana Anchor tests

Hey there!

In this article, we'll cover how to create a GitHub Action which will run our Anchor program tests!

Anchor Github Action

First create a action.yml file in your project root .github/workflows/action.yml which contains the following:

name: Solana Action name
on:
  push:
    branches: [main]

env:
  SOLANA_VERSION: v1.9.9
  ANCHOR_VERSION: 0.24.2

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Use Node.js
      - uses: actions/setup-node@v3
        with:
          node-version: 16.14.2
          cache: "npm"
      - run: npm install
      - run: curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
        shell: bash
      - run: echo "/home/runner/.cargo/bin" >> $GITHUB_PATH
        shell: bash
      - run: curl -sSfL https://release.solana.com/$SOLANA_VERSION/install | sh
        shell: bash
      - run: echo "/home/runner/.local/share/solana/install/active_release/bin" >> $GITHUB_PATH
        shell: bash
      - run: npm i -g @project-serum/anchor-cli@$ANCHOR_VERSION ts-mocha typescript
      - run: anchor test

Enter fullscreen mode Exit fullscreen mode

First we use actions/setup-node@v3 for a Node.JS environment considering we will be running out tests with mocha.

After that we install Rust by running curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y.

Next the Solana CLI with curl -sSfL https://release.solana.com/$SOLANA_VERSION/install | sh.

And final dependency is Anchor CLI and ts-mocha.

To run the tests, we execute the anchor test.

Anchor.toml

It's important that we properly setup our Anchor.toml file to spawn a local Solana validator that will run tests. Let's take a look at a sample Anchor.toml file. Make sure that the address in your declare_id! macro and in Anchor.toml match.

[programs.localnet]
sample_program = "3ZALfG4NyQgxy2SWjSiQoUyBPEvXT2xo7rKc5hPYYJ7z"

[registry]
url = "https://anchor.projectserum.com"

[provider]
cluster = "localnet"
wallet = "/home/solana/my_wallet.json"

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

Enter fullscreen mode Exit fullscreen mode

Conclusion

In this article we've seen how to write a Github Action, that will run our Anchor test against a local/test Solana validator, as well as how the Anchor.toml file should be.

That's it! Hope you enjoyed it, happy coding!

Top comments (0)