bankrun.test.ts 1.7 KB

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