Testing your webapp
This post will refer to the Enzyme testing library which is specifically for React applications, but the principles are generally applicable to any app/service and doubtless there are equivalent testing libraries for other web frameworks.
What do I want from Tests?
Enable Refactoring by providing a safety net
Refactoring, as we all know, is where code changes, but behaviour does not. But, if I change the code and a test breaks, how can I know that the behaviour has not changed? I can’t. At least, the tests can’t tell me that behaviour has not changed. So what was the point of having them again?
All tests are coupled in some way to the actual code, but we’re going to look at the kind of tests that minimize this as far as possible, so breakages when refactoring point to real problems.
Provide a catalogue of Use Cases
Not all tests I write are ones I could guide end users through, but these are the ones I’ll prioritise writing and maintaining over time. That’s because after all the current devs and end users have moved on, these tests will be ones the developers can use to learn what the system is supposed to do, and can take to the users to say ‘Are all these things important/correct etc?’
There may be other ancillary benefits from having tests and there are certainly a lot of downsides of having tests but if I’m meeting the two criteria above, then as far as I’m concerned, having these tests will be worthwhile.
The Test Pyramid, Revisited
Testing nomenclature is a bit of a minefield as far as I’m concerned. Unit in Unit Test means different things to different people. The likes of J.B. Rainsbuger differntiates Integration Tests from Integrated Tests and on and on. Let’s
Class/Method/Function Level … some others in the middle … System Level
Integrated Tests are a scam?
Premise 1: With n layers in the system, you’d need 2n tests to cover every path. Premise 2: For any non-trivial system, 2n is far too large a number of tests to write and maintain so then inevitably you’ll miss out paths.
Conclusion: Integrated tests are a scam.
I think my main problem here is with the definition of layer. Rainsberger says he means basically any kind of function call (fill in) but in practice I often find myself building apps or services that have just one interesting layer.
< Some people call these micro-services and some people say the term micro-service is passe or fashionable and we should just call them services >
That one layer is what Gary Bernhardt would call the ‘functional core’ and does lend itself to fast and reliable testing, so surely tests should just use that?