在mocha测试中使用Promise函数 可以在function前使用async关键字 const initFn = () => { return new Promise(resolve => { message.init().then(()=>resolve()); }) } describe('message test', async function () { await initFn(); it('promise test', function (done) { yourpromisefunction().then(response => { console.log(response); done(); }) .catch(err => { console.log("error:",err) done(err); }) .catch(done); }) run(); } 运行mocha时要添加–delay参数,测试代码中需要添加run(),不然函数运行完不进行接下来的测试。 https://mochajs.org/#delayed-root-suite mocha --delay --recursive ./test/unit/*.test.js 一些mocha的例子: https://gist.github.com/haroldtreen/5f1055eee5fcf01da3e0e15b8ec86bf6 如果需要在整个测试前运行可以参考下方链接: https://stackoverflow.com/a/41874640……

阅读全文