Questions tagged [mocha]
Mocha is a JavaScript test framework for Node.js programs.
146
questions
0
votes
0
answers
18
views
How to run a hardhat node in the cloud
I have a set of tests i run locally using hardhat and mocha. However, I would like to run these tests on every push to prod. How can I start a hardhat node in the cloud for the testing.
1
vote
0
answers
24
views
How to fix `Invalid callback object specfied` error when deploying a smart contract?
My smart contract:
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
contract Inbox {
string public message;
constructor (string memory initiialMessage) {
message = ...
0
votes
2
answers
18
views
Trying to deploy simple smart contract in a local nodejs, mocha environment but receiving error from: 'npm run test'
I am trying to deploy a simple smart contract for testing.
Attached are the output of: 'npm run test'
as well as my test.js file and my package.json file
I hope its something obvious sorry if this is ...
2
votes
1
answer
84
views
TypeError: Cannot read properties of undefined (reading 'checkUpkeep')
i'm getting this error while testing patrick collins smart-contract-lottery project, below is
the part where the error coming from.
describe("checkUpkeep", async function () {
...
0
votes
0
answers
18
views
logging events while testing
In Current Version of ethers v6 ., once the event is emitted how we should log them ., because the older method of logging from transaction receipt is not working "
for example this is my ...
0
votes
0
answers
29
views
Hardhat Errors: Invalid value null supplied to : QUANTITY
Getting this error some times while testing a Hardhat file.
But when I cancel that script and run that same script again, it works.
The complete error is here:
"before each" hook: Before ...
0
votes
0
answers
39
views
Burn ERC1155 from ERC721 Contract
Would like to understand on how burn mechanism works on ERC1155, the ERC1155 is an airdrop token that will be use on GeneratedNFT.sol mint function so that it will to mint an ERC721 token and burn the ...
0
votes
2
answers
232
views
Testing ERC20 Token: Unable to cover Transfer and Approve from null/zero address
I'm working on a project using Hardhat and OpenZeppelin library for an ERC20 token. I've been able to achieve almost 100% coverage, but there are two lines that I'm struggling to cover:
transfer ...
0
votes
0
answers
189
views
Cannot read properties of undefined (reading '0')
I am following FreeCodeCamp's javascript Blockchain/SmartContract course. Facing issue at lesson 9 staging test. Contract deployment and unit tests are running fine. Running mocha test on sepolia ...
1
vote
1
answer
123
views
Testing for success (no revert) in Hardhat
I'm using hardhat with mocha and chai to write tests for a contract. I'm trying to write a test that will expect a function call to be successful (not to revert); the function does not emit any events....
1
vote
2
answers
791
views
How to create a raw ERC-4337 UserOperation from scratch and then send it to bundler?
I want to create a raw ERC-4337 UserOperation. I am using ethers library to write the test cases .
According to ERC-4337 official documentation , the UserOperation is a struct with many fields .
...
0
votes
2
answers
59
views
How do I test burning tokens in my ICO contract?
I'm writing tests for the burn func in ICO.sol
The first two tests are passing, but when I come to test where the tokens are indeed burned, the state of the ICO seems to change. To burn tokens, the ...
0
votes
0
answers
77
views
Mocha Tests unexpected error
My tests where running well but not when I run yarn hardhat test, I get the error below.
Error
An unexpected error occurred:
TypeError: describe is not a function
at Object.<anonymous> (/...
1
vote
1
answer
39
views
why do i get the GS200 error when setting up safe?
I'm trying to set up a gnosis safe with two owners and threshold of two but i keep getting this error and i'm not sure why.
I checked my inputs with other scripts the have the same type of inputs ...
1
vote
0
answers
14
views
Trying to run a javascript test through Truffle. Truffle is not able to read the javascript file or I cannot understand the issue here
I am trying to run truffle test to test a javascript file on windows through powershell. The results keep showing a syntax error on the first character of the .js test file after compiling all ...
2
votes
2
answers
1k
views
How to work with block timestamps in ethers and hardhat?
I have a time-dependent contract that has two require statements
function launch(uint256 _goal, uint32 _startAt, uint32 _endAt) external {
require(_startAt >= block.timestamp, "...
0
votes
0
answers
22
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
20
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
890
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 ...
0
votes
1
answer
108
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
vote
1
answer
843
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
86
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 & ...
0
votes
1
answer
30
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'...
1
vote
1
answer
39
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
155
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 ...
1
vote
0
answers
419
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
71
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 ...
0
votes
1
answer
39
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 =&...
0
votes
1
answer
25
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?
0
votes
0
answers
34
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
vote
0
answers
103
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
41
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
91
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
19
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
646
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
257
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 ...
1
vote
0
answers
61
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
1
vote
1
answer
540
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
628
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
938
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
100
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 ...
1
vote
0
answers
661
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
1
answer
146
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
124
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
113
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
27
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;
}
...
0
votes
1
answer
1k
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
2
answers
134
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% ...
0
votes
2
answers
1k
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"...
0
votes
0
answers
829
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
...