2

I am trying to do a simple test on Truffle, but it seems that the deployed() promise is never being executed.

var MyContract = artifacts.require('MyContract')

contract('MyContract', function(accounts) {

    it('My Test', function () {
        MyContract.deployed().then(function (instance) {
            console.log('Deployed');
            assert.equal(1, 2, "This should fail");
        }).catch(function (e) {
            console.log(e);
            })
    })
})

When I do truffle.cmd test --network ganache the test just executes and doesn't log anything to the console. It seems that no code in the deployed() promise is being executed. If I do console.log(MyContract) I can see the contract, so it seems that it is being found by the Truffle test.

Any idea what could be wrong?

  • Do you have MyContract.deploy(<CONSTRUCTOR PARAMS>) somewhere in your migration scripts? MyContract.deployed() will return the instance deployed in the migration scripts. – ivicaa Jan 28 at 20:44
  • Yes i have that, and it is working correctly when i use the contract from in my app (not from the test) – user1365247 Jan 28 at 20:45
2

You have to return the promise:

...
it('My Test', function () {
    return MyContract.deployed()...
});
...

Alternatively, you can use async functions:

...
it('My Test', async function () {
    var myContract = await MyContract.deployed();
    console.log(myContract.address);
    ...
})
...
  • Don't know how I missed that first return! I was looking for a more complicated solution :) – user1365247 Jan 28 at 20:58
  • It's very easy to miss something in JS. ;-) – ivicaa Jan 28 at 21:00

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.