Skip to main content
Share Your Experience: Take the 2024 Developer Survey
27 votes
Accepted

Run a single hardhat test

Use .only(). For example, your test file would look like this: const { expect } = require("chai"); describe.only("contract tests", function () { it("does function one",...
Julissa DC's user avatar
  • 1,918
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, ...
16 votes
Accepted

How can I debug a truffle JS unit test with vscode?

Add the following to your configurations in launch.json: "configurations": [ { "name": "run tests", "type": "node", "request": "launch", "...
Sebastian Kropp's user avatar
16 votes
Accepted

Testing Error thrown with hardhat

When I test a function for revert I do the following in my javascript test file (e.g. myContractTest.js): const { expect } = require('chai'); const { ethers } = require('hardhat'); const { BigNumber } ...
Majd TL's user avatar
  • 3,237
14 votes

Run a single hardhat test

You'll be glad to know as of hardhat 2.9, the --grep parameter has been added to the test task! This adds the ability to pass in a regular expression through to mocha! This is cool because you can ...
Namaskar's user avatar
  • 249
10 votes

Error: you must provide the json interface of the contract when instantiating a contract object

You could instead try something like this to instantiate a contract : copy paste the abi of your contract in a json file read the file : const contractJson = fs.readFileSync('path_to_abi_file.json'); ...
clement's user avatar
  • 4,312
9 votes

How to set the timeout for truffle test's before block?

Add this in your Truffle configuration file (truffle.js or truffle-config.js): mocha: { enableTimeouts: false, before_timeout: 120000 // Here is 2min but can be whatever timeout is suitable ...
goodvibration's user avatar
9 votes
Accepted

Solidity Exceeds block gas limit during Mocha Tests with Ganache-cli

The error message "n: Exceeds block gas limit" means your transaction has declared a gas value greater that the maximum allowed in the network. A client will reject such transaction. With ganache-...
Ismael's user avatar
  • 30.5k
8 votes
Accepted

I am running my tests in the hardhat environment but it gives me 0 passing all the time

This can happen if await ethers.getSigners() is executed in describe clause. Example that doesn't work (0 passing): describe("ContractName", async () => { await ethers.getSigners() ...
Kof's user avatar
  • 2,954
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
Accepted

Wait / Listen for an external event to fire in a test - ethers / hardhat

With the help of Santiago, I finally got it! So the code should look like this: it.only("Our event should successfully fire on callback", async () => { const callbackValue = 777 ...
Patrick Collins's user avatar
6 votes
Accepted

Property 'revertedWith' does not exist on type 'Assertion'

Pre June 2022 You need to also add ethereum-waffle to your package, not just the nomiclabs one @nomiclabs/hardhat-waffle. you need both yarn add --dev ethereum-waffle you just only need to install ...
Patrick Collins'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
Accepted

How to properly catch events using MochaJs

Explanation: This functionality is not available in Truffle at the moment, as logged events are not displayed on passing tests (source code). I like the idea of tracking events on passing tests as ...
Travis Jacobs's user avatar
5 votes
Accepted

Is the address used to deploy contracts in JS tests in truffle always accounts[0]

Yes, that is the case. accounts[0] is the default. If you like to test calling a function from another account, you can pass {from: accounts[1]} as the last parameter of your function. If you would ...
Bumblebee's user avatar
  • 1,761
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
4 votes
Accepted

New Ganache (1.1.0-beta.0) Causes Invalid JSON RPC response

Maintainer of ganache here. This is a known bug in the beta. See https://github.com/trufflesuite/ganache-cli/issues/471
Ben Burns's user avatar
  • 183
4 votes

Solidity Exceeds block gas limit during Mocha Tests with Ganache-cli

Thanks to Ismael's solution: const ganache = require('ganache-cli'); const Web3 = require('web3'); const options = { gasLimit: 8000000 }; const provider = ganache.provider(options); // quote from ...
Russo's user avatar
  • 1,784
4 votes
Accepted

Mocha assertions not returning correctly on assert.equal with BN

Change this: assert.equal(supplyCap, _cap); To this: assert.equal(supplyCap.toString(), _cap.toString());
goodvibration's user avatar
4 votes

Property 'revertedWith' does not exist on type 'Assertion'

Hardhat has released a new package named hardhat-toolbox to replace hardhat-waffle. Install it with npm install --save-dev @nomicfoundation/hardhat-toolbox and then import it in your hardhat.config.js ...
Antonio U's user avatar
  • 649
3 votes

How can I pass Mocha switches when calling Truffle?

I can do it by adding this to the Truffle configuration file (truffle.js or truffle-config.js): mocha: { useColors: false, reporter: "json" } Available reporters are described here.
goodvibration's user avatar
3 votes

How can I debug a truffle JS unit test with vscode?

Clearly, if you have installed truffle globally (npm install -g truffle), you will need to provide the full path of that directory e.g.: "program": "/usr/local/lib/node_modules/truffle/build/cli....
Gogogol's user avatar
  • 31
3 votes
Accepted

Using ganache-cli provider causes "Error: Provider not set or invalid"

I have read the solution in one of Stephen Grider's Udemy course: const assert = require('assert'); const ganache = require('ganache-cli'); const Web3 = require('web3'); const ...
Carlos Jimenez's user avatar
3 votes
Accepted

Too many decimal places error

wei is the smallest unit of Ether and cannot be subdivided. This line: web3.utils.toWei('0.02', 'wei') says to convert .02 wei to wei, which obviously cannot be done because .02 wei cannot exist to ...
Shawn Tabrizi's user avatar
3 votes

How to define a function inside a truffle contract test

You can use the "before" "beforeEach" "after" and "afterEach" methods to run certain things you need before / after tests. You can also nest "describe" blocks to a max of 4 levels ( if i remember ...
Micky Socaci's user avatar
  • 1,312
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

Run a single hardhat test

For anyone that finds this and is having a similar issue to me, .only or fit/fdescribe work great within a single file. If you have multiple files you can do hardhat test ./test/testfile.js. .only or ...
Ping's user avatar
  • 31
3 votes

Property 'revertedWith' does not exist on type 'Assertion'

I was experiencing this issue as well and most online help weren't addressing the underlying cause. You also have to add the following to your test files to use event emitters from chai matchers: ...
Damilola Fagoyinbo's user avatar
3 votes

solidity function functionCall from openzeppelin-contracts-accounts reverted

i was able to solve this. the issue was functionCall requires a bytes String of an function. So here i was in need of approve function byte string. bytes memory data = abi.encodeWithSignature( ...
Tanjin Alam's user avatar
3 votes
Accepted

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

Not only on smart contract tests, but in Javascript and Nodejs, in general, is common to find many async/await function calls. The Javascript is single-threaded. So, how do you get this sense of ...
Jeremy Then's user avatar
  • 4,609

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