1

Hello i am trying to test my first file in anchor and it keeps giving this error, error i got:

TypeError: Cannot read properties of undefined (reading 'local')
    at Suite.<anonymous> (/home/benny/mycalcdapp/tests/mycalcdapp.ts:6:36)
    at Object.create (/home/benny/mycalcdapp/node_modules/mocha/lib/interfaces/common.js:148:19)
    at context.describe.context.context (/home/benny/mycalcdapp/node_modules/mocha/lib/interfaces/bdd.js:42:27)
    at Object.<anonymous> (/home/benny/mycalcdapp/tests/mycalcdapp.ts:5:1)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Module.m._compile (/home/benny/mycalcdapp/node_modules/ts-node/src/index.ts:439:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)

.ts file for test:

const assert = require('assert');
const anchor = require('@project-serum/anchor');
const { SystemProgram } = anchor.web3;

describe('mycalcdapp', () => {
  const provider = anchor.Provider.local();
  anchor.setProvider(provider);
  const calculator = anchor.web3.Keypair.generate();
  const program = anchor.workspace.mycalcdapp;

  it('creates a calculator', async () => {
    await program.rpc.create('Welocme to solana', {
      accounts: {
        calculator: calculator.publicKey,
        user: provider.wallet.publicKey,
        system_program: SystemProgram.programId,
      },
      signers: [calculator],
    });
    const account = await program.account.calculator.fetch(
      calculator.publicKey
    );
    assert.ok(account.greeting === 'Welcome to solana');
  });
});

i have seen another similar stackoverflow postv about not being able to read rpc but it was not answered either :(

2 Answers 2

3

so I had to import AnchorProvider and web3 from @project-serum/anchor apart from importing * as anchor. then we set the provider as const provider = AnchorProvider.local() also change const {SystemProgram } = anchor.web3 to const {SystemProgram } = web3 and change system_program inside accounts > create rpc to systemProgram and voila our test works!! full test code:

import assert from 'assert';
import * as anchor from '@project-serum/anchor';
import { AnchorProvider, web3 } from '@project-serum/anchor';
const { SystemProgram } = web3;

describe('mycalcdapp', () => {
  const provider = AnchorProvider.local();
  anchor.setProvider(provider);
  const calculator = anchor.web3.Keypair.generate();
  const program = anchor.workspace.Mycalcdapp;

  it('creates a calculator', async () => {
    await program.rpc.create('Welcome to solana', {
      accounts: {
        calculator: calculator.publicKey,
        user: provider.wallet.publicKey,
        systemProgram: SystemProgram.programId,
      },
      signers: [calculator],
    });
    const account = await program.account.calculator.fetch(
      calculator.publicKey
    );
    assert.ok(account.greeting === 'Welcome to solana');
  });
});
Sign up to request clarification or add additional context in comments.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
0

I think the main issue in your code was the name of your dapp.

 const program = anchor.workspace.mycalcdapp;

your workspace name should be initialized with capital letter

 const program = anchor.workspace.Mycalcdapp;

also inside "it" block welcome is misspelled

await program.rpc.create('Welocme to solana', {

1 Comment

Thank you, i was confused to this day about this 💖

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.