Testing Instructions

What are Instructions?

Chapter 5: Testing Instructions

Testing ensures your instructions work as intended. Anchor’s testing framework lets you simulate transactions and verify behavior.

  • Key Points:

    • Run tests with anchor test.

    • Write tests in TypeScript/JavaScript.

    • Validate account states after execution.

  • Code Example:

import * as anchor from "@coral-xyz/anchor";

describe("my_program", () => {
  it("Initializes the account", async () => {
    const program = anchor.workspace.MyProgram;
    await program.methods.initialize().rpc();
    // Add assertions here
  });
});
  • Interactivity: Run the sample test and see pass/fail results in a simulated environment.

Last updated