A Unit test should test the behavior of a unit of work.
Unit tests are isolated and independent of each other.
Unit tests are lightweight tests:
- Repeatable
- Fast
- Consistent
- Easy to write and read
- Any given behavior should be specified in one and only one test
- The execution/order of execution of one test cannot affect the others
Ultimate Guidelines Which Helped Me a Lot.
Whenever possible, use TDD
Structure your tests properly
Name your tests properly
Don't comment out tests
Avoid logic in your tests
Don't write unnecessary expectations
Know your testing framework API
Consider using factory functions in the tests
Don't test multiple concerns in the same test
Cover the general case and the edge cases
Test the behavior, not the internal implementation
Don't mock everything
Create new tests for every defect
Test simple actions
Review test code first
When applying TDD, always start by writing the simplest failing test
When applying TDD, always make small steps in each test-first cycle
Properly set up the actions that apply to all the tests involved
Remember 👉 "Unit tests are code too"
Check out what Ben said in the below Image👇
Our unit test should be like this.
They should meet the same level of quality as the code being tested.
They can be refactored as well to make them more maintainable and/or readable.
Couple of awesome resources:
JS Unit Testing Good Practices and Horrible Mistakes
Unit Testing sucks (and it’s our fault)
Writing Great Unit Tests: Best and Worst Practices
Most Used Libraries:
Hope you like this one💙 Please share your feedback.
I recently started blogging on theankurtyagi.com where I create free content for the community. This article is from there, you should check it out.
If you’re a beginner to JavaScript and want to learn JavaScript e2e concepts, here’s a great course by Robin aka wellpaidgeek who is coding for 20+ years.
I highly recommend buying this course.
Here is the link if someone is interested.
You can follow me for more killer content on Twitter
If you need 1–1 mentorship on any issue about coding career or life or fitness as a coder then you can book a 60–90min time slot with me.
Check out my website for more info about how it works.
Code Life Fitness By Ankur Tyagi
If you liked my recommendations, then I encourage you to sign up for my weekly newsletter
Every Friday, I share a “2-1-1” newsletter with 2 tips on coding from my exp, 1 life relationship tip from my experience & 1 fitness tip.
Sign up below 👇