29
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",...
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",
"...
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 } ...
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
...
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');
...
10
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()
...
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 ...
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-...
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....
6
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 ...
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
...
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 ...
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 ...
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 ...
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 ...
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
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 ...
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());
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 ...
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.
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....
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 ...
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 ...
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 ...
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 ...
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 ...
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:
...
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(
...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
mocha × 146solidity × 66
truffle × 42
testing × 41
hardhat × 34
chai × 21
web3js × 19
truffle-test × 14
javascript × 13
ganache × 12
unittesting × 11
ethers.js × 9
blockchain × 6
testrpc × 6
solc × 6
test × 6
contract-development × 5
ether × 5
nodejs × 5
solidity-0.8.x × 5
events × 3
contract-debugging × 3
smart-contract-wallets × 3
ganache-cli × 3
go-ethereum × 2