Questions tagged [mocha]

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

Filter by
Sorted by
Tagged with
0
votes
1answer
241 views

I have written a javascript code for compiling the solidity code but it's showing error

I was trying to compile my contract using solc compiler but it returning me some errors. I will place my solidity code along with the error that I am getting here. Please help me out to sort this ...
0
votes
0answers
32 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,...
1
vote
1answer
15 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() ...
1
vote
1answer
164 views

Unable to install Mocha Web3 and Ganache-cli

I have been trying to install mocha,Web3 and ganache all through one command but I am facing an issue while installing can someone help me in sorting our this error. Quick response will be appreciated ...
2
votes
1answer
170 views

Trying to understand why my test.js fails

I have a function that is not passing my tests in JS but the function works as expected in Remix. Can you help me contract.sol function test1 () public view returns(bool){ return(true); } ...
0
votes
2answers
29 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; ...
0
votes
1answer
91 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",...
0
votes
1answer
12 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(...
0
votes
0answers
64 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') ...
3
votes
1answer
134 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/...
4
votes
3answers
2k views

How to access in JavaScript multiple return values returned from a solidity function

My contract function returns two values. How to access the multiple return values in JavaScript from a mocha testcase? contract X { function a() pure public returns(uint,uint) { uint p=10; ...
1
vote
0answers
18 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....
1
vote
1answer
389 views

Solidity: How are multiple return values returned?

Working in truffle with TDD testing and am getting some errors when evaluating the return values of some functions. If a function is returning multiple values in Solidity, how are they presented? Is ...
3
votes
2answers
611 views

How can we compile multiple smart contracts by compile.js file?

My compile.js file is below const path = require('path'); const fs = require('fs'); const solc = require('solc'); const ballotPath = path.resolve(__dirname, 'contracts', 'blocktitans_ballot.sol'); ...
0
votes
1answer
182 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 ...
0
votes
1answer
377 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"); ....
-1
votes
1answer
69 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 = ...
0
votes
1answer
17 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" ...
0
votes
0answers
17 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 ...
0
votes
0answers
13 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) => { ...
0
votes
0answers
38 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 ...
0
votes
1answer
40 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. ...
0
votes
1answer
143 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 ...
0
votes
1answer
33 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-...
0
votes
0answers
145 views

How do I write a unit test with Mocha/Waffle to see if the transaction sends the correct amount of tokens to each person?

Intro Basically I have a contract that overrides the transfer function in IERC20. Contract TokenA is IERC20{ function transfer(address recipient, uint256 amount) public override returns (bool) { ...
0
votes
0answers
25 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"...
0
votes
1answer
34 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; ...
1
vote
1answer
98 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 ...
1
vote
1answer
386 views

I got this error during truffle testing. AssertionError: it returns true: expected { Object (tx, receipt, ...) } to equal true

DappToken.sol function transfer(address _to, uint256 _value) public returns (bool success){ require(balanceOf[msg.sender] >= _value); balanceOf[msg.sender] -= _value; balanceOf[...
0
votes
0answers
6 views

Truffle testing:How to convert from Mocha to Solidity

I am following the tutorial at: truffle- Mocha It shows a mocha code for testing. I want to do the testing in Solidity. const SimpleContract = artifacts.require("SimpleContract"); contract(...
0
votes
2answers
625 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 { ...
1
vote
1answer
23 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 ...
6
votes
1answer
5k 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 ...
0
votes
1answer
30 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&...
0
votes
1answer
23 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 ...
0
votes
0answers
40 views

How to use Mocha reporter in Truffle console?

I work with Truffle and Windows Powershell. I set Mocha in truffle-config.js like: module.exports = { . . . mocha: { reporter: '...
2
votes
2answers
936 views

Mocha assertions not returning correctly on assert.equal with BN

Is this an issue with deep matching/object identity? AssertionError: expected <BN: 204fce5e3e25026110000000> to equal <BN: 204fce5e3e25026110000000> It seems strange that chai.should ...
1
vote
2answers
275 views

Timeout w/ Mocha on deploying a contract

Starting to build the following test suite for a contract. It deploys fine on Remix. I get the following error message. 1) "before each" hook for "deploys a contract": Error: Timeout of 20000ms ...
1
vote
0answers
374 views

Mocha timeout when running truffle test. Works fine on ganache, not on public networks

This is the error I get when running truffle test --network kovan: 1) "before all" hook: prepare suite Error: Timeout of 120000ms exceeded. For async tests and hooks, ensure "done()" is called; if ...
13
votes
4answers
5k views

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

I'm running my tests on a private chain and some of them times out: 1) "before all" hook: Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning ...
3
votes
2answers
471 views

How can I pass Mocha switches when calling Truffle?

When calling truffle test some_test.js, I would like to pass Mocha switches. For example: truffle --no-colors test some_test.js works fine. However: truffle --reporter json-streams test some_test.js ...
2
votes
1answer
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) ...
12
votes
2answers
2k views

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

I am using visual studio code and truffle and would like to easily debug my JavaScript tests. A similar question has been asked here: how can I run a truffle test in a debugger?. This has been ...
2
votes
2answers
329 views

How would one use stubs for unit testing solidity code?

Given some contract A, that depends on another contract B, what would be the best way to unit test functions from A without inadvertently testing code from B. Normally, we could use stubs to return ...
2
votes
1answer
387 views

Getting an 'invalid opcode' calling a solidty function from test file

im in the process of learning about ethereum and Solidity, and im having a problem calling a payable function from a test file. This is my contract pragma solidity >=0.4.22 <0.6.0; //pragma ...
1
vote
2answers
581 views

How to test a custom fallback function using web3.js?

I am trying to implement a proxy pattern. I have a proxy contract and a lottery contract, and I would like the proxy contract to delegate calls to the lottery contract. To test that the function ...
0
votes
1answer
338 views

__callback was called after the specified delay

My smart contract should return the ETH rate from Coinbase api .. So i'm using Provable engine and i'm testing my smart contract locally using ganache and ethereum-bridge . It's expected that result ...
3
votes
3answers
657 views

Choosing between Javascript and Solidity for testing smart contracts

Truffle offers two ways to test smart contracts: either using test files written as Solidity contracts, or Javascript/Mocha. https://truffleframework.com/docs/truffle/testing/writing-tests-in-...
13
votes
4answers
9k views

"SyntaxError: Unexpected token import" on truffle test

I get an "SyntaxError: Unexpected token import" error when I do 'truffle test'. I am trying to import a function declared in another file into the java-script test file. I have been searching about ...
0
votes
1answer
83 views

Assert(false) not executing

I am running a mocha test for a solidity SC. One of my test function should pass and then, just after that i am calling assert(false), but it is not executing. it('only manager can call the ...