2016-07-25 78 views
0

我不能在這個簡單的例子柴expect工作:摩卡的done功能似乎永遠不會被調用,並聲明會被忽略:摩卡和柴失敗insde一個承諾

import chai, {expect} from 'chai'; 
import chaiAsPromised from 'chai-as-promised'; 

chai.use(chaiAsPromised); 

import 'isomorphic-fetch'; 

describe('Mocha and Chai',() => { 
    it('tests chai expect inside a promise', (done) => { 
    fetch('http://google.com').then(() => { 
     const actual = 'test'; 
     const expected = 'expected'; 
     console.log(`'${actual}'' equals to '${expected}'?`, actual === expected); 
     expect(actual).to.be.equals(expected); 
     expect(actual).to.eventually.be.equals(expected); 
     done(); 
    }); 
    }); 
}); 

注意,我已經用盡全力,有和沒有chai-as-promisedeventually

這是相關的輸出:

> mocha tools/testSetup.js "src/**/*.spec.js" --reporter progress 

'test'' equals to 'expected'? false 

    1) Mocha and Chai tests chai expect inside a promise: 
    Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test. 

正如你所看到的,許是工作,我得到我的控制檯輸出。但是expect()調用都不起作用,done()調用也不做任何事情。

我不確定它是否相關,但我使用isomorphic-fetch和建議的es6-promise

+0

用'this.timeout(50000)嘗試''後,它()' – slorenzo

回答

1

我發現了這個問題。 done回調實際上並不需要。摩卡瞭解的情況,如果你返回it()塊內的承諾:

it('tests chai expect inside a promise',() => { 
    return fetch('http://google.com').then(() => { 
    const actual = 'test'; 
    const expected = 'expected'; 
    console.log(`'${actual}'' equals to '${expected}'?`, actual === expected); 
    expect(actual).to.be.equals(expected); 
    expect(actual).to.eventually.be.equals(expected);   
    }); 
}); 
+0

還有什麼困擾我的是,如果你錯過了返回承諾,對'expect()'的調用只會通過而不會產生影響,給出誤報。 –