Here is my error codes:
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>
As you can see the error did indeed occurred: Function FunctionThatDoesNotExistsInString does not exists in string.
. However it is not captured as a pass in Jest.
Here is my codes:
test(`
expect(CheckFunctionExistenceByStr(
'any string', 'FunctionThatDoesNotExistsInString'
)).toThrow();
`, () => {
expect(CheckFunctionExistenceByStr(
'any string', 'FunctionThatDoesNotExistsInString'
)).toThrow();
}
);
In this case, the test will fail because the WebDriver could not find the element. In this case, StaleElementReferenceException will be thrown.
Testing try catch errors We have a mock function and we want to test whether it throws the error we are expecting. We can do this by simply passing the function to the expect without actually invoking it, and calling the toThrow method on it with the passed error.
expect(fn).toThrow()
expects a function fn
that, when called, throws an exception.
However you are calling CheckFunctionExistenceByStr
immediatelly, which causes the function to throw before running the assert.
Replace
test(`
expect(CheckFunctionExistenceByStr(
'any string', 'FunctionThatDoesNotExistsInString'
)).toThrow();
`, () => {
expect(CheckFunctionExistenceByStr(
'any string', 'FunctionThatDoesNotExistsInString'
)).toThrow();
}
);
with
test(`
expect(() => {
CheckFunctionExistenceByStr(
'any string', 'FunctionThatDoesNotExistsInString'
)
}).toThrow();
`, () => {
expect(() => {
CheckFunctionExistenceByStr(
'any string', 'FunctionThatDoesNotExistsInString'
)
}).toThrow();
}
);
Yeah, this is just Jest being weird. Instead of doing:
expect(youFunction(args)).toThrow(ErrorTypeOrErrorMessage)
Do this:
expect(() => youFunction(args)).toThrow(ErrorTypeOrErrorMessage)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With