how to assert error thrown with expect(fn).to.throw(Error) in Cypress #9407
Unanswered
MinhTran1997
asked this question in
Component Testing
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Scenario:
i have a input field and a button, both have "disabled" class and cannot be typed in or clicked. i want to check if whether they can be typed in, clicked or not. things would be easy if i just check if there is a "disabled" class in their html attr. but i want to do in a different way, whenever i run Cypress code that make a type in action or a click action
cy.get(input).type(....)
, error will be thrown when running test case. so i want to use expect(fn).to.throw(Error) assertion to catch that errorwhen i run cypress code, there will be 2 functions, one is to cy.get(input field).type('...') and one is to cy.get(button).click(). then i will check when these two functions being executed, expect the errors will be thrown after typing in or clicking, if true, the test case will pass.
Code is something like this:
cy.get(
input[id='input']).should('be.visible').then(() => { function typeName () { cy.get(
input[id='input']).type('some thing'); } expect(typeName).to.throw(Error); })
Current status:
test case fails, it seems like i the function
typeName()
doesn't run so there is no error to assert:assert: expected [Function: typeName ] to be a function (PASS)
assert: expected [Function: typeName ] to throw Error (FAIL)
many thanks.
Minh
Beta Was this translation helpful? Give feedback.
All reactions