【发布时间】:2020-01-20 23:36:34
【问题描述】:
我正在使用 Mocha 模块编写一些 Solidity 测试。尽管调用了 done() 函数并且 promise 已解决(注释掉的 console.log() 语句表明包含的模块 compile.js 中的 Promise 确实已解决),但测试在下面失败并出现此错误。 也许我没有正确解释错误?我是 Node.js 的新手,如果我弄得一团糟,我深表歉意。
1) "before each" hook for "Deploy a contract":
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
const assert = require('assert');
const ganache = require('ganache-cli');
const Web3 = require('web3');
const web3 = new Web3(ganache.provider());
let accounts;
let inbox;
beforeEach(async (done) => {
// Get a list of all accounts
accounts = await web3.eth.getAccounts();
// console.log(accounts);
const generate = require('../compile');
await generate()
.then(async data => {
var interface = data.interface;
var bytecode = data.bytecode;
// console.log('ABI ' + interface);
// console.log('BIN ' + bytecode);
inbox = await new web3.eth.Contract(JSON.parse(interface))
.deploy({data: bytecode, arguments: ['Greetings!']})
.send({from: accounts[0], gas: '1000000'});
});
done();
});
describe('Inbox testing', () => {
it('Deploy a contract', () => {
console.log('Contract ' + inbox);
});
});
从compile.js导入的函数generate()返回promise
function generate() {
return new Promise((resolve, reject) => {
...
})
})
}
module.exports = generate;
【问题讨论】:
标签: node.js mocha.js async.js web3