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.
Unit test examples #17
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
base: master
Are you sure you want to change the base?
Unit test examples #17
Changes from all commits
982a2c0
fd9934f
1935a7a
d5d055d
e234e2b
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
There is no benefit to instantiating this schema each time the method is called, it can just be done once above the function
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.
if (!isValid)
seems more natural here - it is functionally the same, but more correctly matches the reasoning, sincestatusCode === 400
is just a consequence of the result being invalidThere 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.
While running the tests, this would look like:
When writing tests, it's often not as good to DRY the code as it is with source code. The worst perpetrator usually are the generic setup functions that are extracted just because a few lines are often used together, but it also applies in the general separation of test cases (drying with
describe
). The reason for that is the importance of test case readability in human language. That's why some frameworks like ava.js completely eliminatedescribe
- and while I don't agree with the full elimination, I do think it should be used sparingly. E.g.Here, every test stands as a single readable test, without having to remember the
describe
context. It isn't a problem here, but with more tests that becomes a bigger issue.Another reason to not use this is that
describe
is often very useful for writing conditional subcases of the main case. This is usually a more adaptable approach, compared to "success vs. fail" which always just splits into two. And using those within every conditional subcase ends up with a pyramid of doom very fast. E.g. imagine introducing the success and fail within all of the following:The above is an example of integration tests, and using
describe.each
would help there, but the point about success and fail cases as additional describes still stands.Another change I did was the removal of explicit usage of test data within the test name - it isn't important for the reader to know the exact string tested, what's important is knowing that the password is valid. The rules for password being valid are many, so even putting a specific string doesn't give much info and might be misleading since it could be considered a special case.
I've also reworded
successfully validates
toreturn true/false
since 1. password is validated successfully in both cases, it's just that the result of that validation is different 2. returning a boolean is actually what we expect in this case, so it is correct to state that - it gives us useful info, since another possible implementation of this function would've been to throw an error if the password is invalid.Lastly, I've started every test with
should
- this is not a rule and there are probably differing opinions about this, but using a word like that does help to point people in a consistent direction for the wording of each test.Not sure if what I wrote here is also something to mention in the antipattern section, since the code does not seem wrong per se, it just seems like it can provide more information if structured differently, at least based on my experience.
@bubafinder I know you already approved, but would like to hear your opinion as well.