1

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

  • 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 at 8:47
  • Its compiling, but there is no console.log, says 0 passing – user1566713 Jan 9 at 8:56
  • that is the tests result. what command are you entering? – Sanjay S B Jan 9 at 8:59
  • truffle test, also truffle test ./test/testfile.js. Both are giving the same results – user1566713 Jan 9 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 at 20:08
1

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.

| improve this answer | |
  • Ya, this method working fine, I have already tested. But not the async/await as given in documentation – user1566713 Jan 9 at 9:07
0

While writing tests using truffle or for that matter if you are using the mocha library, you have to place the it blocks inside a describe block.

contract("Name of a contract", accounts => {
  describe("What are you testing e.g. a function", () => {
   it("the function does something", async () => {
     \\ your assertions go here
   });
   it("the function does not do something", async () => {
     \\ your assertions go here
   });
  });

  describe("another function", () => {
   it("the function does something", async () => {
     \\ your assertions go here
   });
   it("the function does not do something", async () => {
     \\ your assertions go here
   });
  });
});

Your it blocks are directly in the contract block. May be this is the problem.

| improve this answer | |
  • No, not working, same results, anyways I am going with then statements. – user1566713 Jan 9 at 9:16
  • I have just added the pseudo code here. you can look at this github.com/sanjukammath/tic-tac-toe/blob/master/test/…. May be I missed something – Sanjay S B Jan 9 at 9:19
  • try adding the deploying statements in a beforeEach block. The link is to a training repo that I created. You can see extensive tests all written using async/await. – Sanjay S B Jan 9 at 9:20
  • Ya, I have added but the same results, in your code, describe should also contain async otherwise it will give a compilation error. It seems problem with truffle, if you keep another test that is built with .then statements, the test using async await works, but not when its run on isolation, or has a single test done with async/await – user1566713 Jan 9 at 9:22
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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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