2017-01-20 35 views
7

升級後,摩卡甚至不能運行一個簡單的測試,這裏是爲什麼我得到「錯誤:分辨率方法超標」?

const assert = require('assert'); 

it('should complete this test', function (done) { 
    return new Promise(function (resolve) { 
    assert.ok(true); 
    resolve(); 
    }) 
    .then(done); 
}); 

我把這個代碼here

我瞭解,現在拋出一個異常Error: Resolution method is overspecified. Specify a callback * or * return a Promise; not both.

但如何代碼使其工作?我不明白。我有

node -v 6.9.4 

mocha -v 3.2.0 

如何在新的和正確的格式運行這段代碼現在?

回答

6

剛落
.then(done);function()

您在返回無極所以調用做是多餘的,因爲它在錯誤消息

在老版本中說你在的情況下使用回調取代function(done)這樣的異步方法

it ('returns async', function(done) { 
    callAsync() 
    .then(function(result) { 
     assert.ok(result); 
     done(); 
    }); 
}) 

現在,您可以選擇返回Prom ISE

it ('returns async', function() { 
    return new Promise(function (resolve) { 
    callAsync() 
     .then(function(result) { 
      assert.ok(result); 
      resolve(); 
     }); 
    }); 
}) 

但同時使用是誤導 (例如,見這裏https://github.com/mochajs/mocha/issues/2407

2

摩卡允許要麼使用一個回調:

it('should complete this test', function (done) { 
    new Promise(function (resolve) { 
    assert.ok(true); 
    resolve(); 
    }) 
    .then(done); 
}); 

返回一個承諾:

it('should complete this test', function() { 
    return new Promise(function (resolve) { 
    assert.ok(true); 
    resolve(); 
    }); 
}); 

你不能這樣做。

相關問題