Introduction to Unit Testing

Hello
I have a problem with the code.
In the second part Building our First Test Part 2. The program does not work for me as intended. I don’t understand if the code is for payable or not payable functions

Works for me making not payable the function, but i cannot create a test that works for payable functions.
image

image

Hey @camarosan

The code does not work if you try to send eth to a non-payable function such as setMessage()

If you want to send eth to your function, it needs to be payable
function setMessage(string memory _msg) public payable {}

hi @dan-i

i copy the same code that Filip post and it does not work for the second test. with the payable function.
image

Updated
Hello Again
it is solved, thanks to a future video in the course :smile:
image
image

1 Like

Seems everyone is having the same kind of problems. Your reply was the most straight forward one. Step by step until fininsh line! Thanks m8!

When I run test in the console, I get the following error.

my test code is

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

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


I read in the Forum that downgrading NodeJS helps. Should I downgrade NodeJS?

How do I solve this error?

It is indeed a bug in node, follow this faq to fix it:
https://forum.ivanontech.com/t/faq-how-to-downgrade-node-js/22908/2

1 Like

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