Skip to content
Development
Command

/test-ts

Run TypeScript tests for Solana frontends and Anchor programs

From plugin
solana-ai-kit
10130 skills15 agents30 commands7 MCP
Install
> /plugin marketplace add solanabr/solana-ai-kit
> /plugin install solana-ai-kit@stbr

How it fires

How this command gets triggered: by you, by Claude, or both.

  • Fires itselfClaude auto-loads it when your prompt matches the work.
  • You can call itInvoke it directly when you want it.
  • Slash command/test-ts

Context preview

What this command does when you run it.

Run TypeScript tests for Solana frontends and Anchor programs

Command definition

test-ts.md
description: "Run TypeScript tests for Solana frontends and Anchor programs"

You are running TypeScript tests. This command covers Anchor program tests, frontend component tests, and integration tests.

Related Skills

  • [testing.md](../skills/ext/solana-dev/skill/references/testing.md) - Testing strategy details
  • [frontend-framework-kit.md](../skills/ext/solana-dev/skill/references/frontend-framework-kit.md) - React/Next.js patterns
  • [programs/anchor.md](../skills/ext/solana-dev/skill/references/programs/anchor.md) - Anchor test patterns

Step 1: Identify Test Type

echo "๐Ÿ” Detecting TypeScript test configuration..."

# Check for Anchor tests
if [ -f "Anchor.toml" ] && [ -d "tests" ]; then
    echo "โš“ Anchor TypeScript tests detected"
fi

# Check for Vitest
if grep -q "vitest" package.json 2>/dev/null; then
    echo "โšก Vitest configured"
fi

# Check for Jest
if grep -q "jest" package.json 2>/dev/null; then
    echo "๐Ÿƒ Jest configured"
fi

# Check for Playwright
if grep -q "playwright" package.json 2>/dev/null; then
    echo "๐ŸŽญ Playwright E2E tests configured"
fi

---

Anchor Program Tests

Run Anchor Tests

echo "โš“ Running Anchor TypeScript tests..."

# Build first
anchor build

# Run all tests
anchor test

# Skip rebuild (faster iteration)
anchor test --skip-build

# Run specific test file
anchor test tests/vault.ts

# Run with logs
RUST_LOG=debug anchor test

Anchor Test Pattern

import * as anchor from "@coral-xyz/anchor";
import { Program } from "@coral-xyz/anchor";
import { MyProgram } from "../target/types/my_program";
import { expect } from "chai";

describe("my_program", () => {
  const provider = anchor.AnchorProvider.env();
  anchor.setProvider(provider);

  const program = anchor.workspace.MyProgram as Program<MyProgram>;

  it("initializes vault", async () => {
    const [vaultPda] = anchor.web3.PublicKey.findProgramAddressSync(
      [Buffer.from("vault"), provider.wallet.publicKey.toBuffer()],
      program.programId
    );

    await program.methods
      .initialize()
      .accounts({
        vault: vaultPda,
        authority: provider.wallet.publicKey,
        systemProgram: anchor.web3.SystemProgram.programId,
      })
      .rpc();

    const vault = await program.account.vault.fetch(vaultPda);
    expect(vault.authority.toString()).to.equal(
      provider.wallet.publicKey.toString()
    );
  });

  it("deposits funds", async () => {
    const [vaultPda] = anchor.web3.PublicKey.findProgramAddressSync(
      [Buffer.from("vault"), provider.wallet.publicKey.toBuffer()],
      program.programId
    );

    const depositAmount = new anchor.BN(1_000_000_000); // 1 SOL

    await program.methods
      .deposit(depositAmount)
      .accounts({
        vault: vaultPda,
        authority: provider.wallet.publicKey,
      })
      .rpc();

    const vault = await program.account.vault.fetch(vaultPda);
    expect(vault.balance.toNumber()).to.equal(depositAmount.toNumber());
  });

  it("fails with insufficient funds", async () => {
    const [vaultPda] = anchor.web3.PublicKey.findProgramAddressSync(
      [Buffer.from("vault"), provider.wallet.publicKey.toBuffer()],
      program.programId
    );

    try {
      await program.methods
        .withdraw(new anchor.BN(999_000_000_000)) // More than balance
        .accounts({
          vault: vaultPda,
          authority: provider.wallet.publicKey,
        })
        .rpc();
      expect.fail("Should have thrown");
    } catch (err) {
      expect(err.message).to.include("InsufficientFunds");
    }
  });
});

LiteSVM TypeScript Tests

For faster tests without validator:

npm install --save-dev litesvm
import { LiteSVM } from 'litesvm';
import { PublicKey, Transaction, Keypair } from '@solana/web3.js';

describe("litesvm tests", () => {
  let svm: LiteSVM;
  const programId = new PublicKey("YourProgramId...");

  beforeAll(() => {
    svm = new LiteSVM();
    svm.addProgramFromFile(programId, "target/deploy/program.so");
  });

  it("processes instruction", () => {
    const payer = Keypair.generate();
    svm.airdrop(payer.publicKey, 1_000_000_000);

    const tx = new Transaction();
    tx.recentBlockhash = svm.latestBlockhash();
    tx.add(/* your instruction */);
    tx.sign(payer);

    const result = svm.sendTransaction(tx);
    expect(result.err).toBeNull();
  });
});

---

Frontend Component Tests

Vitest Setup

echo "โšก Running Vitest tests..."
npm run test
# or
npx vitest run

React Component Test

// components/__tests__/WalletButton.test.tsx
import { render, screen, fireEvent } from '@testing-library/react';
import { describe, it, expect, vi } from 'vitest';
import { WalletButton } from '../WalletButton';

// Mock wallet hooks
vi.mock('@solana/wallet-adapter-react', () => ({
  useWallet: () => ({
    connected: false,
    connect: vi.fn(),
    disconnect: vi.fn(),
    publicKey: null,
  }),
}));

describe('WalletButton', () => {
  it('renders connect button when not connected', () => {
    render(<WalletButton />);
    expect(screen.getByText('Connect Wallet')).toBeInTheDocument();
  });

  it('calls connect on click', async () => {
    const { useWallet } = await import('@solana/wallet-adapter-react');
    const mockConnect = vi.fn();
    vi.mocked(useWallet).mockReturnValue({
      connected: false,
      connect: mockConnect,
      disconnect: vi.fn(),
      publicKey: null,
    });

    render(<WalletButton />);
    fireEvent.click(screen.getByText('Connect Wallet'));
    expect(mockConnect).toHaveBeenCalled();
  });
});

Hook Testing

// hooks/__tests__/useBalance.test.tsx
import { renderHook, waitFor } from '@testing-library/react';
import { describe, it, expect, vi } from 'vitest';
import { useBalance } from '../useBalance';

describe('useBalance', () => {
  it('fetches balance for address', async () => {
    const mockAddress = 'So11111111111111
Read more
Ships withsolana-ai-kit

Production-ready Claude Code configuration for full-stack Solana development. Combines best practices from multiple sources into an agent-optimized, token-efficient config you can install and adapt to your specific project.

Get the whole plugin

Other commands on solana-ai-kit.