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.