Member-only story

The Ultimate Guide to Unit Testing in JavaScript.

Ankur Tyagi
2 min readOct 26, 2020

--

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

--

--

Ankur Tyagi
Ankur Tyagi

Written by Ankur Tyagi

Teaching Writing and Blogging To Developers. Newsletter: http://theankurtyagi.substack.com Digital Products: http://theankurtyagi.gumroad.com

No responses yet