20 votes
Accepted

"SyntaxError: Unexpected token import" on truffle test

All it needed was to add a package.json file to the project directory with some babel dependencies and doing an 'npm install'. Also, adding a '.babelrc' file to the truffle project directory. Finally, ...
19 votes

How to get contract's ether balance at hardhat / waffle

const { ethers, waffle} = require("hardhat"); const provider = waffle.provider; const balance0ETH = await provider.getBalance(user1.address);
Russo's user avatar
  • 1,764
15 votes

How to get contract's ether balance at hardhat / waffle

Maybe you can try: await contract.provider.getBalance(contract.address) contract is a 'Contract' class in @ethersproject.
amber's user avatar
  • 151
12 votes
Accepted

Transaction gas cost in truffle test case

Note: This answer is only valid for Truffle v4. I've tried this test with the MetaCoin example from truffle (ie run truffle unbox metacoin in an empty directory) var MetaCoin = artifacts.require("./...
Ismael's user avatar
  • 30.5k
11 votes
Accepted

How do you create a new contract instance with an address *and* arguments?

You can't. In the first example, you are not merely instantiating a contract that is there. You are deploying a new instance and passing arguments into the constructor function. Constructors run one ...
Rob Hitchens's user avatar
11 votes
Accepted

Hardhat vs foundry, which to use for testing?

When it comes to testing, Foundry excels compare to Hardhat, the speed difference is incredible. In my test with 1k queries and transactions on-chain the difference is from 15 min with hardhat to 1 ...
donoso.eth's user avatar
  • 1,423
8 votes
Accepted

Using different accounts for executing a function in solidity unit testing

As mentioned in the comments, common development frameworks (Hardhat, Truffle) provide you with multiple accounts. You can't spoof msg.sender in contracts, but you can change which account sends a ...
Lauri Peltonen's user avatar
6 votes

"SyntaxError: Unexpected token import" on truffle test

I tried i_robot's solution, and it almost* worked, I had to add: require('babel-register')({ ignore: /node_modules\/(?!zeppelin-solidity)/ }); require('babel-polyfill'); at the top of my truffle....
Kevin's user avatar
  • 199
6 votes

How to get contract's ether balance at hardhat / waffle

You can obtain the ETH balance of your Contract directly using the following code snippet: import {ethers} from "hardhat" const contractBalance = await ethers.provider.getBalance("...
Shariq Anwar's user avatar
6 votes
Accepted

What is `signer.sendTransaction(...)` and its purpose in the Hardhat mainnet forking docs?

What are the parameters within signer.sendTransaction() Signer is an ethers.js concept. Check its docs to see how it is used, but a simple example is this: await signer.sendTransaction({ to: ...
Franco Victorio's user avatar
5 votes

unit test with truffle, solidity or mocha?

I have written more than a thousand tests for different smart contracts and can say that you will almost always want to use javascript/mocha with truffle. There are very few cases that I have run into ...
TovarishFin's user avatar
5 votes

How to access in JavaScript multiple return values returned from a solidity function

I'm not sure the version of web3 used in the previous answer by ivicaa. On web3 "v1.0.0-beta.37", multiple values are actually returned in an Object, not an Array. However, since the ...
fodisi's user avatar
  • 51
5 votes
Accepted

Testing Token with Uniswap liquidity provisioning using hardhat

It sounds like you're facing one of two problems - either you're using a local dev chain and haven't deployed Uniswap to it, or you're using a testing fork of a chain, and haven't told your test suite ...
Linum Labs's user avatar
  • 1,438
5 votes
Accepted

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

minimal code to implement Lauri's suggestion Target.sol (contract being tested) pragma solidity ^0.8.10; contract Target { address payable owner; constructor () { owner = payable(...
sola24's user avatar
  • 1,228
4 votes
Accepted

Unit tests written in Solidity, how to change msg.sender address, msg.value,

You cannot change the sender address from within solidity. The solidity tests are run inside the EVM and it doesn't allow the modification of the msg.sender. It is possible that a modified version of ...
Ismael's user avatar
  • 30.5k
4 votes

Reset balance in truffle test accounts after each test

I had a similar issue. Never figured out how to reset the balances, but if you use the ganache-cli provider you can configure how much ether your test accounts are given at the start, so you can bump ...
Howard's user avatar
  • 421
4 votes

Transaction gas cost in truffle test case

The following code is the same code as Ismael's code, with the changes to work with Truffle v5, as getBalance's return type, beside gasUsed and gasPrice values are not BN by default: var MetaCoin = ...
Shadow78's user avatar
4 votes
Accepted

solidity-coverage adds extra events which make unit-test fail

I would suggest using my truffle-assertions library to assert whether events have been emitted. Using that, it doesn't matter what index an event has been emitted, just that it has been emitted. That ...
Rosco Kalis's user avatar
  • 2,137
4 votes

How to get contract's ether balance at hardhat / waffle

Updated 2022 contractFactory = await ethers.getContractFactory("MySmartContract"); contract = await contractFactory.deploy(); await contract.deployed(); const contractBalance = await ...
Aaron Ullal's user avatar
4 votes

How to configure `truffle test` to fork the main/rinkeby chain as its local chain?

You can fork Rinkeby or the Mainnet pretty easily using Ganache-CLI. Read this article for a detailed explanation https://blockheroes.dev/test-smart-contracts-on-mainnet/ The simplest command is: ...
Ioana Roceanu's user avatar
3 votes

How to Compare Two Arrays in Solidity

I'm not sure if this implementation works now. But as having a hash collision is so so so... hard, you can hash the two different arrays and compare it's hashes. If this is possible to do, will be so ...
CPereez19's user avatar
  • 2,835
3 votes

Mock Smart Contract For Unit Testing

The other approach would be to use a mocking library, like Doppelganger
Marek Kirejczyk's user avatar
3 votes
Accepted

Mock Smart Contract For Unit Testing

I created a little implementation of the Storage contract. Note that I renamed your abstraction to StorageInterface. I also added some functions to Reader, since there isn't much to test unless it ...
Rob Hitchens's user avatar
3 votes

How to access in JavaScript multiple return values returned from a solidity function

I would suggest that you learn how to work with truffle. It helps with compile, test, deploy of smart contracts. It is also based on mocha. For an example of writing unit tests in truffle with ...
ivicaa's user avatar
  • 7,519
3 votes

How to get contract's ether balance at hardhat / waffle

This way is work for me const { ethers } = require('hardhat') async function getBalance(address) { const balance = await ethers.provider.getBalance(address) return hre.ethers.utils.formatEther(...
Mhammed Talhaouy's user avatar
3 votes

How to catch multiple events from a single function in the hardhat framework?

I found the answer. You just have to append the next event in the same expect. Something like this: await expect(contract.claimTokens(busd.address)) .to.emit(contract, "Claimed") ....
Shubham Sharma's user avatar
3 votes
Accepted

How to write tests for "msg.sender" being a contract?

You can impersonate any address with the impersonateAccount network helper. Given the address of some contract, you have to do something like this: await helpers.impersonateAccount(contractAddress); ...
Franco Victorio's user avatar
3 votes

Hardhat vs foundry, which to use for testing?

I agree with other answers about foundry's speed, it is incredible, as is its reandomization testing and its debugging options. So, if you need fast tests or are stuck on an error, quickly (and easily)...
kuco 23's user avatar
  • 228
2 votes

Is there a way to unit test Solidity contracts without running ethereum node?

You have to run a node at least locally, but I think private chains are what you're looking for. Basically you just run a private chain on your local computer (create your own chain) and then mine ...
jojeyh's user avatar
  • 769
2 votes

Problems with UINT values when testing with mocha/chai

You can use .valueOf() to get the return value from the getter function. I've used this in one of the test cases I wrote sometime back. You can take a look at this for reference. Hope this helps.
Sanchit's user avatar
  • 3,482

Only top scored, non community-wiki answers of a minimum length are eligible