11import assert from 'node:assert' ;
2- import { describe , it } from ' node:test' ;
2+ import { before , describe , it } from " node:test"
33import * as anchor from '@coral-xyz/anchor' ;
44import {
55 Keypair ,
@@ -26,42 +26,58 @@ async function createAndProcessTransaction(
2626 additionalSigners : Keypair [ ] = [ ] ,
2727) : Promise < BanksTransactionResultWithMeta > {
2828 const tx = new Transaction ( ) ;
29- // Get the latest blockhash and minimum rent-exempt balance
29+ // Get the latest blockhash
3030 const [ latestBlockhash ] = await client . getLatestBlockhash ( ) ;
3131 tx . recentBlockhash = latestBlockhash ;
3232 // Add transaction instructions
3333 tx . add ( instruction ) ;
3434 tx . feePayer = payer . publicKey ;
3535 //Add signers
3636 tx . sign ( payer , ...additionalSigners ) ;
37+ // Process transaction
3738 const result = await client . tryProcessTransaction ( tx ) ;
3839 return result ;
3940}
4041
4142describe ( 'Close an account' , async ( ) => {
4243 // Configure the client to use the local cluster.
43- const context = await startAnchor ( '' , [ { name : 'close_account_program' , programId : PROGRAM_ID } ] , [ ] ) ;
44+ const context = await startAnchor (
45+ "" ,
46+ [ { name : "close_account_program" , programId : PROGRAM_ID } ] ,
47+ [ ]
48+ ) ;
4449 const provider = new BankrunProvider ( context ) ;
4550
4651 const payer = provider . wallet as anchor . Wallet ;
4752 const program = new anchor . Program < CloseAccount > ( IDL , provider ) ;
48- // Derive the PDA for the user's account.
53+
4954 const user = Keypair . generate ( ) ; // Generate a new user keypair
5055
51- //Transfer SOL to the user account to cover rent
52- const transferInstruction = SystemProgram . transfer ( {
53- fromPubkey : payer . publicKey ,
54- toPubkey : user . publicKey ,
55- lamports : 2 * LAMPORTS_PER_SOL ,
56- } ) ;
56+ before ( async ( ) => {
57+ //Transfer SOL to the user account to cover rent
58+ const transferInstruction = SystemProgram . transfer ( {
59+ fromPubkey : payer . publicKey ,
60+ toPubkey : user . publicKey ,
61+ lamports : 2 * LAMPORTS_PER_SOL ,
62+ } ) ;
5763
58- await createAndProcessTransaction ( context . banksClient , payer . payer , transferInstruction , [ payer . payer ] ) ;
64+ await createAndProcessTransaction (
65+ context . banksClient ,
66+ payer . payer ,
67+ transferInstruction ,
68+ [ payer . payer ]
69+ ) ;
70+ } ) ;
5971
60- const [ userAccount , userAccountBump ] = PublicKey . findProgramAddressSync ( [ Buffer . from ( 'USER' ) , user . publicKey . toBuffer ( ) ] , program . programId ) ;
72+ // Derive the PDA for the user's account.
73+ const [ userAccount , userAccountBump ] = PublicKey . findProgramAddressSync (
74+ [ Buffer . from ( "USER" ) , user . publicKey . toBuffer ( ) ] ,
75+ program . programId
76+ ) ;
6177
62- it ( ' Can create an account' , async ( ) => {
78+ it ( " Can create an account" , async ( ) => {
6379 await program . methods
64- . createUser ( ' Jacob' )
80+ . createUser ( " Jacob" )
6581 . accounts ( {
6682 user : user . publicKey ,
6783 } )
@@ -70,12 +86,12 @@ describe('Close an account', async () => {
7086
7187 // Fetch the account data
7288 const userAccountData = await program . account . userState . fetch ( userAccount ) ;
73- assert . equal ( userAccountData . name , ' Jacob' ) ;
89+ assert . equal ( userAccountData . name , " Jacob" ) ;
7490 assert . equal ( userAccountData . user . toBase58 ( ) , user . publicKey . toBase58 ( ) ) ;
7591 assert . notEqual ( userAccountData , null ) ;
7692 } ) ;
7793
78- it ( ' Can close an Account' , async ( ) => {
94+ it ( " Can close an Account" , async ( ) => {
7995 await program . methods
8096 . closeUser ( )
8197 . accounts ( {
@@ -86,7 +102,9 @@ describe('Close an account', async () => {
86102
87103 // The account should no longer exist, returning null.
88104 try {
89- const userAccountData = await program . account . userState . fetchNullable ( userAccount ) ;
105+ const userAccountData = await program . account . userState . fetchNullable (
106+ userAccount
107+ ) ;
90108 assert . equal ( userAccountData , null ) ;
91109 } catch ( err ) {
92110 // Won't return null and will throw an error in anchor-bankrun'
0 commit comments