I cannot replicate the issue. I was able to pass null
when I did not have an argument to pass. Happy to take a look at your full code if you have a GH.
Here's what I tested (and anchor test
ran fine):
use anchor_lang::prelude::*;
declare_id!("6itiTNjzVZWxSgrjaGwMMjakDvrmnkwPG5YsbiU2jSkX");
#[program]
mod enum_test {
use super::*;
pub fn dummy(_ctx: Context<Dummy>, _data: Option<DummyState>) -> Result<()> {
Ok(())
}
}
#[derive(Accounts)]
pub struct Dummy<'info> {
#[account(mut)]
pub signer: Signer<'info>,
pub system_program: Program<'info, System>,
}
#[derive(AnchorSerialize, AnchorDeserialize, Copy, Clone, PartialEq, Eq)]
pub enum DummyState {
One,
Two,
}
And my test:
import * as anchor from "@coral-xyz/anchor";
import { Program } from "@coral-xyz/anchor";
import { EnumTest } from "../target/types/enum_test";
import { expect } from "chai";
describe("enum-test", () => {
// Configure the client to use the local cluster.
anchor.setProvider(anchor.AnchorProvider.env());
const program = anchor.workspace.EnumTest as Program<EnumTest>;
// Reusable function to test dummy calls
async function testDummyCall(dummyArgs: any) {
try {
const tx = await program.methods.dummy(dummyArgs).rpc();
expect(tx).to.be.ok;
} catch (error) {
expect(error).to.not.be.ok;
}
}
it("Is dummy one!", async () => {
await testDummyCall({ one: {} });
});
it("Is dummy two!", async () => {
await testDummyCall({ two: {} });
});
it("Is not a dummy!", async () => {
await testDummyCall(null);
});
it("Fails on dummy three!", async () => {
try {
await program.methods.dummy({ three: {} }).rpc();
} catch (error) {
expect(error).to.be.ok;
}
});
});