2022 Developer Survey is open! Take survey.

Questions tagged [mocha]

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

Filter by
Sorted by
Tagged with
0 votes
0 answers
8 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; } function ...
user avatar
  • 1
0 votes
0 answers
9 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.
user avatar
0 votes
0 answers
14 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 ...
user avatar
0 votes
1 answer
28 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 ...
user avatar
0 votes
0 answers
14 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"); ...
user avatar
  • 1
0 votes
2 answers
48 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% ...
user avatar
  • 103
0 votes
0 answers
99 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 ...
user avatar
0 votes
2 answers
239 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"...
user avatar
  • 51
0 votes
0 answers
351 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 ...
user avatar
0 votes
0 answers
49 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 ...
user avatar
1 vote
2 answers
429 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(...
user avatar
1 vote
2 answers
683 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 ...
user avatar
0 votes
1 answer
112 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/...
user avatar
1 vote
7 answers
788 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 ...
user avatar
1 vote
2 answers
159 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 ...
user avatar
0 votes
4 answers
288 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 = ...
user avatar
  • 125
0 votes
1 answer
426 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&...
user avatar
  • 431
1 vote
1 answer
474 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 '"...
user avatar
  • 431
0 votes
0 answers
9 views

How to catch the revert error in javascript mocha for testing solidity contracts [duplicate]

I struggled to find a nice solution to my question and thought of sharing it here so that others could benefit and others may have better suggestions. I have a solidity code where I have a function ...
user avatar
  • 227
15 votes
4 answers
5k views

Run a single hardhat test

I have a test file in hardhat like so: const { expect } = require("chai"); describe("contract tests", function () { it("does function one", async function () { ...
user avatar
3 votes
1 answer
76 views

Chai closeTo on Ethereum Events

I know that we can use closeTo chai matcher like this: expect(BigNumber.from(100)).to.be.closeTo(BigNumber.from(101), 10); And we can test whether a transaction emits an event with specific arguments ...
user avatar
  • 438
1 vote
2 answers
496 views

Can`t fix: AssertionError [ERR_ASSERTION]: Invalid callback object specified

I´m doing my first smart contract, these errors are probably no npm but versioning problems. Though intensive research I could not find a proper way to fix it. Thank you for your help! const assert = ...
user avatar
  • 11
1 vote
0 answers
76 views

How to write Test case for Internal functions in solidity which inherits the ERC721

**This is my Smart Contract code which has inherited the ERC721 and I want to write the test case for _setTokenURI ** contract ProductNFT is ERC721 { constructor () public ERC721(_tokenName,...
user avatar
1 vote
1 answer
382 views

How to test ether value in truffle test script using Mocha and chai

I'm having a method inside my contract which return a variable having value is 100 ether for example. E.g: uint256 private baseline constructor() { baseline = 100 ether; } function getBaseline() ...
user avatar
1 vote
2 answers
413 views

How to write test case for the require function

**Contract** contract ProductNFT is ERC721{ uint public totalSupply; address public wadmin; address public brand; address public designer; ...
user avatar
0 votes
1 answer
2k views

Is it possible to send ether to a contract after deployment inside a hardhat test?

This is using Hardhat with ethers.js: My test wants to deploy a contract and then I want to send ether to it: const { ethers } = require("hardhat"); describe("HandleEther contract",...
user avatar
  • 147
1 vote
1 answer
1k views

Use contract from outside contract folder in hardhat test

When I try to import a contract from other artifacts folders or node modules into the dapp test using this.LinkToken = await ethers.getContractFactory('@chainlink/contracts/src/v0.4/LinkToken.sol') ...
user avatar
  • 23
0 votes
1 answer
17 views

Unit testing does not fail when I add 1 to the variables to be equated to

Just started with unit testing after creating my token, but the test cases are passing even if I add a number in the equals bracket. In test one, if I change to expect(await instance.balanceOf(...
user avatar
  • 1
2 votes
1 answer
67 views

Cannot call method with mocha

I am running some test in my first smart contract called Inbox.sol. I Want to call the method .message in order to see if it is equal to the default variable I use when I deploy the contract with web3....
user avatar
  • 180
-1 votes
1 answer
315 views

SyntaxError: await is only valid in async function

I do not understand why I am having a syntax error? I am trying to run my test javascript which is available here const assert = require('assert'); const ganache = require('ganache-cli'); const Web3 = ...
user avatar
  • 227
5 votes
1 answer
5k views

Testing Error thrown with hardhat

How would you test the following function with solidity v0.7 and hardhat v2.3.3? function myFunction(int8 _num) public { require(_num > 5, "Num should be bigger than 5"); ....
user avatar
0 votes
1 answer
102 views

Aave Flashloan `reverted with reason '73'`

I'm getting an error when attempting to execute an Aave V2 Flashloan within a Hardhat-mocha test while connected to a fork of the Kovan network. It's unclear to me what revert reason "73" ...
user avatar
  • 131
0 votes
1 answer
150 views

How to test multiple contracts?

I have created a Ethereum dapp that has multiple contracts that access one another and use methods within each other. I would like to create a Javascript test which would emulate a use-case for this ...
user avatar
0 votes
0 answers
36 views

Testing a smart contract with Truffle

so I'm testing a smart contract a js framework called mocha and running the test through truffle. require('chai') .use(require('chai-as-promised')) .should() contract('Token', (accounts) => { ...
user avatar
1 vote
0 answers
83 views

Withdraw funds from contract

I made the following withdraw function in my smart contract: function withdraw() public onlyOwner { (bool success, ) = msg.sender.call{value: address(this).balance}(""); } I call ...
user avatar
  • 17
0 votes
1 answer
341 views

mocha beforeEach is not running

beforeEach hook is not running. THis is a strange error, I googled but did not find anything. For anything strange, I delete node-modules and reinstall and it works but this time it did not work. ...
user avatar
  • 491
0 votes
1 answer
583 views

Code Example: Waiting for N blocks then running a method?

I'm new to Javascript and Etherjs. I'm trying to interact with a specific contract in the following way. The contract has two functions, hi and bye. I want to first run contract.hi();, wait N blocks ...
user avatar
0 votes
1 answer
98 views

Issue while testing a smart contract

Im getting myself into smartcontract development using solidity, I wrote the below smart contract and I have some issues with the Chai and Mocha test for the first function addCandidate() //SPDX-...
user avatar
0 votes
0 answers
92 views

error.message.indexOf is not a function truffle ganache

for truffle developers var Election = artifacts.require('./Election.sol'); contract("Election",function(accounts){ var electionInstance; it("throws an exception for double voting"...
user avatar
5 votes
1 answer
579 views

Test contract internal functions

I'd like to test a contract internal functions. I used to inherit the contract and test it with truffle but that involved Solidity test files. Can I do the same with my current stack (hardhat/mocha/...
user avatar
0 votes
1 answer
92 views

Truffle test: Problem with understanding the test

I am trying to understand the test provided at: Tutorial ("Test balance after deposit", function() { return SimpleBank.deployed().then(function(instance) { sb = instance; ...
user avatar
  • 1,195
1 vote
1 answer
404 views

Perimeter of mocha, waffle and chai

Here is what i've understood (please tell me if i am wrong): Mocha (mochajs.org) is a testing framework Waffle (getwaffle.io) is another testing framework chai: This is not a framework, this is a ...
user avatar
  • 1,039
1 vote
1 answer
28 views

How to resolve the issue while trying to test my contract

I'm new to blockchain development and can't figure out what I have done wrong. This is my smart contract Election : pragma solidity ^0.5.0; contract Election { struct Candidate { uint ...
user avatar
0 votes
1 answer
158 views

Error while testing a smart contract functionality with Mocha

I wanted to test my code functionality with the Mocha Framework, but every time it responded me with an error, not an assertion. The specific error was: Error:cannot find module "ganache-cli&...
user avatar
0 votes
1 answer
45 views

Synchronizing test and web3 enabled nodejs service

I have a test suite running in truffle, and an external web3 connected node service running - both connected to a ganache-cli network. My problem is: my tests are supposed to pick up an event ...
user avatar
0 votes
1 answer
270 views

Solc-js: error "before each" hook for "deploys a contract": ReferenceError: accounts is not defined

I have just started building contracts. Kindly help me in resolving this error. Attached image of error. Lottery/contract/Lottery.sol: pragma solidity ^0.4.17; contract Lottery{ address public ...
user avatar
0 votes
5 answers
1k views

solc-js error: "before each" hook for "deploys a contract": SyntaxError: Unexpected token u in JSON at position 0

The issue is probably here: const { interface, bytecode } = require('../compile');. interface in the test file is undefined. inbox/contracts/Inbox.sol: pragma solidity ^0.7.0; contract Inbox { ...
user avatar
  • 23
0 votes
1 answer
467 views

Get contract's balance in js test

When getting contract's balance on Remix, it returns the correct answer. But when the same function is called with JS Test, it returns a lot of junk. I referred to How to get a contract's balance ...
user avatar
  • 141
7 votes
1 answer
12k views

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

That's the error that occurs when I try to test my contract. Can someone tell me what the problem is? That's the code for the test: const assert = require('assert'); const fs = require('fs'); const ...
user avatar
  • 93
2 votes
1 answer
2k views

When testing a contract, NodeJS returns: "SyntaxError: Unexpected token u in JSON at position 0"

My NodeJS script gives me this error: 1)"before each" hook for "deploy a factory and a lottery" SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse((anonymous)) at Context.(anonymous) ...
user avatar
  • 93