'Class PublicKey is missing in schema: publicKey

Describe the bug Unable to use the change methods of the contract on jest with near test-environment. I get this error when trying to use the change methods

Greeting Contract Tests › should change the greeting from the contract using the set_greeting method

Class PublicKey is missing in schema: publicKey

  at serializeStruct (../../node_modules/borsh/lib/index.js:308:15)
  at serializeField (../../node_modules/borsh/lib/index.js:291:13)
  at ../../node_modules/borsh/lib/index.js:312:13
      at Array.map (<anonymous>)
  at serializeStruct (../../node_modules/borsh/lib/index.js:311:29)
  at Object.serialize (../../node_modules/borsh/lib/index.js:334:5)
  at signTransactionObject (node_modules/near-api-js/lib/transaction.js:219:29)
  at Object.signTransaction (node_modules/near-api-js/lib/transaction.js:237:16)
  at Account.signTransaction (node_modules/near-api-js/lib/account.js:99:16)
  at node_modules/near-api-js/lib/account.js:118:34

To Reproduce Steps to reproduce the behavior:

Clone https://github.com/hack-a-chain-software/near.monorepo Paste this into the greeting.spec.ts:

import "jest";
import { Account, connect, Contract } from "near-api-js";
import { KeyStore } from "near-api-js/lib/key_stores";
import { NearConfig } from "near-api-js/lib/near";

/**
 * @name GreetingContract
 * @description - This will handle the interactions
 * with the contract with better TS types
 */
export class GreetingContract {
  private contract: any;

  /**
   * @constructor
   * @param contract
   */
  constructor(contract: any) {
    this.contract = contract as any;
  }

  /**
   * @description - This method gets the message on chain to the user account_id
   */
  public async getGreeting(params: { account_id: string }): Promise<string> {
    return await this.contract.get_greeting(params);
  }

  /**
   * @description - This method updates the message for the account on NEAR
   */
  public async updateGreeting(params: { message: string }) {
    return await this.contract.set_greeting(params);
  }
}

describe("Greeting Contract Tests", () => {
  let contract: GreetingContract;
  let account: Account;
  let config: NearConfig & {
    contractName: string;
    accountId: string;
    deps: { keyStore: KeyStore };
    testAccountId: string;
  };

  /** @description - Runs Before Everything and initializes the near instances */
  beforeAll(async () => {
    // @ts-ignore
    config = nearConfig;

    const near = await connect(config);

    account = await near.account(config.accountId as string);

    contract = await new GreetingContract(
      new Contract(account, config.contractName, {
        viewMethods: ["get_greeting"],
        changeMethods: ["set_greeting"],
      })
    );
  });

  /** @description - Gets the current greeting from the smart contract and checks if it goes okay */
  it("should get the greeting from the contract using the `get_greeting` method", async () => {
    // Gets the current message for that account id on the contract
    const message = await contract.getGreeting({
      account_id: account.accountId,
    });

    console.log(message);

    expect(message).toEqual("Hello");
  });

  it("should change the greeting from the contract using the `set_greeting` method", async () => {
    // Gets the current message for that account id on the contract
    await contract.updateGreeting({
      message: "Whats Up Darling!",
    });

    const message = await contract.getGreeting({
      account_id: account.accountId,
    });

    expect(message).toEqual("Whats Up Darling!");
  });
});

Expected behavior Pass both of the tests using the near config from the near-test-environment

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

OS: Mac OS Monterey Version: Monterey Additional context If someone knows a way to bypass this will be cool also 😄



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source