当 promise 在断言期间被拒绝时,业力通过测试
Karma passes test when promise is rejected during assertion
如果在 'does fail'
测试中承诺本身被拒绝,那么测试将如我所料失败。如果断言 fails/error 是 thrown/promise 在承诺的 "then" 中被拒绝,我会记录说 ERROR: 'Unhandled promise rejection'
并且测试无论如何都会通过。我如何让它在拒绝时失败而不是记录拒绝未处理?
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: 'AssertionError@http://localhost:9876/base/test/test_index.js?7f696b0b50c0a51c7a2fa5278582072b20241a3b:39222:25
您必须 return then(...)
的 return 值,而不是您创建的第一个承诺。
如果在 'does fail'
测试中承诺本身被拒绝,那么测试将如我所料失败。如果断言 fails/error 是 thrown/promise 在承诺的 "then" 中被拒绝,我会记录说 ERROR: 'Unhandled promise rejection'
并且测试无论如何都会通过。我如何让它在拒绝时失败而不是记录拒绝未处理?
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: 'AssertionError@http://localhost:9876/base/test/test_index.js?7f696b0b50c0a51c7a2fa5278582072b20241a3b:39222:25
您必须 return then(...)
的 return 值,而不是您创建的第一个承诺。