I can't test events using Truffle 5.x. I get an error: The current provider doesn't support subscriptions: HttpProvider
I have the following test code in a truffle project to check if an event was emitted:
it('event is emitted', async function(done) {
let watcher = contract.Event(function(error, event) {
console.log(`Error: ${error}`)
console.log(`Event: ${event}`)
done();
});
await triggerEvent();
});
error should be undefined, event should contain an emitted event.
The code outputs the following output:
Error: Error: The current provider doesn't support subscriptions: HttpProvider
Event: undefined
$ truffle version
Truffle v5.0.1 (core: 5.0.1)
Solidity v0.5.0 (solc-js)
Node v11.1.0
$ npm --version
6.4.1
Yes, so HttpProvider doesn't support subscriptions. I'm not sure what you are specifying in your Truffle config... Check out the web3 provider docs: https://web3js.readthedocs.io/en/1.0/web3-eth.html#providers
One option you have if you are trying to test events is to look at the logs. When you execute a contract method, the returned object has a logs property. Inspect this and you can see what events were emitted.
@eggplantzzz My Truffle config is very straightforward:
module.exports = {
networks: {
ganache: {
host: "localhost",
port: 7545,
gas: 5000000,
network_id: "*"
}
}
};
It worked fine with testing events in Truffle 4.x, but does not work with Truffle 5.x. Do you know if anything was changed in relation to this?
I'll try to use the logs property instead.
Also @mushketyk, I'm not sure about the syntax you were using in 4...the web3 docs have a syntax like the following:
myContract.once("myEvent"[, options], callback)...
Check it out...
https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#once
@eggplantzzz myContract.on("myEvent" ... errors with myContract.on is not a function
Also if we enable websockets in the truffle config (cc @mushketyk) we instead get Error: connection not open. This error might be related to https://github.com/ethereum/web3.js/issues/1965 which was fixed in ~beta.36~ beta.38 of web3v1 and the latest truffle is one version behind that.
The latest Truffle uses Web3 version 1.0.0-beta.37. I think Ganache might be using 35 maybe?
I'll check out the websockets setting.
Sorry I meant this is fixed in web3-v1-beta.38. Updated my comment above.
any solution on that issue?
@emrem6 I think you should be able to use websockets with the latest Truffle (5.1.33). If you are still having a problem and can provide me with more detail I'll happily try and lend a hand.
You will need to supply a websockets provider and doctor up your network configuration a bit. See this for some extra info.