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);
});
});
.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 withanchor build
after you cleaned.rpc({skipPreflight: true})
anchor test --skip-local-validator Finished release [optimized] target(s) in 0.91s Finishedtest
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