-
Notifications
You must be signed in to change notification settings - Fork 273
feat: add the value expected in getBy error messages #550
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -100,7 +100,12 @@ export const getByText = (instance: ReactTestInstance) => | |||||||||||||
try { | ||||||||||||||
return instance.find((node) => getNodeByText(node, text)); | ||||||||||||||
} catch (error) { | ||||||||||||||
throw new ErrorWithStack(prepareErrorMessage(error), getByTextFn); | ||||||||||||||
throw new ErrorWithStack( | ||||||||||||||
`${prepareErrorMessage(error)} with text '${ | ||||||||||||||
typeof text === 'string' ? text : text.toString() | ||||||||||||||
}'`, | ||||||||||||||
getByTextFn | ||||||||||||||
); | ||||||||||||||
} | ||||||||||||||
}; | ||||||||||||||
|
||||||||||||||
|
@@ -112,20 +117,29 @@ export const getByPlaceholderText = (instance: ReactTestInstance) => | |||||||||||||
); | ||||||||||||||
} catch (error) { | ||||||||||||||
throw new ErrorWithStack( | ||||||||||||||
prepareErrorMessage(error), | ||||||||||||||
`${prepareErrorMessage(error)} with placeholder text '${ | ||||||||||||||
typeof placeholder === 'string' ? placeholder : placeholder.toString() | ||||||||||||||
}'`, | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
We should probably be able to the if condition, sa template string should do the checks & toString() call for us. |
||||||||||||||
getByPlaceholderTextFn | ||||||||||||||
); | ||||||||||||||
} | ||||||||||||||
}; | ||||||||||||||
|
||||||||||||||
export const getByDisplayValue = (instance: ReactTestInstance) => | ||||||||||||||
function getByDisplayValueFn(placeholder: string | RegExp) { | ||||||||||||||
function getByDisplayValueFn(displayValue: string | RegExp) { | ||||||||||||||
try { | ||||||||||||||
return instance.find((node) => | ||||||||||||||
getTextInputNodeByDisplayValue(node, placeholder) | ||||||||||||||
getTextInputNodeByDisplayValue(node, displayValue) | ||||||||||||||
); | ||||||||||||||
} catch (error) { | ||||||||||||||
throw new ErrorWithStack(prepareErrorMessage(error), getByDisplayValueFn); | ||||||||||||||
throw new ErrorWithStack( | ||||||||||||||
`${prepareErrorMessage(error)} with display value '${ | ||||||||||||||
typeof displayValue === 'string' | ||||||||||||||
? displayValue | ||||||||||||||
: displayValue.toString() | ||||||||||||||
}'`, | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
We should probably be able to the if condition, sa template string should do the checks & toString() call for us. |
||||||||||||||
getByDisplayValueFn | ||||||||||||||
); | ||||||||||||||
} | ||||||||||||||
}; | ||||||||||||||
|
||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,7 +37,12 @@ const makeQuery = <P: mixed, M: mixed>( | |
(node) => isNodeValid(node) && matcherFn(node.props[name], matcher) | ||
); | ||
} catch (error) { | ||
throw new ErrorWithStack(prepareErrorMessage(error), getBy); | ||
throw new ErrorWithStack( | ||
`${prepareErrorMessage(error)} with ${name} '${ | ||
typeof matcher === 'string' ? matcher : JSON.stringify(matcher) || '' | ||
}'`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the reason for using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
getBy | ||
); | ||
} | ||
}; | ||
|
||
|
@@ -47,7 +52,12 @@ const makeQuery = <P: mixed, M: mixed>( | |
); | ||
|
||
if (results.length === 0) { | ||
throw new ErrorWithStack('No instances found', getAllBy); | ||
throw new ErrorWithStack( | ||
`No instances found with ${name} '${ | ||
typeof matcher === 'string' ? matcher : JSON.stringify(matcher) || '' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we have |
||
}'`, | ||
getAllBy | ||
); | ||
} | ||
|
||
return results; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably be able to the if condition, sa template string should do the checks & toString() call for us.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was my first idea but I was getting this Flow error:
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems to work

Would you be happy with that @mdjastrzebski?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sregg doing
text.toString()
orString(text)
(generally safer) is the way to go here, yea 👍There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Btw, I think that
prepareErrorMessage
function is a perfect place for handling this extra text. Similar to what you have in a test here: https://github.com/callstack/react-native-testing-library/pull/550/files#diff-4246008ab708750218ff9e41118401a1R14