2

I am using anchor, bankrun and jest to test my contract on solana. How can I get better errors or exact problems in my tests.

code

import { startAnchor } from "anchor-bankrun";
import { PublicKey } from "@solana/web3.js";
import { BankrunProvider } from "anchor-bankrun";
import { RoyaltyNft } from "../target/types/royalty_nft";
import { Program } from "@coral-xyz/anchor";
import { expect, describe, test, beforeEach } from "@jest/globals";

import * as anchor from "@coral-xyz/anchor";
import { Console, log } from "console";

const ROYALTY_PROGRAM_ID = new PublicKey(
  "GaMVToeXNUeCjzduQegv8tURziz9mCrbgtzUF6LFEx29"
);
describe("royalty_nft", () => {
  let context;
  let provider: BankrunProvider;
  let program: Program<RoyaltyNft>;
  let payer: anchor.Wallet;

  test("initiate contract", async () => {
    console.log("intializing contract");
    context = await startAnchor("./", [], []);
    provider = new BankrunProvider(context);

    program = anchor.workspace.RoyaltyNft as Program<RoyaltyNft>;
    payer = provider.wallet;
  });

  test("create_nft", async () => {
    console.log("payer is", payer.publicKey.toBase58());
    console.log("program is", program.programId.toBase58());

    context = await startAnchor("./", [], []);
    provider = new BankrunProvider(context);

    program = anchor.workspace.RoyaltyNft as Program<RoyaltyNft>;
    payer = provider.wallet;

    const [mintPda, mintPdaBump] = await PublicKey.findProgramAddress(
      [Buffer.from("mint")],
      program.programId
    );

    const [tokenAccountPda, tokenAccountBump] =
      await PublicKey.findProgramAddress(
        [
          payer.publicKey.toBuffer(),
          program.programId.toBuffer(),
          mintPda.toBuffer(),
        ],
        anchor.utils.token.TOKEN_PROGRAM_ID
      );
    console.log("mintPda", mintPda.toBase58());
    console.log("tokenAccountPda", tokenAccountPda.toBase58());
    try {
      const tx = await program.methods
        .createNft("SYM", "test-token", "test-uri")
        .accounts({
          payer: payer.publicKey,
          mint: mintPda,
          tokenAccount: tokenAccountPda,
        })
        .rpc({ skipPreflight: true });
      expect(tx).toBeDefined();
    } catch (err) {
      console.error("Transaction failed", err);
      throw err;
    }
  }, 40000);
});

I think I have a pretty reasonable timeout attached to the test since it's running on a bankrun server that's supposed to be way faster that a regular network. The following is what my anchor test --skip-local-validator --skip-deploy shows up.

prompt recieved

warning: `royalty_nft` (lib test) generated 5 warnings (run `cargo fix --lib -p royalty_nft --tests` to apply 3 suggestions)
    Finished `test` profile [unoptimized + debuginfo] target(s) in 0.59s
     Running unittests src/lib.rs (/Users/deauth/Developer/solana/anchor/royalty_nft/target/debug/deps/royalty_nft-56c58f9ccee5bd6a)

Found a 'test' script in the Anchor.toml. Running it as a test suite!

Running test suite: "/Users/deauth/Developer/solana/anchor/royalty_nft/Anchor.toml"

Error: websocket error
yarn run v1.22.22
$ jest


  console.log
    intializing contract

      at tests/index.spec.ts:21:13



 RUNS  tests/index.spec.ts
[2024-11-15T14:43:43.334098000Z INFO  solana_program_test] "royalty_nft" SBF program from ./target/deploy/royalty_nft.so, modified 50 minutes

  console.log
    payer is 71HjRo8RJ3dipCgniyV4M4hio66sFZZPJmv1rmnNEAez

      at tests/index.spec.ts:30:13



  console.log
    program is GaMVToeXNUeCjzduQegv8tURziz9mCrbgtzUF6LFEx29

      at tests/index.spec.ts:31:13


 RUNS  tests/index.spec.ts
[2024-11-15T14:43:43.427824000Z INFO  solana_program_test] "royalty_nft" SBF program from ./target/deploy/royalty_nft.so, modified 50 minutes

  console.log
    mintPda 5waNARDaizyDBaoFH8ZumecfHL7jpuWwFq3V942DsY72

      at tests/index.spec.ts:53:13



  console.log
    tokenAccountPda EtFFysBpckLRonWqRo7AyaRCX8A65ueH8w69VNSS3LA9

      at tests/index.spec.ts:54:13

 FAIL  tests/index.spec.ts (42.041 s)
  royalty_nft
    ✓ initiate contract (114 ms)
    ✕ create_nft (40004 ms)

  ● royalty_nft › create_nft

    thrown: "Exceeded timeout of 40000 ms for a test.
    Add a timeout value to this test to increase the timeout, if this is a long-running test. See https://jestjs.io/docs/api#testname-fn-timeout."

      27 |   });
      28 |
    > 29 |   test("create_nft", async () => {
         |       ^
      30 |     console.log("payer is", payer.publicKey.toBase58());
      31 |     console.log("program is", program.programId.toBase58());
      32 |

      at tests/index.spec.ts:29:7
      at Object.<anonymous> (tests/index.spec.ts:14:9)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 passed, 2 total
Snapshots:   0 total
Time:        42.103 s, estimated 43 s
Ran all test suites.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Is there a better way to know what exactly is taking so long? Can I tap into the program logs when executing with bankrun.

1 Answer 1

1

Im guessing whats happening here is that you have global

let context; and let provider: BankrunProvider;

And since jest tests can also run in parallel the provider gets killed during the tests or conflict. Does the same problem happen if you only run the second test?

You can either not have these global. Or initialize them only once or run the tests sequential:

jest --runInBand

Also in your anchor.toml just set anchor test to:

[scripts]
jest_test = "yarn test"
test = "yarn test" 

Only for the anchor "test" command anchor will actually inject the needed env variables to use anchor program in the test.

Here is a tutorial for bankrun and jest: https://solana.com/developers/guides/advanced/testing-with-jest-and-bankrun

And a video: https://www.youtube.com/watch?v=rut9l6nPZls&t=8s&ab_channel=SolPlay

3
  • still no luck, tried both approaches. Could you help and try for yourself in this repo. - I changed the command to jest --runInBand for now in package.json. Commented Nov 16 at 21:07
  • They run fine for me if i set test = "yarn test" i also updated the answer to include more info an resources. The errors are: "Missing signature for public key" in the first one and "Missing signature for public key(s)" in the second one
    – Jonas H.
    Commented Nov 16 at 21:24
  • 1
    Thanks a lot for the resources, I did what you asked of me, still no luck ### However I recently deleted my test-ledger folder in the folder root and the in the .anchor folder and since then my test-validator either refused to start or was just misbehaving ( do not remember the prompts anymore). And since I assumed that bankrun anyways creates its own environment, the validator failing shouldn't have been an issue. It worked for you so I cloned my own repo (doesn't have the ledger files) and it worked for me too. Mysterious. Any clues to what would have happened? Thanks a lot anyways Commented Nov 17 at 18:16

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.