Tests from the crypt

Terrifying true tales of Node.js test-driven-development


Anna Gerber


πŸ‘Œ Tests should be

  • 😌 Fast
  • πŸ•΅οΈβ€β™€οΈ Clear
  • πŸ‘ Reliable
  • πŸ›  Maintainable

😰 Tests too slow? Benchmark!

  • Log start and end times
  • Run mocha with the --slow flag
  • Use ts:
           mocha test/testThings.js | ts '[%Y-%m-%d %H:%M:%.S]'

😌 Parallelize

😌 Don’t test third party / framework code

😌 Run tests selectively:

Filter on tags / keywords

mocha --grep billing

😨 Unbalanced

Too top-heavy (more E2E or integration tests than unit tests)


😱 Buried (in setup / teardown)

😌 Fixtures

  • Provide a baseline for data but also makes setup less transparent
  • Alternatively, speed up setup by:
    • Using in-memory or mocked db
    • Batching db writes

😱 Enigmatic tests

πŸ•΅οΈβ€β™€οΈ Clear descriptions

- pinpoint where things went wrong

  • Unique to each test
  • Include the component, scenario, and expected outcome
  • Easy to find - don't use templates or functions to generate descriptions!!!

πŸ•΅οΈβ€β™€οΈ Targeted assertions
- pinpoint what went wrong

  • Using chai assert - provide human readable messagesΒ 
  • Be specific - don't use deep equal assertions everywhere

πŸ•΅οΈβ€β™€οΈ Decipherable data

πŸ•΅οΈβ€β™€οΈ Avoid abstraction

  • Be explicit
  • Use helpers judiciously to hide boilerplate / setup / teardown
  • Avoid hiding test assertions in helpers

😨 Flickering tests

  • Race conditions in tests
  • Mocha scope issues

πŸ‘ Kill unreliable tests πŸ”ͺ

πŸ‘ Don't just test the happy path

πŸ‘ Test against contracts

πŸ›  Refactor your tests!

  • red / green / refactor applies to tests as well as code
  • periodically review / refactor all tests (not just new ones)
  • reliable tests are as valuable as reliable application code

πŸ›  Write standalone tests

πŸ›  Re-use appropriately

πŸ›  Mind your mocks

πŸ§˜β€β™‚οΈ Be mindful

  • up-front cost of writing the test
  • cost of running this test over time
  • cost of maintaining this test over time
  • balance against the benefit of the test

Tests from the crypt

Anna Gerber


PCCW Global - we are hiring 😊



Tests from the crypt

By Anna Gerber

Tests from the crypt

  • 703