Return promise from assert inside 'then'

Return promise from assert inside 'then'

我需要使用 'then' 创建一系列 selenium 命令,但在这种情况下我不知道如何 return 来自断言的承诺。我不断收到此警告:

(node:18772) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): AssertionError [ERR_ASSERTION]: 'League of Legends' == 'Dota 2 on Reddit'

(node:18772) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我所说的例子:

var assert = require('assert');
var webdriver = require('selenium-webdriver');

it('should do something with promises', function(done) {
  this.timeout(300000);
  driver = new webdriver.Builder().
  withCapabilities(webdriver.Capabilities.firefox()).
  build();
  driver.get("https://www.reddit.com/r/leagueoflegends/").
  then( () => driver.getTitle()).
  then( (title) => assert.equal(title,"Dota 2 on Reddit")).
  then(() => driver.quit()).
  then(() => done());
});

你在某个地方遇到了错误,而且它没有在任何地方得到处理。一个快速简单的解决方法是添加

.catch(e => done(e))

到最后一个 .then(...) 结束。这将捕获来自任何 .then(...) 的任何错误,您可以适当地处理它。如果您调用 done() 并向其传递任何参数,例如 done(e),Mocha 会认为测试失败。如果失败,您总是可以像这样调用 assert.fail

.catch(e => assert.fail(e, 'expected value', 'Unknown description here'))