-
Notifications
You must be signed in to change notification settings - Fork 5
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
Refactor: use testing library on unit tests #13
Open
pedrorosarioo
wants to merge
5
commits into
sanar:master
Choose a base branch
from
pedrorosarioo:tests/use-testing-library
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9c36494
chore(deps): add @testing-library/react-native
5f735b8
chore(jest): extend expect with toHaveStyle from jest-native
e3d2963
test(highlight-text): improve test suites
d5f3668
refactor(test): use better selectors on repetitions test
pedrorosarioo 391efbd
fix(tests): declare getAllByText
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { toHaveStyle } from '@testing-library/jest-native'; | ||
|
||
expect.extend({ toHaveStyle }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Best to use
getBy
for check existence andqueryBy*
for check if is missingref: https://kentcdodds.com/blog/common-mistakes-with-react-testing-library#using-query-variants-for-anything-except-checking-for-non-existence
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.
Hmmm, doesn't make any sense to test if the value returned by a
getBy...
is truthy, since it throws an error when it is not (docs). The reason for an eventual failure would never be because the value is falsy, so we never have to check if a value returned by a get is truthy, that's why we don't have any example of it on docs, neither on Kent C. Dods article. That approach only applies to `queryBy', because it returns null when there is no matches, which is a falsy value... What is wanted here is to check the organization of the elements, thats why we are using queryBy, we don't want to make any manipulation over the element, we are not checking the content, only checking if the library is organizing the components as expected (i.e wrapping the searched words into a single element on DOM).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.
It's precisely because your test fails that you should use it, this way there is no chance for you to get a false positive. The
getBy*
queries should always return true, otherwise your test breaks.Anyway, it's indicated by the creator of the library. Perhaps he deserves some credit for that suggestion. 😅
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.
Maybe you are not thinking about the code. Why would I do a
getByText
without store or use it for nothing? Sorry it really didn't convince me to check a condition that can not be different, sounds so like an anti-pattern check if something that will alway return a truthy value is doing this 😅This is not about the creator, I really think he did and does such a good job, maybe you're just making a mistaken interpretation for this situation.
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.
Maybe if I rephrase my question it gets more clear: If the test will always fail before reach the verification (
toBeTruthy
) when it is not truthy, why would I need to add this verification?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.
Tests will not fail before reach the verification, will fail because it's impossible to check. The element that was expected to exists, don't exists.
You will check if the component that is expected to be rendered, was really rendered. Check if something that returns true , is really true is only the way to check. Could be checked if returns 1. Will be the same scenario, if not returns what is expected, should be treated as an error.
Imagine a screen where a person can update her data. Is expected to found a submit button, but for some reason, the button is not rendered. Is this an error?
How this scenario impacts user? Can she finishes the data update process?
For me, cases like that shoud be treated as an error and will be the same for the tests.
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.
Hmmm, I think you're wrong here. This way, the test WILL fail before reach the verification. The
get
methods already check if the value is truthy or falsy for us, so we don't need to check if the return of aget
is truthy again, if it didn't throw error, so it is already truthy. Do a quick test: write a suite to test if some element exists on the DOM. Note that if you useexpect(getByText('my-element-label').toBeTruthy()
, there is no way for this test to fail because it was expecting a truthy value then received a falsy value. It happens because when javascript try to evaluategetByText('my-element-label')
it will throw an error, and it occurs BEFORE the evaluation of the wholeexpect
, because javascript starts evaluating the leaf nodes (in this case, the inner function calls first). Did it make sense for you?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.
I got it!