Skip to content

Commit a11af54

Browse files
MonilBhavsarOSBotify
authored andcommitted
Merge pull request #52439 from Expensify/vit-unitTests
[NoQA] Update the checklists and proposal template to include requirement of unit tests (cherry picked from commit bb4c89d) (CP triggered by mountiny)
1 parent 5045dba commit a11af54

File tree

4 files changed

+39
-45
lines changed

4 files changed

+39
-45
lines changed

.github/PULL_REQUEST_TEMPLATE.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ This is a checklist for PR authors. Please make sure to complete all tasks and c
9494
- [ ] I followed the guidelines as stated in the [Review Guidelines](https://github.com/Expensify/App/blob/main/contributingGuides/PR_REVIEW_GUIDELINES.md)
9595
- [ ] I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like `Avatar`, I verified the components using `Avatar` are working as expected)
9696
- [ ] I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
97-
- [ ] I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
97+
- [ ] I verified any variables that can be defined as constants (ie. in CONST.ts or at the top of the file that uses the constant) are defined as such
9898
- [ ] I verified that if a function's arguments changed that all usages have also been updated correctly
9999
- [ ] If any new file was added I verified that:
100100
- [ ] The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
@@ -109,6 +109,7 @@ This is a checklist for PR authors. Please make sure to complete all tasks and c
109109
- [ ] I verified that all the inputs inside a form are aligned with each other.
110110
- [ ] I added `Design` label and/or tagged `@Expensify/design` so the design team can review the changes.
111111
- [ ] If a new page is added, I verified it's using the `ScrollView` component to make it scrollable when more elements are added to the page.
112+
- [ ] I added [unit tests](https://github.com/Expensify/App/blob/main/tests/README.md) for any new feature or bug fix in this PR to help automatically prevent regressions in this user flow.
112113
- [ ] If the `main` branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the `Test` steps.
113114

114115
### Screenshots/Videos

contributingGuides/PROPOSAL_TEMPLATE.md

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
### What changes do you think we should make in order to solve the problem?
88
<!-- DO NOT POST CODE DIFFS -->
99

10+
### What specific scenarios should we cover in unit tests to prevent reintroducing this issue in the future?
11+
<!-- Clearly describe the different test cases you recommend adding or updating. Explain how they will ensure the problem is fully covered and that any future changes do not cause a regression. Consider edge cases, input variations, and typical user interactions that could trigger this issue. To get guidance on how to write unit tests, refer to the README.md in the tests folder. -->
12+
1013
### What alternative solutions did you explore? (Optional)
1114

1215
**Reminder:** Please use plain English, be brief and avoid jargon. Feel free to use images, charts or pseudo-code if necessary. Do not post large multi-line diffs or write walls of text. Do not create PRs unless you have been hired for this job.

contributingGuides/REVIEWER_CHECKLIST.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
- [ ] I verified that this PR follows the guidelines as stated in the [Review Guidelines](https://github.com/Expensify/App/blob/main/contributingGuides/PR_REVIEW_GUIDELINES.md)
3131
- [ ] I verified other components that can be impacted by these changes have been tested, and I retested again (i.e. if the PR modifies a shared library or component like `Avatar`, I verified the components using `Avatar` have been tested & I retested again)
3232
- [ ] I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
33-
- [ ] I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
33+
- [ ] I verified any variables that can be defined as constants (ie. in CONST.ts or at the top of the file that uses the constant) are defined as such
3434
- [ ] If a new component is created I verified that:
3535
- [ ] A similar component doesn't exist in the codebase
3636
- [ ] All props are defined accurately and each prop has a `/** comment above it */`
@@ -54,6 +54,7 @@
5454
- [ ] I verified that all the inputs inside a form are aligned with each other.
5555
- [ ] I added `Design` label and/or tagged `@Expensify/design` so the design team can review the changes.
5656
- [ ] If a new page is added, I verified it's using the `ScrollView` component to make it scrollable when more elements are added to the page.
57+
- [ ] For any bug fix or new feature in this PR, I verified that sufficient [unit tests](https://github.com/Expensify/App/blob/main/tests/README.md) are included to prevent regressions in this flow.
5758
- [ ] If the `main` branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the `Test` steps.
5859
- [ ] I have checked off every checkbox in the PR reviewer checklist, including those that don't apply to this PR.
5960

tests/README.md

+32-43
Original file line numberDiff line numberDiff line change
@@ -59,62 +59,51 @@ expect(onyxData).toBe(expectedOnyxData);
5959

6060
## Documenting Tests
6161

62-
Tests aren't always clear about what exactly is being tested. To make this a bit easier we recommend adopting the following format for code comments:
62+
Comments are just as critical in tests as the tests themselves. They provide context behind why the test was written and what the expected behavior is supposed to be which will benefit the future generations of engineers looking at them. Think about it. When was the last time you saw a unit test and wondered why it was written that way and then you didn't want to touch it because you didn't know if changing the behavior of the test was appropriate or not? It was probably pretty recent :D
6363

64-
```
65-
// Given <initial_condition>
66-
... code that sets initial condition
64+
In order to give future engineers the best context for a unit test, follow this guide:
65+
66+
1. DO add three sections to every test:
67+
- "Given" - This introduces the initial condition of the test
68+
- "When" - Describes what action is being done and the thing that is being tested
69+
- "Then" - Describes what is expected to happen
6770

68-
// When <something_happens>
69-
... code that does something
71+
2. DO begin each comment section with the literal words "Given", "When", and "Then", just like the examples below.
72+
3. DO explain **WHY** the test is doing what it is doing in every comment.
73+
4. DO NOT explain **WHAT** the code is doing in comments. This information should be self-evident.
74+
75+
The format looks like this:
7076

71-
// Then <expectation>
72-
... code that performs the assertion
7377
```
78+
// BAD
79+
// Given an account
80+
{* code that sets initial condition *}
7481
75-
## Example Test
82+
// When it is closed
83+
{* code that does something *}
7684
77-
```javascript
78-
HttpUtils.xhr = jest.fn();
79-
80-
describe('actions/Report', () => {
81-
it('adds an optimistic comment', () => {
82-
// Given an Onyx subscription to a report's `reportActions`
83-
const ACTION_ID = 1;
84-
const REPORT_ID = 1;
85-
let reportActions;
86-
Onyx.connect({
87-
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`,
88-
callback: val => reportActions = val,
89-
});
90-
91-
// Mock Report_AddComment command so it can succeed
92-
HttpUtils.xhr.mockImplementation(() => Promise.resolve({
93-
jsonCode: 200,
94-
}));
95-
96-
// When we add a new action to that report
97-
Report.addComment(REPORT_ID, 'Hello!');
98-
return waitForBatchedUpdates()
99-
.then(() => {
100-
const action = reportActions[ACTION_ID];
101-
102-
// Then the action set in the Onyx callback should match
103-
// the comment we left and it will be in a loading state because
104-
// it's an "optimistic comment"
105-
expect(action.message[0].text).toBe('Hello!');
106-
expect(action.isPending).toBe(true);
107-
});
108-
});
109-
});
85+
// Then the user is logged out
86+
{* code that performs the assertion *}
87+
88+
// GOOD
89+
// Given an account of a brand new user
90+
{* code that sets initial condition *}
91+
92+
// When the account is closed by clicking on the close account button
93+
{* code that does something *}
94+
95+
// Then the user should be logged out because their account is no longer active
96+
{* code that performs the assertion *}
11097
```
11198

11299
## When to Write a Test
113100

114-
Many of the UI features of our application should go through rigorous testing by you, your PR reviewer, and finally QA before deployment. It's also difficult to maintain UI tests when the UI changes often. Therefore, it's not valuable for us to place every single part of the application UI under test at this time. The manual testing steps should catch most major UI bugs. Therefore, if we are writing any test there should be a **good reason**.
101+
Many of the UI features of our application should go through rigorous testing by you, your PR reviewer, and finally QA before deployment. However, the code is mature enough now that protecting code against regressions is the top priority.
115102

116103
**What's a "good reason" to write a test?**
117104

105+
- Any PR that fixes a bug
106+
- When introducing a new feature, cover as much logic as possible by unit tests
118107
- Anything that is difficult or impossible to run a manual tests on
119108
- e.g. a test to verify an outcome after an authentication token expires (which normally takes two hours)
120109
- Areas of the code that are changing often, breaking often, and would benefit from the resiliency an automated test would provide

0 commit comments

Comments
 (0)