1

I am running test for the default program which comes with anchor setup. However, I keep getting the above error.

the steps I followed were

  1. anchor init (project-name)
  2. start solana-test-validator
  3. anchor build -> anchor deploy
  4. update program ids on both anchor.toml and lib.rs
  5. anchor build -> anchor deploy then run the test

default lib.rs with hello world

use anchor_lang::prelude::*;

declare_id!("8455D3DCzghmDygw2STMttPo9sB6SBHUavNP7eHbqNiu");

#[program]
pub mod hello_world {
    use super::*;

    pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
        msg!("Hello, world!");
        Ok(())
    }
}

#[derive(Accounts)]
pub struct Initialize {}

test file

it("Is initialized!", async () => {
// Add your test here.
    const tx = await program.methods.initialize().rpc();
    console.log("Your transaction signature", tx);
  });

Error

   Is initialized!:
     TypeError: Cannot read properties of undefined (reading 'methods')
3
  • please provide the code and example
    – vicyyn
    Commented Aug 4, 2022 at 13:41
  • Hace you built the program using anchor build first? It may be because your IDL (and thus, types) aren't built
    – Harry.
    Commented Aug 4, 2022 at 16:28
  • 1
    Please provide more information to make it easier for others to provide an appropriate answer. As your question is currently written, it's difficult to give you a correct answer. A code snippet would be pretty helpful.
    – Ademola
    Commented Aug 4, 2022 at 20:32

1 Answer 1

1

It looks like your test can't find the Program types for your IDL. If the test file you have supplied in your question is indeed everything in the file, a full test file would likely look like:

import * as anchor from "@project-serum/anchor";
import { Program } from "@project-serum/anchor";
import { Hello } from "../target/types/hello";

describe("hello", () => {
  // Configure the client to use the local cluster.
  anchor.setProvider(anchor.AnchorProvider.env());

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

  it("Is initialized!", async () => {
    // Add your test here.
    const tx = await program.methods.initialize().rpc();
    console.log("Your transaction signature", tx);
  });
});

As you can see in the snippet I've posted, it imports the program types in this line: import { Hello } from "../target/types/hello";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.