Search code examples
typescriptsolanaanchor-solana

Failing tests: cannot read properties of undefined(reading local) solana devnet


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 :(


Solution

  • 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', {