[Ethereum] Test using async/await in truffle not working

soliditytruffletruffle-test

I am using node v12.13.1, truffle@5.1.8 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

Best Answer

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.

Related Topic