Skip to main content
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,784
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
15 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,503
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
12 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
  • 55.4k
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

Updated 2022 contractFactory = await ethers.getContractFactory("MySmartContract"); contract = await contractFactory.deploy(); await contract.deployed(); const contractBalance = await ...
Aaron Ullal's user avatar
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
6 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
  • 278
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,448
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,238
5 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
5 votes

Hardhat vs foundry, which to use for testing?

Here is my experience with Hardhat - for us, Hardhat works very well. We use it as the Test Node, I think in the Foundry ecosystem this is equivalent to the Anvil. So here are the points why Hardhat ...
tenbits's user avatar
  • 2,428
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 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
4 votes

Hardhat vs foundry, which to use for testing?

I personally use Foundry. You can test whatever you want in either, so won’t (and shouldn’t) have to use both in a given project. The biggest difference is that Foundry lets your write tests in ...
Alex Otsu's user avatar
  • 478
4 votes

Hardhat vs foundry, which to use for testing?

Go with Foundry - it's faster and more ergonomic than anything else on the market right now. Solidity code should be tested in Solidity itself. It doesn't matter if you are coming from the JavaScript ...
Paul Razvan Berg'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
zkMarek's user avatar
  • 553
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
  • 55.4k
3 votes

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

The second example is creating an instance of the contract at a specific address that already exists on the blockchain. You pass in the address to MyContract to tell the EVM where to look when ...
Shane Fontaine'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

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