2

I am using node v12.13.1, [email protected] and ubuntu 18.04 and running the test using documentation of async/await.

https://www.trufflesuite.com/docs/truffle/testing/writing-tests-in-javascript#using-async-await

This is my test code:

const DappTokenSale = artifacts.require("DappTokenSale");
const DappToken = artifacts.require("DappToken");


contract("DappToken", async accounts => {
    let dapptokenInstance = await DappToken.deployed();
    let dapptokenSale = await DappTokenSale.deployed();
    let admin = accounts[0]
    console.log(admin)
    let buyer = accounts[1]
    let tokenPrice = 1000000000000000; //wei (0.001 ether)
    // 3. Set the token available for sale
    let tokensAvailable = 750000;
    let numberOfTokens;
    it("Token buying", async() => {
        console.log(admin)
        let dapptokenInstance = await DappToken.deployed();
        console.log(dapptokenInstance.address, "hello")

    })

})

But there is no console.logs (e.g. console.log(admin)) when I run the tests.

enter image description here

6
  • try making changes to your contract. may be add a comment. If there is no change it will not recompile and hence redeploy.
    – Sanjay S B
    Jan 9, 2020 at 8:47
  • Its compiling, but there is no console.log, says 0 passing Jan 9, 2020 at 8:56
  • that is the tests result. what command are you entering?
    – Sanjay S B
    Jan 9, 2020 at 8:59
  • truffle test, also truffle test ./test/testfile.js. Both are giving the same results Jan 9, 2020 at 9:01
  • I see no obvious issue with your code. I tried with a local project and it works as expected. The problem might be somewhere else. Perhaps in the migration scripts, a bad truffle version, a dependency. Are you testing against ganache or a private blockchain? You might try forcing to recompile the contracts truffle compile --all.
    – Ismael
    Jan 10, 2020 at 20:08

2 Answers 2

2

please try this:

contract("DappToken", function(accounts) {
    it("Token buying", function() {
        return DappToken.deployed().then(function (instance) {
            console.log(instance, 'instance');
        });
    });
});

This is my example in other smart contract, where the tests are running smoothly:

contract("CarData", function(accounts) {
    //setting up the account[1] will lead to revert, because account[1] is not allowed to call the method
    it('should revert the transaction of insertCarData when the from parameter is not the owner of the contract', function() {
        return CarData.deployed()
        .then(function (instance) {
            return instance.insertCarData("BG1234566", 1529767983, 1529767983, 1529767983, "1994", "Bulgaria", "Red", {from: accounts[0]});
        });
    });
});

It might be possible that console.logs are not showing when running the tests. Please try to run some method of your smart contract.

1
  • 2
    Ya, this method working fine, I have already tested. But not the async/await as given in documentation Jan 9, 2020 at 9:07
0

This code works perfectly fine:

const DappTokenSale = artifacts.require("DappTokenSale")
const DappToken = artifacts.require("DappToken")

contract("DappToken", async accounts => {
  let admin
  let buyer
  let tokenPrice = 1000000000000000 //wei (0.001 ether)
  // 3. Set the token available for sale
  let tokensAvailable = 750000
  let numberOfTokens
  it("Token buying", async () => {
    admin = accounts[0]
    buyer = accounts[1]
    console.log(admin, "Admin")
    console.log(buyer, "buyer")

    let dapptokenInstance = await DappToken.deployed()
    let dapptokenSale = await DappTokenSale.deployed()
    console.log(dapptokenInstance.address, "Token address")
    console.log(dapptokenSale.address, "Token Sale address")
  })
})

Seems one shouldn't include await DappToken.deployed() within the main outer contract function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.