【发布时间】:2019-10-10 23:28:09
【问题描述】:
ant 测试以下模块index.ts
async function theFunctionFail() {
return await fail();
}
async function theFunctionSucceed() {
return await succeed();
}
async function fail() {
throw new Error();
}
async function succeed() {
return "a";
}
export { theFunctionFail, theFunctionSucceed };
使用测试index.test.ts
import { theFunctionFail, theFunctionSucceed } from "../index";
it('theFunctionFail', () => {
expect(theFunctionFail()).rejects;
});
输出中的UnhandledPromiseRejectionWarning 是什么
(node:10515) UnhandledPromiseRejectionWarning: Error
(node:10515) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:10515) [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.
PASS src/__tests__/index.test.ts
✓ theFunctionFail (6ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 1.806s
Ran all test suites.
参考?将 expect(theFunctionFail()).rejects 包装在 try-catch(err) 块中并不能避免我认为值得修复的警告。
为什么测试没有失败?/我怎样才能让测试失败?如果我的测试发现了一个严重的缺陷,我的理解应该不会成功。
我正在使用带有react-scripts 的 Typescript 和 Jest 24.7.1。
【问题讨论】:
标签: javascript typescript unit-testing promise jestjs