-
-
Notifications
You must be signed in to change notification settings - Fork 7
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
Extend testing guidelines #38
Conversation
This PR proposes a new set of guidelines for writing unit tests across our projects. Most are new, or if they are followed to some degree, they have not yet been codified.
9e62285
to
7015953
Compare
guides/unit-testing.md
Outdated
}) | ||
``` | ||
|
||
## Use Jest's mock functions instead of Sinon |
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.
This is a corollary of "Use Jest", but I chose to call it out because we have a lot of older tests that were presumably written before the conversion to Jest and use Sinon.
guides/unit-testing.md
Outdated
}); | ||
``` | ||
|
||
## Use `it` to specify known behavior for a unit of code |
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.
Originally I had a section which explained the purpose of test names and why they're arranged using describe
and it
. Essentially, you can draw a straight line from Jest to the birth of behavior-driven development. However, BDD is a subject unto itself, and I didn't want to overload people with information.
guides/unit-testing.md
Outdated
|
||
Do not use "should" at the start of the test name. The official Jest documentation [omits this word from their examples](https://jestjs.io/docs/next/getting-started), and it creates noise when reviewing the list of tests that Jest outputs after a run. | ||
|
||
Do not repeat the name of the function or method in the name of the test if it is possible to communicate what a consumer expects more clearly. |
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's another bit of advice that could go here, which is to be aware of scenarios — changes in the context which affect the behavior of the thing under test — and call them out in the test name. That probably deserves its own guideline, however, and I've already written a lot 😅
guides/unit-testing.md
Outdated
2️⃣ | ||
|
||
``` typescript | ||
describe('if "addToken" is true', () => { |
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.
Nit: I think it would be better to encourage people to group tests by scenario, rather than by other similar factors. The description of this guideline kinda implies that already, but this example isn't as clear because this isn't written like a scenario.
Maybe a description like "when adding a token" would be better here? That might prompt people to think more about the purpose of the tests and the real scenarios they represent, rather than just on inputs and outputs.
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.
Good point. I've updated this guideline here: 1decf7b
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.
Great work! This is a really comprehensive guide.
There are a few high-level guidelines that I'd like to add at some point, but they can come later (e.g. what should you test, test coverage, refactor code-under-test to be more easily testable, etc.)
- Remove arguments for non-determinism - Make sub-guidelines more concise and hide details
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.
Another suggestion: Prefer it.each
over a for-loop inside the test.
@Mrtenz I'm going to be off for a while starting next week so I'm wondering if it makes sense to add this in a new PR? Plus this PR is pretty big 😅 |
I think I've addressed all the feedback above. I've made a few more tweaks along the way, so let me know if there's anything that doesn't make sense or could be simplified. |
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.
LGTM!
This PR proposes a new set of guidelines for writing unit tests across our projects. Most are new, or if they are followed to some degree, they have not yet been codified.
Fixes #25.
👉 View rendered document 👈