goldbergyoni / goldbergyoni/javascript-testing-best-practices

How do you write testable code?

Open
#189 10 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
24.6k
Forks
2.1k
PR merge metrics
No merged PRs in 30d

Description

Not sure if that is out of the scope of this best practice guide. I, however, miss an integral part that explains how to write code that is easier to test in the first place.

That is only explained on the side in point [1.13 - other generic testing hygiene](https://github.com/goldbergyoni/javascript-testing-best-practices#-%EF%B8%8F113-other-generic-good-testing-hygiene). This could be split into two or three points that specificially focus on the do's and don'ts to enable testing and how to structure such code.

This is my subjective view or recommendation I would put into the list:

**Bad examples:**
* Using global state can make testing a nightmare, when two tests attempt to access the same state at the same time.
* Mutability grants loopholes for impossible state

**Good examples:**
* Extract pure data handling and decision making to their own side-effect free function to promote easier testing
* Make options and behavior configurable (using dependency injection/rejection in function parameters/class constructor) to allow unit-testing.

In my experience, that goes hand in hand with benefits like more readable code, configurable behavior and options, focused functions. Some recommendation how to structure code or functions in a way that enables testing in the first place would be great.

What do you think?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.