Questions tagged [chai]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0 votes
1 answer
27 views

Testing safeTransferFrom with onlyAllowedOperator using chai/hardhat

I am writing tests for an NFT using safeTransferFrom with onlyAllowedOperator and the tests are failing with and without onlyAllowedOperator. Here's the function: function safeTransferFrom(address ...
  • 103
0 votes
0 answers
21 views

Im trying to run a js script on the blockchain using hardhat ,it throws an error whenever I run it on the command line

This is the following error I get when running the command "npx hardhat --network localhost scripts/1_deploy.js" HeadersTimeoutError: Headers Timeout Error at Timeout.onParserTimeout [...
0 votes
1 answer
22 views

check balance before and after transaction with chai

I'm writing the test cases for a contract, and I have a problem trying to match the balance, because of the transaction gas. what is the correct way to do it? it("should allow the owner of the ...
  • 105
0 votes
2 answers
31 views

Error: Transaction reverted: function selector was not recognized and there's no fallback nor receive function

I'm getting a console error in hardhat test case this is my solidity code /** * donateToCampaign - Allows a user to donate to a specific campaign. * * @param _id the ID of the campaign to donate to....
  • 105
1 vote
2 answers
22 views

conflict peer dependencies: nomicfoundation & hardhat-deploy-ethers

I'm facing a blockage so any help will be appreciated: @nomiclabs/hardhat-ethers is an essential peer dependency to hardhat-deploy-ethers but that will prevent me from installing @nomicfoundation/chai-...
  • 21
0 votes
2 answers
127 views

Transaction reverted: trying to deploy a contract whose code is too large

My smart contract is over 300 lines of code, complete with events, modifiers, internal functions, getters and setters. I'm trying to run tests using hardhat and chai. All I'm getting is this error: ...
0 votes
1 answer
19 views

Unittest for excisting NFT

I have build a contract (not yet deployed) that allows to check if the msg.sender holds a certain NFT: pragma solidity ^0.8.17; import "@openzeppelin/contracts/interfaces/IERC721.sol"; ...
0 votes
0 answers
11 views

How can i bind log() to my runtime in my unit test file?

How can I make log() bind to my runtime in my chai unit-testing script? I have a hardhat solidity project. Included snippets and pseudocode. This framework is very new to me so if there are obvious ...
  • 121
1 vote
1 answer
27 views

AssertionError: expected Promise{…} to equal 1

Code: describe("uploading post", async () => { let result, postCounter const hash = "whatever" before(async () => { result = await ...
0 votes
2 answers
165 views

Error: VM Exception while processing transaction: reverted with reason string 'Didn't send enough!' on testing contract

Getting this error while testing one of functions functionality in the contract AssertionError: Expected transaction to be reverted with You need to spend more ETH!!, but other exception was thrown: ...
  • 13
1 vote
1 answer
23 views

Principles of Testing Smart Contracts

Generally, when writing tests for smart contracts, how comprehensive do the tests have to be? Also, how does one know what to test (e.g., which line in the function in the original contract file the ...
1 vote
1 answer
29 views

How to get the gas costs of a reversed transaction when testing (with Chai / ethers.js / Hardhat)?

Does anyone know how to get the gas costs of a reversed transaction when testing with Hardhat / Chai? I found out I can get the receipt object of a transaction, which holds the required data to ...
0 votes
0 answers
19 views

AssertionError: expected 0 to equal " ". The numerical values of the inputs were compared, and they differed

I'm pretty new to coding and solidiy - I'm testing a covered-call option smart contract in hardhat - specifically a function to check if the balance of the smart contract has been updated following ...
  • 11
1 vote
1 answer
55 views

Hardhat and Chai testing : How should I write the test?

I've digged through all the related question, but none of these worked for me. I used this repo, and replaced the contract and test file : https://github.com/NomicFoundation/hardhat-boilerplate EDIT ...
1 vote
1 answer
81 views

TypeError: <FUNCTION_NAME>.owner / .connect() is not a function on hardhat unit testing

Plugin and tools used: hardhat-chai-matcher, chai, mocha Issue: Was wonder if its my environment setup wrong, so i ran other test but theyre passing just alright: This is my test script written ...
  • 13
0 votes
2 answers
176 views

Hardhat chai .revertWith() not catching reverted error from Smart Contract

I am using Hardhat and chai for my testing and attempting to trigger an error that gets reverted to make sure my contract is acting properly. Note: I'm running on a forked hardhat chain of mainnet. I ...
0 votes
1 answer
95 views

Getting error while testing transfer function using chai

I have a function that transfers the amount from owner to user. I'm getting success in the event emitted message and my test is failing every time. Here is my code. function investInMPay(uint256 ...
  • 55
0 votes
1 answer
22 views

Event test failing because of address's casing

I am trying to test if the event is emitted with the correct arguments however my tests are failing because of some casing issue. What is the best away to overcome this issue?
  • 76
0 votes
0 answers
39 views

Hardhat tests failing when testing require statement

Hi I am trying to test a smart contract I wrote using hardhat, with chai and @nomicfoundation/hardhat-chai-matchers. I have followed the documentation however I am having no luck. Please see below the ...
  • 21
1 vote
1 answer
124 views

Writing Solidity tests for array length & contents (chai)

tdlr; solution I had to await both of the values used in my assertions. For grabbing the length of the array, I needed to add a new method to my contract, as shown here. The reason for this is in the ...
0 votes
0 answers
91 views

to.be.reverted function showing invalid chai property

it("money",async()=>{ console.log(await instance.hasRole("0xc5d48d6799b0d7d7cdfebe6afd6fac6dff6746359d2e02d434f8172e9a93fe97",accounts[8])); console.log(await ...
2 votes
1 answer
32 views

How do you know when an operation should be performed synchronously or asynchronously in smart contract tests?

What operations are usually perform asynchronously in smart contract tests, and what operations are usually performed synchronously in smart contract tests? When are each of these usually used?
2 votes
1 answer
50 views

How do mocha and chai work?

I got this snippet from: https://hardhat.org/tutorial/testing-contracts In this test, what does const { expect } = require("chai") mean? Also, I thought describe() is from the Mocha ...
1 vote
0 answers
10 views

How does describe() in mocha work here?

I got this snippet from: https://hardhat.org/tutorial/testing-contracts In this test, what does const { expect } = require("chai") mean? Also, I thought describe() is from the Mocha ...
3 votes
1 answer
307 views

Why are there so many async and await in smart contract tests?

What’s the use of async and await and why are there so many of them in smart contact tests?
2 votes
1 answer
143 views

What is a good way to learn Solidity? [duplicate]

I’ve been watching YouTube videos and learning from Udemy on solidity for a year. I have basic understanding of blockchain. My problem is that although the instructors show the what to do and how to ...
0 votes
1 answer
149 views

Hardhat Testing: Error: Contract with a Signer cannot override from (operation="overrides.from", code=UNSUPPORTED_OPERATION]

I'm having problems with my testing. I'm using hardhat and chai library. Solidity contract: function setMsg(string memory _message) public{ message = _message; } and my testing file is ...
  • 105
0 votes
1 answer
168 views

How to check for emitted event inside solidity constructor when writing hardhat tests?

Say I have contract Foo: contract Foo { event OwnerAdded(address indexed owner); address public owner; constructor(address _owner) { owner = _owner emit OwnerAdded(owner) } How would I ...
1 vote
3 answers
174 views

Chai expect not reverting when it does on-chain

I'm currently writing some tests for my contract to make sure the custom errors are being reverted in the contract. Example: function fund(address sender, uint256 amount) public { if (amount <...
3 votes
3 answers
262 views

revertedWith not matched error message

I am expecting a revert from an Imported Ownable.sol contract since the executing address is not the owner of the function but the test fails without matching the error message thrown from the Ownable....
0 votes
1 answer
66 views

ReferenceError: totalSupply is not defined

Im using a course, and I got this error: ReferenceError: totalSupply is not defined and Idk what it means by that, cause I never got this error before. can somebody help me fix this error? thanks and ...
1 vote
0 answers
49 views

Approve and Transfer ERC20 Tokens in contract

I'm pretty new to solidity and struggling with some pretty simple stuff so I was hoping somebody would be able to help me out. Basically, I want to be able to send an existing ERC20 token (ie: USDT) ...
0 votes
1 answer
80 views

hardhat: want to test an emit event that is in a library

file Library library Events { error Mint(); } file Contract calling the event emit Event.Mint() Can't get Chai detect the event that is in the library Throw this error in both cases: ...
  • 1
2 votes
4 answers
342 views

Encountering difficulties while writing tests with .connect(notDeployer)

I am currently working on my tests on hardhat, and I am currently stuck while trying to perform a transaction with another account than the deployer. Here is the test: ... describe("gift", ...
0 votes
0 answers
77 views

Hardhat: AssertionError: expected 9999... to be a number or a date. What's the error exactly?

I have been trying to write test cases in Hardhat and came across this weird error. I am trying to compare 2 values and hardhat is giving me errors. Code: expect(BigNumber(userBalanceNew)).to.be....
0 votes
0 answers
51 views

Is it possible to mock the low-level "call" in Solidity (0.8.9) using Waffle/Chai?

I am using Hardhat with Waffle/Chai assertions. I am trying to set up mock contracts from waffle docs. await mockContract.mock.call.returns(true, ethers.utils.formatBytes32String("")); is ...
  • 1
0 votes
1 answer
24 views

How to validate functions in hardhat test

Actually i am validating an function to check that only whitelisted members or owner can access this function but when i am running this test file i am getting reverting error that Error Validations ...
  • 172
0 votes
1 answer
172 views

How to Properly Unit Test onlyOwner

My contract inherits the ownable.sol which has a mint() method that is marked onlyOwner like below: function mint(address _to, string memory newTokenURI) public onlyOwner I know that within the ...
  • 153
0 votes
0 answers
90 views

Issue with Hardhat's Chai Matchers

I have a solc version 0.6.6 contract which includes a basic withdraw() that looks like this function withdraw() public onlyOwner { owner.transfer(address(this).balance); } I also have couple of ...
0 votes
0 answers
97 views

hardhat-chai-matchers: How to distinguish if a transaction was reverted because it ran out of gas or other reason?

I'm trying to force a ran out of gas error and test it using the new Hardhat's assertion library, @nomicfoundation/hardhat-chai-matchers. First I tried a very low gas of 1: await expect(_attack.attack(...
  • 153
1 vote
1 answer
308 views

How to cause a .call to revert in hardhat test

I am trying to cover all of my lines in my smart contract unit tests with hardhat, and I am not sure how to test the Raffle__TransferFailed() error. Is there some way to force this transaction to fail?...
0 votes
1 answer
180 views

How to reference a contract's library function in hardhat testing

I have a contract that uses an eth/usd price feed library to convert a uint value into its USD equivalent. pragma solidity 0.8.0; import "@chainlink/contracts/src/v0.8/interfaces/...
1 vote
1 answer
183 views

How to use variables set inside a beforeEach with hardhat testing

describe("func", function () { let owner beforeEach(async function () { const [owner] = await ethers.getSigners() }) it.only("should correctly set the address", ...
0 votes
1 answer
114 views

How can we assign owner's address in an OwnableUpgradeable contract?

So, I was testing my contract using Hardhat. So here's my doubt, somehow in my contract owner() and _msgSender() functions are returning different values (0x000.. in owner() ). Can someone pls guide ...
1 vote
1 answer
14 views

Passing an error testing suite in contract

hello how do i make a proper testing suite for error? the contract has this modifier for freeMint() fn modifier protectTier(uint256 _level) { require( _level != privateTier, &...
0 votes
2 answers
853 views

Hardhat test is not working

I try to run a simple test in Hardhat but for some reason it is not working it always returns: 0 passing (1ms) This is the test: const { ethers } = require("hardhat") const { expect, assert ...
  • 101
0 votes
1 answer
92 views

How to return two variables of uint256 types while using Hardhat testing

I have a setter function that takes two arguments and makes a simple update of two variables. uint256 public userAgeA; uint256 public userAgeB; function setAge(uint256 _userAgeA, uint256 _userAgeB) ...
0 votes
1 answer
103 views

Migrating from Waffle

sorry for a newbie question. I have a file from my study with the following code: const { expect, assert } = require("chai"); const { ethers } = require("hardhat"); const { ...
1 vote
1 answer
84 views

Test if a smart contract function has been called

In my hardhat project I'm testing my smart contracts using chai. For the sake of simplicity let's assume my contract is: contract MyContract { function a() external { b(); } function b() ...
  • 605
5 votes
5 answers
443 views

unit testing with chai : is there a way to simulate or force a failed transaction?

Using hardhat and chai, is there a way to simulate/force a transaction failure so the error code will execute? e.g. with the following, I want it to throw the error because the transaction didn't ...
  • 1,058