bankrun.test.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { describe, it } from "node:test";
  2. import * as anchor from "@coral-xyz/anchor";
  3. import { Keypair, PublicKey } from "@solana/web3.js";
  4. import { BankrunProvider } from "anchor-bankrun";
  5. import { assert } from "chai";
  6. import { startAnchor } from "solana-bankrun";
  7. import type { CounterAnchor } from "../target/types/counter_anchor";
  8. import IDL from "../target/idl/counter_anchor.json" with { type: "json" };
  9. const PROGRAM_ID = new PublicKey(IDL.address);
  10. describe("counter_anchor", async () => {
  11. // Configure the client to use the anchor-bankrun
  12. const context = await startAnchor(
  13. "",
  14. [{ name: "counter_anchor", programId: PROGRAM_ID }],
  15. []
  16. );
  17. const provider = new BankrunProvider(context);
  18. const payer = provider.wallet as anchor.Wallet;
  19. const program = new anchor.Program<CounterAnchor>(IDL, provider);
  20. // Generate a new keypair for the counter account
  21. const counterKeypair = new Keypair();
  22. it("Initialize Counter", async () => {
  23. await program.methods
  24. .initializeCounter()
  25. .accounts({
  26. counter: counterKeypair.publicKey,
  27. payer: payer.publicKey,
  28. })
  29. .signers([counterKeypair])
  30. .rpc();
  31. const currentCount = await program.account.counter.fetch(
  32. counterKeypair.publicKey
  33. );
  34. assert(
  35. currentCount.count.toNumber() === 0,
  36. "Expected initialized count to be 0"
  37. );
  38. });
  39. it("Increment Counter", async () => {
  40. await program.methods
  41. .increment()
  42. .accounts({ counter: counterKeypair.publicKey })
  43. .rpc();
  44. const currentCount = await program.account.counter.fetch(
  45. counterKeypair.publicKey
  46. );
  47. assert(currentCount.count.toNumber() === 1, "Expected count to be 1");
  48. });
  49. });