Introduction to Unit Testing

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?

Hi @mervxxgotti

Your test runs fine in my computer.

Run:

  • truffle develop

Once the blockchain is ready

  • test

Regards,
Dani

1 Like

Tried running > truffle develop then > test

Still gets “0 passing”, is there something wrong with my assert function? or is this a version issue? even when I change the setMessage string so it doesn’t match the assert function and should fail, it still reports “0 passing” so “0 passing” shows regardless.

Even when I add in the code from the second part of the Unit Testing Introduction, I still get “0 passing” when I thought I should be getting “2 passing” and the it() messages?

Thanks for all the help!

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", "FAIL!");
  });
  it("should set the message correctly", async function(){
    let instance = await HelloWorld.deployed();
    await instance.setMessage("TESTTEST");
    let message = await instance.getMessage();
    assert(message === "TESTTEST", "FAIL2!");
  });
});

Hey @mervxxgotti

You see ‘0 passing’ because your tests are not running however I was able to copy/paste your code and run it.
There has to be something wrong somewhere else, push your code on GitHub and give me the link so that I can copy and test.

Thanks
Dani

It’s working now. While figuring out how to push to GitHub I noticed the test .js file was not actually in the test folder but I made it accidentally outside of it :sweat_smile:

Thanks so much for you patience!

Since it’s necessary anyway, does Ivan On Tech have any lessons on using GitHub or can you recommend me any good tutorials?

Thanks!

1 Like

I learned git with lots of practice and google :smiley:
It can seem annoying at the beginning but the more you practice, the more it will become clear.
For any question, YouTube is also full of Git tutorial.