I'm using chai to test my contracts. I have a test that calls a function that does not exist in that contract. This is the contract:

// SPDX-License-Identifier: MIT

pragma solidity 0.8.13;
import "@openzeppelin/contracts-upgradeable/token/ERC20/ERC20Upgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC20/extensions/ERC20BurnableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "@openzeppelin/contracts-upgradeable/utils/math/SafeMathUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/utils/cryptography/ECDSAUpgradeable.sol";

contract Token is
    //This function gets called by the TokenFactory contract
    function initialize(string memory _name, string memory _symbol)
        __ERC20_init(_name, _symbol);

This is the test.js

it("assure the version() function is not available in first contract", async function() {
        const addressOfFirstToken = await tokenFactory.getTokenAddress(0);
        const addressOfSecondToken = await tokenFactory.getTokenAddress(1);
        const firstToken = await Token.attach(addressOfFirstToken);
        const secondToken = await Token.attach(addressOfSecondToken);
        expect(await firstToken.version()).to.throw(TypeError);

This is the message it pops up when I run

npx hardhat test
  1) Factory token is upgradeable
       assure the version() function is not available in first contract:
     TypeError: firstToken.version is not a function
      at Context.<anonymous> (test/createFactoryUpgradeable.js:68:33)

1 Answer 1


I had the same problem. According to https://stackoverflow.com/a/20220016, "You need to pass a function to chai, but your code is passing the result of calling the function instead."

This worked for me:

expect(await firstToken.version()).to.throw("firstToken.version is not a function");

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.