2016-08-30 44 views
3

如果承諾本身在'does fail'測試中被拒絕,則測試失敗,正如我所期望的那樣。如果斷言失敗/錯誤被拋出/承諾在承諾的「當時」被拒絕,我會記錄說ERROR: 'Unhandled promise rejection'並且測試通過。我如何讓它拒絕而不是記錄拒絕未處理?在斷言期間承諾被拒絕時,Karma通過測試

import { expect } from 'chai'; 

describe.only('What?',() => { 
    const e = new Error('NOPE'); 

    it('does fail',() => Promise.reject(e)); 

    it('should fail when rejected',() => { 
    const promise = new Promise(r => r()); 

    promise.then(() => Promise.reject(e)); 

    return promise; 
    }); 

    it('should fail when thrown, then caught then rejected',() => { 
    const promise = new Promise(r => r()); 

    promise 
     .then(() => { throw e; }) 
     .catch(() => Promise.reject('huh')); 

    return promise; 
    }); 

    it('should fail/reject when thrown, then caught then rethrown',() => { 
    const promise = new Promise(r => r()); 

    promise 
     .then(() => { throw e; }) 
     .catch(er => { throw er; }); 

    return promise; 
    }); 

    it(`doesn't matter if I expect`,() => { 
    const promise = new Promise(r => r()); 

    promise.then(() => { 
     expect(1).to.eq(2); 
    }); 

    return promise; 
    }); 
}); 

然後報告...

START: 
    What? 
    ✖ does fail 
    ✔ should fail when rejected 
ERROR: 'Unhandled promise rejection', Error{stack: undefined} 
    ✔ should fail when thrown, then caught then rejected 
ERROR: 'Unhandled promise rejection', 'huh' 
    ✔ should fail/reject when thrown, then caught then rethrown 
ERROR: 'Unhandled promise rejection', Error{stack: undefined, line: 47567, sourceURL: 'http://localhost:9876/base/test/test_index.js?7f696b0b50c0a51c7a2fa5278582072b20241a3b'} 
    ✔ doesn't matter if I expect 
ERROR: 'Unhandled promise rejection', AssertionError{message: 'expected 1 to equal 2', showDiff: true, actual: 1, expected: 2, stack: '[email protected]://localhost:9876/base/test/test_index.js?7f696b0b50c0a51c7a2fa5278582072b20241a3b:39222:25 
+0

我沒有看到done()的任何用法 – chchrist

+0

是從mocha或jasmine上面的代碼? – chchrist

+1

...和任何「期望」。 –

回答

3

你必須返回then(...)的返回值,而不是你創建的第一個應許。