not*_*eek 12 javascript unit-testing ecmascript-6 jestjs
这是我的错误代码:
FAIL build/__test__/FuncOps.CheckFunctionExistenceByString.test.js
?
expect(CheckFunctionExistenceByStr(
'any string', 'FunctionThatDoesNotExistsInString'
)).toThrow();
Function FunctionThatDoesNotExistsInString does not exists in string.
at CheckFunctionExistenceByStr (build/FuncOps.js:35:15)
at Object.<anonymous> (build/__test__/FuncOps.CheckFunctionExistenceByString.test.js:12:51)
at new Promise (<anonymous>)
at <anonymous>
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,确实发生了错误:Function FunctionThatDoesNotExistsInString does not exists in string..然而,它并没有被捕获为Jest中的传球.
这是我的代码:
test(`
expect(CheckFunctionExistenceByStr(
'any string', 'FunctionThatDoesNotExistsInString'
)).toThrow();
`, () => {
expect(CheckFunctionExistenceByStr(
'any string', 'FunctionThatDoesNotExistsInString'
)).toThrow();
}
);
Run Code Online (Sandbox Code Playgroud)
小智 17
expect(fn).toThrow()需要一个功能fn是,调用时,抛出异常.
但是,您正在调用CheckFunctionExistenceByStrimmediatelly,这会导致函数在运行assert之前抛出.
更换
test(`
expect(CheckFunctionExistenceByStr(
'any string', 'FunctionThatDoesNotExistsInString'
)).toThrow();
`, () => {
expect(CheckFunctionExistenceByStr(
'any string', 'FunctionThatDoesNotExistsInString'
)).toThrow();
}
);
Run Code Online (Sandbox Code Playgroud)
同
test(`
expect(() => {
CheckFunctionExistenceByStr(
'any string', 'FunctionThatDoesNotExistsInString'
)
}).toThrow();
`, () => {
expect(() => {
CheckFunctionExistenceByStr(
'any string', 'FunctionThatDoesNotExistsInString'
)
}).toThrow();
}
);
Run Code Online (Sandbox Code Playgroud)
是的,这只是 Jest 很奇怪。而不是做:
expect(youFunction(args)).toThrow(ErrorTypeOrErrorMessage)
Run Code Online (Sandbox Code Playgroud)
做这个:
expect(() => youFunction(args)).toThrow(ErrorTypeOrErrorMessage)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2508 次 |
| 最近记录: |