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:(
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');
});
});
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', {
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.