Skip to content
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

Merged
merged 28 commits into from
Sep 1, 2023
Merged

Extend testing guidelines #38

merged 28 commits into from
Sep 1, 2023

Conversation

mcmire
Copy link
Contributor

@mcmire mcmire commented Aug 15, 2023

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 👈

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.
@mcmire mcmire force-pushed the add-more-testing-guidelines branch from 9e62285 to 7015953 Compare August 15, 2023 23:15
})
```

## Use Jest's mock functions instead of Sinon
Copy link
Contributor Author

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 Show resolved Hide resolved
});
```

## Use `it` to specify known behavior for a unit of code
Copy link
Contributor Author

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 Show resolved Hide resolved

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.
Copy link
Contributor Author

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 Show resolved Hide resolved
2️⃣

``` typescript
describe('if "addToken" is true', () => {
Copy link
Member

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.

Copy link
Contributor Author

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

guides/unit-testing.md Outdated Show resolved Hide resolved
guides/unit-testing.md Outdated Show resolved Hide resolved
guides/unit-testing.md Outdated Show resolved Hide resolved
guides/unit-testing.md Outdated Show resolved Hide resolved
guides/unit-testing.md Outdated Show resolved Hide resolved
guides/unit-testing.md Outdated Show resolved Hide resolved
guides/unit-testing.md Outdated Show resolved Hide resolved
guides/unit-testing.md Outdated Show resolved Hide resolved
guides/unit-testing.md Outdated Show resolved Hide resolved
guides/unit-testing.md Outdated Show resolved Hide resolved
guides/unit-testing.md Outdated Show resolved Hide resolved
guides/unit-testing.md Outdated Show resolved Hide resolved
guides/unit-testing.md Outdated Show resolved Hide resolved
guides/unit-testing.md Outdated Show resolved Hide resolved
guides/unit-testing.md Outdated Show resolved Hide resolved
guides/unit-testing.md Outdated Show resolved Hide resolved
Copy link
Member

@Gudahtt Gudahtt left a 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.)

@mcmire mcmire requested a review from a team as a code owner August 31, 2023 16:54
docs/sdlc.md Outdated Show resolved Hide resolved
Copy link
Member

@Mrtenz Mrtenz left a 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.

docs/unit-testing.md Outdated Show resolved Hide resolved
@mcmire
Copy link
Contributor Author

mcmire commented Aug 31, 2023

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 😅

@mcmire
Copy link
Contributor Author

mcmire commented Aug 31, 2023

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.

Copy link
Member

@Gudahtt Gudahtt left a comment

Choose a reason for hiding this comment

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

LGTM!

@mcmire mcmire merged commit f601a8d into main Sep 1, 2023
3 checks passed
@mcmire mcmire deleted the add-more-testing-guidelines branch September 1, 2023 16:08
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.

Standards: Unit testing
3 participants