【问题标题】:Mocha complains about done() even though it is used摩卡抱怨 done() 即使它被使用
【发布时间】: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


    【解决方案1】:

    我认为mocha 可能会发疯,因为您需要在运行测试后手动关闭web3 连接。在测试运行后尝试调用disconnect

    after(done => {
      web3.currentProvider.disconnect()
      done();
    }
    

    【讨论】:

    • 感谢您的建议。不幸的是,它只添加了另一个奇怪的错误“ TypeError: web3.currentProvider.disconnect is not a function at Context.done (test/inbox-test.js:48:27)”
    • ganache-core也许你可以试试web3.currentProvider.close()。如果它也不起作用,也许ganache.provider().close() ?? Lmk 怎么回事 :) PS:更新上面的代码调用done()hehe
    【解决方案2】:

    您不能在 Mocha 中将 done 回调与异步函数一起使用。此外,将异步函数传递给.then 也不是一个好主意。我会重构测试函数以仅使用异步样式代码。

    beforeEach(async () => {
      // Get a list of all accounts
      const accounts = await web3.eth.getAccounts();
      // console.log(accounts);
    
      const generate = require('../compile');
      const data = await generate();
      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'});
    });
    

    【讨论】:

    • 感谢您的解释。不幸的是,建议的重构并没有解决问题 - 我仍然收到相同的错误“错误:超过 2000 毫秒的超时。对于异步测试和钩子,请确保调用了“done()”;如果返回 Promise,请确保它已解决。 "
    • 你能找到解决办法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-02
    • 1970-01-01
    • 2020-06-15
    • 2011-08-09
    • 2014-09-17
    • 1970-01-01
    相关资源
    最近更新 更多