test.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import { describe, test } from 'node:test';
  2. import { PublicKey, Transaction, TransactionInstruction } from '@solana/web3.js';
  3. import { assert } from 'chai';
  4. import { start } from 'solana-bankrun';
  5. describe('hello-solana', async () => {
  6. // load program in solana-bankrun
  7. const PROGRAM_ID = PublicKey.unique();
  8. const context = await start([{ name: 'hello_solana_program', programId: PROGRAM_ID }], []);
  9. const client = context.banksClient;
  10. const payer = context.payer;
  11. test('Say hello!', async () => {
  12. const blockhash = context.lastBlockhash;
  13. // We set up our instruction first.
  14. const ix = new TransactionInstruction({
  15. keys: [{ pubkey: payer.publicKey, isSigner: true, isWritable: true }],
  16. programId: PROGRAM_ID,
  17. data: Buffer.alloc(0), // No data
  18. });
  19. const tx = new Transaction();
  20. tx.recentBlockhash = blockhash;
  21. tx.add(ix).sign(payer);
  22. // Now we process the transaction
  23. const transaction = await client.processTransaction(tx);
  24. assert(transaction.logMessages[0].startsWith(`Program ${PROGRAM_ID}`));
  25. assert(transaction.logMessages[1] === 'Program log: Hello, Solana!');
  26. assert(transaction.logMessages[2] === `Program log: Our program's Program ID: ${PROGRAM_ID}`);
  27. assert(transaction.logMessages[3].startsWith(`Program ${PROGRAM_ID} consumed`));
  28. assert(transaction.logMessages[4] === `Program ${PROGRAM_ID} success`);
  29. assert(transaction.logMessages.length === 5);
  30. });
  31. });