Introduction to Unit Testing

My savior, thank you

1 Like

Hey @dan-i
I am getting the below error while trying to test:

aadyakalra@Aadyas-MacBook-Air Helloworld % ./node_modules/.bin/truffle compile

Compiling your contracts…

Everything is up to date, there is nothing to compile.

aadyakalra@Aadyas-MacBook-Air Helloworld % ./node_modules/.bin/truffle console
truffle(ganache)> test
ExtendableError: Unknown network “ganache”. See your Truffle configuration file for available networks.
at Object.validateNetworkConfig (/Users/aadyakalra/Documents/Ethereum-course-advanced/Helloworld/node_modules/truffle/build/webpack:/packages/environment/environment.js:110:1)
at Object.detect (/Users/aadyakalra/Documents/Ethereum-course-advanced/Helloworld/node_modules/truffle/build/webpack:/packages/environment/environment.js:16:1)
at Object.run (/Users/aadyakalra/Documents/Ethereum-course-advanced/Helloworld/node_modules/truffle/build/webpack:/packages/core/lib/commands/test/index.js:142:1)
at Command.run (/Users/aadyakalra/Documents/Ethereum-course-advanced/Helloworld/node_modules/truffle/build/webpack:/packages/core/lib/command.js:136:1)
at Object. (/Users/aadyakalra/Documents/Ethereum-course-advanced/Helloworld/node_modules/truffle/build/webpack:/packages/core/lib/console-child.js:39:1)
at webpack_require (/Users/aadyakalra/Documents/Ethereum-course-advanced/Helloworld/node_modules/truffle/build/webpack:/webpack/bootstrap:19:1)
at /Users/aadyakalra/Documents/Ethereum-course-advanced/Helloworld/node_modules/truffle/build/webpack:/webpack/bootstrap:83:1
at Object. (/Users/aadyakalra/Documents/Ethereum-course-advanced/Helloworld/node_modules/truffle/build/consoleChild.bundled.js:89:10)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions…js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
truffle(ganache)>

Even the Migration of my Contracts is happening fine, don’t know why the test is saying "unknown network ganache"

and below is my Helloworldtest.js file

const Helloworld = artifacts.require("Helloworld");

contract("Helloworld", async function(){
  it("should initialize correctly", async function(){
    let instance = await Helloworld.deployed();
    let message = await instance.getMessage();
    assert(message === "Hello again !");
  });
});

Please help :pray:

Do you need me to share something else?

1 Like

Please share your truffle config and a screenshot of you ganache-gui

For those of you who have the “TypeError [ERR_INVALID_REPL_INPUT]” when trying to run the test…

I had this same issue and solved it. I am on Ubuntu. I tried to update to latest node version (15), to no avail. I read that it is suggested to revert to node version 10, which for me solved the issue.

Remediation steps:

npm cache clean -f
npm install --dev n
n 10

Then verify your node version, which should be 10.23:

node --version

Then run the Truffle console and the test:

truffle console
>test
1 Like

Hi @filip I am having trouble executing tests, I deployed the helloWorld contract correctly and test looks ok but when I run the test command I received following exception.

TypeError [ERR_INVALID_REPL_INPUT]: Listeners for uncaughtException cannot be used in the REPL
at process. (repl.js:307:15)
at process.emit (events.js:327:22)
at process.emit (/usr/local/lib/node_modules/truffle/build/webpack:/~/source-map-support/source-map-support.js:465:1)
at processEmit [as emit] (/usr/local/lib/node_modules/truffle/build/webpack:/~/signal-exit/index.js:155:1)
at _addListener (events.js:358:14)
at process.addListener (events.js:406:10)
at Runner.run (/usr/local/lib/node_modules/truffle/node_modules/mocha/lib/runner.js:868:11)
at Mocha.run (/usr/local/lib/node_modules/truffle/node_modules/mocha/lib/mocha.js:612:17)
at /usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/test.js:128:1
at new Promise ()
at Object.run (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/test.js:127:1)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
truffle(ganache)> check me
Current message Message - 14

Now in my previous post I mentioned that the Ganache-2.1.1-mac.dmg doesn’t work for Mac OS Catalina, so I installed the latest Ganache and started working with it. I see that I could deploy the truffle contract with the latest Ganache and the transactions are updated correctly.

1 Like

Hi @kHarsh

Follow this faq to fix the issue: FAQ - How to downgrade Node.Js

Hi Filip! Great classes!
Using the example from the video. We are testing if the message was really initialized.
What happened if the test is successful but inside the contract we have a constructor initializing to something different ?
I can assume that the message was successfully set as “Hello Again!” but then changed.
What kind of result should I expected from the test unit ?

What happened if the test is successful but inside the contract we have a constructor initializing to something different ?
I can assume that the message was successfully set as “Hello Again!” but then changed.
What kind of result should I expected from the test unit ?

The constructor method is called as soon as the contract is deployed.

If the constructor sets the message to “hello there!” but in your migration file you set it to “Hello again”, the final message will be “hello again”.

regards,
Dani

1 Like

I managed to get it working by downgrading node, but I couldn’t run the test in truffle console, it runs not in console mode, truffle test, does anyone know why?

Hi @mwonder

Can you clarify the issue?

but I couldn’t run the test in truffle console, it runs not in console mode, truffle test, does anyone know why?

Hi! I am having a problem with the “Introduction to Unit Testing”. Per the instructions in the video, I created the javascript file in the “Test” folder (using Filip’s code provided in the lesson). While in the truffle console, I then enter “test” and encounter the following error (see text below). Please help, as I can’t figure out what is going wrong. -Thanks!

TypeError [ERR_INVALID_REPL_INPUT]: Listeners for uncaughtException cannot be used in the REPL
at process. (repl.js:307:15)
at process.emit (events.js:327:22)
at process.emit (C:\Users\jsand\AppData\Roaming\npm\node_modules\truffle\build\webpack:~\source-map-support\source-map-support.js:465:1)
at processEmit [as emit] (C:\Users\jsand\AppData\Roaming\npm\node_modules\truffle\build\webpack:~\signal-exit\index.js:155:1)
at _addListener (events.js:358:14)
at process.addListener (events.js:406:10)
at Runner.run (C:\Users\jsand\AppData\Roaming\npm\node_modules\truffle\node_modules\mocha\lib\runner.js:868:11)
at Mocha.run (C:\Users\jsand\AppData\Roaming\npm\node_modules\truffle\node_modules\mocha\lib\mocha.js:612:17)
at C:\Users\jsand\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\test.js:128:1
at new Promise ()
at Object.run (C:\Users\jsand\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\test.js:127:1)
at runMicrotasks ()
at processTicksAndRejections (internal/process/task_queues.js:93:5)

Hi @CaliCrypto22

You can fix that issue by downgrading nodejs as explained in this faq: FAQ - How to downgrade Node.Js

@filip i ran into an error while am doing th

e test class please see below error screen dump

Hey @chim4us

Follow this FAQ to fix the issue: FAQ - How to downgrade Node.Js

1 Like

Thanks so much @dan-i it worked for me

1 Like

I could not get the version of JS 0.5.12 to work like Filip’s. I am using Solidity 0.5.8 for the project and it was working until I got this in cmder @filip

project

project2


project5
I updated the Node JS version here to v10.21.1 using nvm manager
project6
Here I fixed the problem. I had a different messge so of course it was failing

1 Like

I’m on a Mac, had the same problem as many others here, I followed the things other ppl did, what I ended up with, and it worked was:
uninstall node with <brew uninstall --ignore-dependencies node>
then I installed “Node 10.18 (this link)
then I closed the terminal, opened it again, ran “truffle console” to get into the console and then “test”.

1 Like

Thanks this one works for me :slight_smile:

1 Like

I was having the same issues as the rest of the forum with running test but it not working so I had to downgrade node.js.

When i use test, it runs now but it always gives me “0 passing”.

I’m not sure if this is problem with node or my code? I also saw there is a new Smart Contract 201 class. Should I just do that if this old version has compatibility issues?

Here is my code:

HelloWorld.js

pragma solidity 0.5.12;

contract HelloWorld {
  string message = "Hello World!";

  function getMessage() public view returns (string memory) {
    return message;
  }

  function setMessage(string memory _message) public payable {
    message = _message;
  }
}

2_HelloWorld_deploy.js

const HelloWorld = artifacts.require("HelloWorld");

module.exports = function(deployer, network, accounts){
  deployer.deploy(HelloWorld).then(function(instance){
    instance.setMessage("PROMISE", {value: 100000000000000000, from: accounts[0]}).then(function(){
        console.log("SET SUCCESS");
      }).catch(function(err){
        console.log("ERROR: " + err);
      });
  }).catch(function(err){
    console.log("Deloy failed: " + err);
  });
};

HelloWorldTest.js

const HelloWorld = artifacts.require("HelloWorld");

contract("HelloWorld", async function(){

  it("should initialize correctly", async function(){
    let instance = await HelloWorld.deployed();
    let message = await instance.getMessage();
    assert(message == "PROMISE");
  });
});

Thanks for the help!

Here is the terminal screenshot, not sure why I’m getting 0 passing when in the video it shows Filip gets 2 passing?