From ced0586613b707b599bbfbaa8d7b4d2d84a0ed46 Mon Sep 17 00:00:00 2001 From: Aryan Bhosale <36108149+aryanbhosale@users.noreply.github.com> Date: Wed, 10 Jan 2024 23:16:14 +0530 Subject: [PATCH] Error handling in testing-with-anchor.js --- .../client/testing-with-anchor.js | 73 +++++++++++-------- 1 file changed, 44 insertions(+), 29 deletions(-) diff --git a/code/anchor/testing-with-anchor/client/testing-with-anchor.js b/code/anchor/testing-with-anchor/client/testing-with-anchor.js index e26d31edc..4f7c83609 100644 --- a/code/anchor/testing-with-anchor/client/testing-with-anchor.js +++ b/code/anchor/testing-with-anchor/client/testing-with-anchor.js @@ -3,41 +3,56 @@ const anchor = require("@project-serum/anchor"); const { SystemProgram } = anchor.web3; describe("testing-with-anchor", () => { - const provider = anchor.Provider.env(); - anchor.setProvider(provider); - const program = anchor.workspace.TestingWithAnchor; - const baseAccount = anchor.web3.Keypair.generate(); + let provider; + let program; + let baseAccount; + + before(async () => { + provider = anchor.Provider.env(); + anchor.setProvider(provider); + program = anchor.workspace.TestingWithAnchor; + baseAccount = anchor.web3.Keypair.generate(); + }); it("Is initialized!", async () => { - // call initialize! - let tx = await program.rpc.initialize(provider.wallet.publicKey, { - accounts: { - baseAccount: baseAccount.publicKey, - user: provider.wallet.publicKey, - systemProgram: SystemProgram.programId, - }, - signers: [baseAccount], - }); - console.log("Your transaction signature", tx); + try { + let tx = await program.rpc.initialize(provider.wallet.publicKey, { + accounts: { + baseAccount: baseAccount.publicKey, + user: provider.wallet.publicKey, + systemProgram: SystemProgram.programId, + }, + signers: [baseAccount], + }); + console.log("Your transaction signature", tx); + } catch (error) { + console.error("Error initializing:", error); + throw error; + } }); it("Is incrementing!", async () => { - let account = await program.account.baseAccount.fetch( - baseAccount.publicKey - ); - console.log("Count is", account.count.toString()); + try { + let account = await program.account.baseAccount.fetch( + baseAccount.publicKey + ); + console.log("Count is", account.count.toString()); - // Call increment! - await program.rpc.increment({ - accounts: { - baseAccount: baseAccount.publicKey, - authority: provider.wallet.publicKey, - }, - }); + // Call increment! + await program.rpc.increment({ + accounts: { + baseAccount: baseAccount.publicKey, + authority: provider.wallet.publicKey, + }, + }); - let accountAfterIncrement = await program.account.baseAccount.fetch( - baseAccount.publicKey - ); - console.log("Count is", accountAfterIncrement.count.toString()); + let accountAfterIncrement = await program.account.baseAccount.fetch( + baseAccount.publicKey + ); + console.log("Count is", accountAfterIncrement.count.toString()); + } catch (error) { + console.error("Error incrementing:", error); + throw error; + } }); });