Tests from the crypt
Terrifying true tales of Node.js test-driven-development
Β
Anna Gerber
@AnnaGerber
π 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 π
Β
@AnnaGerber
Tests from the crypt
By Anna Gerber
Tests from the crypt
- 955