Skip to content

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

Merged

Conversation

sregg
Copy link
Contributor

@sregg sregg commented Sep 21, 2020

Implements #549

Summary

Add the value that was expected in error message for the following methods:

  • getByText('bla') => No instances found with text 'bla'
  • getByPlaceholderText('bla') => No instances found with placeholder text 'bla'
  • getByDisplayValue('bla') => No instances found with display value 'bla'
  • getByA11yLabel('bla') => No instances found with accessibilityLabel 'bla'
  • getByA11yHint('bla') => No instances found with accessibilityHint 'bla'
  • getByA11yRole('bla') => No instances found with accessibilityRole 'bla'
  • getByA11yStates('bla') => No instances found with accessibilityStates 'bla'
  • getByA11yState({ disabled: true}) => No instances found with accessibilityState '{"disabled":true}'
  • getByA11yValue({ min: 50 }) => No instances found with accessibilityValue '{"min":50}'

Test plan

I updated some tests to expect the new error message

Copy link
Member

@mdjastrzebski mdjastrzebski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work @sregg! 👍

I've added some suggestions for improving code readability.

Comment on lines 104 to 106
`${prepareErrorMessage(error)} with text '${
typeof text === 'string' ? text : text.toString()
}'`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`${prepareErrorMessage(error)} with text '${
typeof text === 'string' ? text : text.toString()
}'`,
`${prepareErrorMessage(error)} with text '${text}'`,

We should probably be able to the if condition, sa template string should do the checks & toString() call for us.

Copy link
Contributor Author

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:

image

Cannot coerce `text` to string because  `RegExp` [1] should not be coerced.

Copy link
Contributor Author

@sregg sregg Sep 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

${text.toString()}

seems to work
image

Would you be happy with that @mdjastrzebski?

Copy link
Member

@thymikee thymikee Sep 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sregg doing text.toString() or String(text) (generally safer) is the way to go here, yea 👍

Copy link
Member

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

Comment on lines 120 to 122
`${prepareErrorMessage(error)} with placeholder text '${
typeof placeholder === 'string' ? placeholder : placeholder.toString()
}'`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`${prepareErrorMessage(error)} with placeholder text '${
typeof placeholder === 'string' ? placeholder : placeholder.toString()
}'`,
`${prepareErrorMessage(error)} with placeholder '${placeholder}'`,

We should probably be able to the if condition, sa template string should do the checks & toString() call for us.

Comment on lines 136 to 140
`${prepareErrorMessage(error)} with display value '${
typeof displayValue === 'string'
? displayValue
: displayValue.toString()
}'`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`${prepareErrorMessage(error)} with display value '${
typeof displayValue === 'string'
? displayValue
: displayValue.toString()
}'`,
`${prepareErrorMessage(error)} with display value '${displayValue}'`,

We should probably be able to the if condition, sa template string should do the checks & toString() call for us.

Comment on lines 41 to 43
`${prepareErrorMessage(error)} with ${name} '${
typeof matcher === 'string' ? matcher : JSON.stringify(matcher) || ''
}'`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason for using JSON.stringify instead of toString like in other places?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm getting this error if I use toString()

image

Cannot call `matcher.toString` because property `toString` is missing in  mixed

throw new ErrorWithStack('No instances found', getAllBy);
throw new ErrorWithStack(
`No instances found with ${name} '${
typeof matcher === 'string' ? matcher : JSON.stringify(matcher) || ''
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have pretty-format in our deps, so let's use that with {min: true} option set: https://github.com/facebook/jest/tree/master/packages/pretty-format#usage-with-options

@sregg
Copy link
Contributor Author

sregg commented Sep 28, 2020

@thymikee I addressed your comments in my latest commit

Copy link
Member

@thymikee thymikee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@thymikee thymikee merged commit 484c9f7 into callstack:master Oct 12, 2020
@thymikee
Copy link
Member

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants