Questions tagged [mocha]

Mocha is a JavaScript test framework for Node.js programs.

Filter by
Sorted by
Tagged with
0 votes
0 answers
9 views

Accessing the "accounts" variables of the local blockchain

here's the following code : const Vault = artifacts.require("Vault"); describe("Write features of vault", function (accounts) { it("able to receive money", async ...
0 votes
1 answer
7 views

Is there any method in web3js library to know if a given address is a contract address or an wallet's account address?

I am writing solidity tests . I am using JS for testing . Is there any method in web3js library to know that a certain address is a contract's address or not ?
0 votes
1 answer
24 views

Getting "Runtime error: code size to deposit exceeds maximum code size" when deploying contract with web3 on ganache

I'm new to blockchain. I'm trying to deploying a contract through web3 on ganache (the npm version) for testing purpose with mocha. During the deployment attempt, ganache returns with the following ...
  • 1
0 votes
1 answer
15 views

Time based contract testing on Hardhat

So I have this function that will cancel the campaign for my crowdfund contract. I have created a test script for this but doesnt seems to work. Please have a look at my syntax below CrowdFund.sol // ...
  • 1
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
1 answer
29 views

Testing Struct Data

I have a struct and getter function that should return the id to zero when I delete it from my function but I dont know on how to return from my testing script Here is my struct, mapping & ...
  • 1
0 votes
1 answer
27 views

Trying to test a simple contract but failing

I am currently following my first Ethereum course, and I am trying to test the a very simple contract, but it keeps failing. When I run the contract on Remix, everything seems to be working fine, so I'...
  • 103
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 ...
0 votes
0 answers
41 views

got this error while deploying my contract

please help me what is error in above code... this is my code.and it is exact same code what my course teaher taught. compile.js const path = require('path'); const fs = require('fs'); const ...
0 votes
0 answers
35 views

How to load proxy contract's address on implementation contract?

I am learning about Proxy contract . I have been writing contracts in Remix from the day I started learning Solidity . I will explain my problem statement in some points . CounterV1 is implementation ...
0 votes
1 answer
21 views

No events emitted when running test in mocha

Good day, I'm new to blockchain and doing a university project. my contract file seems to be compiling but all my test cases are failing, saying no events emitted. I've included the test case and ...
  • 1
0 votes
1 answer
28 views

nested mapping not working during test

I have a function that have a modifier that it must be the owner before it can cancel the order and I'm using the nested mapping that i've created for this struct as below //mapping mapping(bytes32 =&...
  • 1
0 votes
0 answers
50 views

"before each" hook for "is deployed": SyntaxError: Unexpected token o in JSON at position 1

I am trying to test my contract in Mocha. and I am running into this issue where npm run test is giving the following result. 1) "before each" hook for "is deployed" 0 passing (...
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
28 views

Custom error not working during test

I tried to use custom error instead of using a require in modifier like so: modifier tokenExist(bytes32 ticker) { if(tokens[ticker].tickerAddress != address(0)) revert Exchange__TokenNotExist()...
  • 1
1 vote
0 answers
42 views

Mocha Testing timeout in package.json or Hardhat Config file?

i have a conflicting question. Where should I put the mocha implementation in package.json or hardhat config file? i would assume that if i put "mocha":{ "timeout" : x} in package....
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
306 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
141 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
0 answers
36 views

How to add delay while testing smart contract

Currently I am testing smart contract with truffle framework. In my smart contract I have a function which can be executed for limited time period(say 100 seconds from contract deploy). How can I ...
  • 1
1 vote
0 answers
31 views

Hardhat: Report tests without executing them

according to mocha docs there is --dry-run flag that does it, but it does not work with hardhat: Error HH305: Unrecognized param --dry-run when running yarn hardhat test
  • 36
1 vote
1 answer
307 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?...
1 vote
1 answer
182 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
2 answers
330 views

Exceeds block gas limit or base fee exceeds gas limit

I am learning web3, and a person is using solidity ^0.4.5, and I am adapting his code to solidity ^0.8.9, and I am facing this problem when if I have 1000 here: factory = await new web3.eth.Contract(...
0 votes
1 answer
44 views

Chain tests in mocha framework with smart contract testing

I am doing smart contract testing with hardat and mocha framework. Suppose I have two following it blocks testing contract A. it('test1', () => { contractA.updateValue(val); // some other ...
  • 198
1 vote
0 answers
367 views

Promise is returning timeout error [Timeout of 20000ms exceeded]

Error: Timeout of 20000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/home/hh-fcc/hardhat-smartContract-lottery-fcc/test/...
0 votes
0 answers
64 views

how can do a Load Test for a web3 script?

In my scenario, I use truffle and ganache. I created a simple Contract like this contract SimpleContract { string public name ; uint counter = 0; event myEvent(uint id , string name); ...
0 votes
1 answer
100 views

How should I approach testing functions that call already deployed contracts? [ethers.js] [chai] [mocha]

Suppose my contract utilizes the WETH contract (address 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2) in some manner. I can write test cases with Chai/Mocha just fine for most cases (they're "...
2 votes
1 answer
71 views

Truffle contract factory test with Javascript

Here is my smart contract source code. // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.9; contract CampaignFactory { address[] public deployedCampaigns; function createCampaign(uint ...
1 vote
1 answer
61 views

How to write the javascript test code on whether a smart contract function is successfully executed or not?

I wrote a lottery smart contract and gonna to test an "enter" functionality of the smart contract. The below code is javascript test code for the "enter" function of the smart ...
0 votes
1 answer
25 views

Hi,I had written the code for voting purpose.Can you guys help me out to write a truffle testing code for this?

// SPDX-License-Identifier: MIT pragma solidity ^0.8.9; contract voting{ address private onlyowner; uint private Virat =0; uint private Rohit =0; constructor(){ onlyowner = msg.sender; } ...
  • 11
0 votes
0 answers
35 views

Mocha Test Error

When running commands for testing such as npm run test, the following error occurs. Note: I have the LTS node.js v.16.15 installed.
0 votes
0 answers
153 views

Unit test in hardhat of SushiToken function delegatebySig

I'm playing around with contracts and trying to get the hang of it. My problem is I'm stuck with a singe function I like to unit test. I have absolutely no idea how to create an signatory in the ...
0 votes
1 answer
698 views

solidity function functionCall from openzeppelin-contracts-accounts reverted

I was calling flashLoan function using following parameters for hardhat testing expect( await this.pool.flashLoan(100, attacker.address, this.token.address, 0x00) ).to.be.not.reverted; This is ...
0 votes
0 answers
20 views

cosloe log multiple variables returned from Solidity functions

So I have a Solidity function which returns multiple values: function getReserves() external returns (uint112, uint112) { require(UniswapPair!= address(0), "Create Pair first"); ...
  • 1
0 votes
2 answers
109 views

Testing against local network vs testnet

I have a simple test to mint tokens on a newly deployed contract. While developing, I was testing locally and the test never failed. Now that the contract is deployed to the testnet, I get a 50% ...
  • 103
0 votes
0 answers
247 views

Hardhat/waffle: ether transfer with no effect

I'm going through the fantastic ethernaut challenges to learn solidity. In order to test and deploy my solutions I'm using hardhat/waffle/ethers. When running my contracts locally on the hardhat ...
0 votes
2 answers
980 views

How to test if msg.value is 1 ether in test

I have a function which mints when 1 eth is send to it. The function is function _mint() public payable{ require(msg.value == 1 ether , "Send more ethers: 1 ether is required"...
  • 113
0 votes
0 answers
688 views

Error "Returned values aren't valid, did it run Out of Gas" for simple view function

I am trying to compile, deploy and run a function for a simple Ethereum contract, but when I run the function(a simple view function that returns an int) it says that I ran out of gas: $ npm run test ...
1 vote
0 answers
174 views

Integration Tests for Smart Contracts

So we write integration tests in order to test interactions between contracts. Could be interactions with on-chain contracts and also off-chain services. When it comes to unit tests, I think I have a ...
5 votes
3 answers
2k views

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

I'm running a test using waffle in hardhat. import { assert, expect } from "chai" . . . it("can only be changed through governance", async () => { await expect(box.store(...
4 votes
2 answers
2k views

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

I'm waiting for an event to fire in my tests. This isn't happening from a transaction I'm executing so I can't use anything from transaction receipts (for example, these won't work Listening to events ...
0 votes
1 answer
458 views

How do i get mocha's before() hook to work properly?

This is the test code im trying to run: const {expect} = require("chai"); const {ethers} = require("hardhat"); const {SignerWithAddress} = require("@nomiclabs/hardhat-ethers/...
6 votes
11 answers
3k views

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

I am facing rather unusual behavior from hardhat. I have written my test cases in a file inside test folder when i run yarn teston the terminal it gives me 0 passing. Don't have any idea as to why it ...
1 vote
2 answers
777 views

How to test a function which calls another contract from inside the function?

newToken.sol import "@openzeppelin/contracts/token/ERC1155/IERC1155Receiver.sol"; contract newToken is ERC1155 { using Counters for Counters.Counter; Counters.Counter private ...
1 vote
4 answers
871 views

Uncaught Error: Callback was already called in mocha

Following an aging tutorial, I'm trying to learn how to test a smart contract: const assert = require('assert'); const ganache = require('ganache-cli'); const Web3 = require('web3'); const web3 = ...
  • 155
0 votes
2 answers
1k views

Invalid Chai property when testing smart contract

In a hardhat project I'm using smock to mock an ERC20 contract and chai's calledOnce to assert: const {expect} = require("chai") const {artifacts, ethers} = require("hardhat&...
  • 605
1 vote
1 answer
931 views

Debug hardhat project using intellij

I've created an hardhat project that uses typescript. As I'm trying to run the test from intellij I'm getting: TSError: ⨯ Unable to compile TypeScript: test/index.ts(2,10): error TS2305: Module '"...
  • 605