1

I just finished following along project 3 where we include actions and blinks to the peanut butter voting project. At the very end, we had to anchor clean and anchor test to have it clear its cache and rebuild the program. We also included the following lines of code to to set provider and get the program

 anchor.setProvider(anchor.AnchorProvider.env());
  let votingProgram = anchor.workspace.Voting as Program<Voting>;

And finally had to replace the pubkey with the pub address of the deployed program.

However, now I fail every test and get no explanation as to why. I have no clue why it's happening:

(node:33455) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
 FAIL  tests/votingdapp.spec.ts (15.302 s)
  Voting
    ✕ initialize Poll (91 ms)
    ✕ initialize candidate (19 ms)
    ✕ vote (14 ms)

  ● Voting › initialize Poll





  ● Voting › initialize candidate





  ● Voting › vote





Test Suites: 1 failed, 1 total
Tests:       3 failed, 3 total
Snapshots:   0 total
Time:        15.364 s

Here's the full code in the file, please let me know what other info might be needed to solve the issue:

import * as anchor from '@coral-xyz/anchor';
import {Program} from '@coral-xyz/anchor';
import {Keypair, PublicKey} from '@solana/web3.js';
import { startAnchor } from "solana-bankrun";
import { BankrunProvider } from 'anchor-bankrun';
import { Voting } from 'anchor/target/types/voting';

const IDL = require("../target/idl/voting.json");
const votingAddress = new PublicKey("AHobZPpSamDDDkA792LiLvwMQmNE7BZNMcYHV2bvAXUR");

describe('Voting', () => {
  let context;
  let provider;
  anchor.setProvider(anchor.AnchorProvider.env());
  let votingProgram = anchor.workspace.Voting as Program<Voting>;

  beforeAll(async () => {
    // context = await startAnchor("", [{ name: "voting", programId: votingAddress }], []);
      // provider = new BankrunProvider(context);

    // votingProgram = new Program<Voting>(
    //   IDL,
    //   provider,
    // );
  });

  it("initialize Poll", async () => {
    await votingProgram.methods.initializePoll(
      new anchor.BN(1),
      "what is your favorite type of peanut butter",
      new anchor.BN(0),
      new anchor.BN(1829039043),
    ).rpc();

    const [pollAddress, _] = PublicKey.findProgramAddressSync(
      [new anchor.BN(1).toArrayLike(Buffer, "le", 8)],
      votingAddress,
    );

    const poll = await votingProgram.account.poll.fetch(pollAddress);
    console.log(poll);

    expect(poll.pollId.toNumber()).toBe(1);
    expect(poll.description).toBe("what is your favorite type of peanut butter");
  });

  it("initialize candidate", async () => {
    await votingProgram.methods.initializeCandidate(
      "Smooth",
      new anchor.BN(1),
    ).rpc();

    await votingProgram.methods.initializeCandidate(
      "Crunchy",
      new anchor.BN(1),
    ).rpc();

    // fetches the address
    const [smoothAddress] = PublicKey.findProgramAddressSync(
      [new anchor.BN(1).toArrayLike(Buffer, "le", 8), Buffer.from("Smooth")],
      votingAddress,
    );

    const smoothCandidate = await votingProgram.account.candidate.fetch(smoothAddress);

    const [crunchAddress] = PublicKey.findProgramAddressSync(
      [new anchor.BN(1).toArrayLike(Buffer, "le", 8), Buffer.from("Crunchy")],
      votingAddress,
    );

    const crunchyCandidate = await votingProgram.account.candidate.fetch(crunchAddress);

    console.log(smoothCandidate, crunchyCandidate)

    expect(smoothCandidate.voteCount.toNumber()).toEqual(0);
    

  });

  it("vote", async () => {
    await votingProgram.methods.vote(
      "Smooth",
      new anchor.BN(1),
    ).rpc();        

    await votingProgram.methods.vote(
      "Smooth",
      new anchor.BN(1),
    ).rpc();   

    const [smoothAddress] = PublicKey.findProgramAddressSync(
      [new anchor.BN(1).toArrayLike(Buffer, "le", 8), Buffer.from("Smooth")],
      votingAddress,
    );

    const smoothCandidate = await votingProgram.account.candidate.fetch(smoothAddress);
    console.log(smoothCandidate);
  
  });

});
5
  • on the .rpc(), you can put .rpc({skipPreflight: true}) and log out the signature to check on explorer.solana.com. I just ran your tests locally and was able to see the expected logs and tests passed. It's likely something with anchor build after you cleaned
    – Jacob Creech
    Commented Oct 16 at 22:10
  • Do you find any Solution? Because I am stuck on the same problem. Just For Reference: youtube.com/… at 2:29:50 / 11:08:04 My Terminal Logs are after adding .rpc({skipPreflight: true}) anchor test --skip-local-validator Finished release [optimized] target(s) in 0.91s Finished test profile [unoptimized + debuginfo] target(s) in 0.23s Running unittests src/lib.rs (/home/harsh/Documents/sol-temp/voting-dapp/anchor/target/debug/deps/voting-bd3969dd6e10ac46) Deploying cluster: 127.0.0.1:8899 Upgrade authority: /home/harsh/.config/sol
    – Harsh Jha
    Commented Oct 17 at 20:18
  • I added the skip preflight and got some stuff logged but it was only ``` at Function.parse (node_modules/@coral-xyz/anchor/src/error.ts:250:14) at translateError (node_modules/@coral-xyz/anchor/src/error.ts:282:37) at MethodsBuilder.rpc [as _rpcFn] (node_modules/@coral-xyz/anchor/src/program/namespace/rpc.ts:35:29)" ``` I have no idea what's wrong. I think my lib.rs file is identical to yours in the video. Could it be my route file? @JacobCreech Commented Oct 19 at 19:40
  • I also tried running the tests individually and what i get is an error related to the use of "as Program<Voting>" saying its failing to run due to a missing semicolon. However, it still prints "votingProgram" when i clog it after initializing it so I am so lost @JacobCreech Commented Oct 19 at 20:05
  • After much digging I see that it has to be an issue with Jest, and the way it was configured by anchor bankrun. I've tried configuring it to work with TS and specifically ES6 syntax but still have no luck. Can't make a functioning blink demo without having this fixed, so I'd love any help with this if possible @JacobCreech Commented Oct 19 at 20:44

0

Your Answer

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