0

I'm writing a small utility class to create an abstraction for addEventListener (JavaScript/DOM)

Let's say I have to test for these features:

  1. Single event target with a single event
  2. Multiple events
  3. Multiple event targets
  4. Delegated events
  5. Once

Now if I understand unit testing properly, I should create a test for each feature.

However, is there a need to test combined features? E.g.

  1. Multiple events + multiple targets
  2. Multiple event targets + delegated events
  3. Multiple event targets + delegated events + multiple events

Should I implement those combined tests in addition to single feature tests, or should I believe that I am fully covered by the single feature tests?

3
  • 1
    If a situation might conceivably introduce an error condition that wouldn't appear in simpler situations, then it deserves a unit test. I'm sure you've encountered bugs of that kind... Commented Oct 22, 2020 at 8:28
  • 1
    If those scenarios are plausible in runtime, then yes, It seems reasonable. Even if they aren't you might want to know what's the code behaviour once any of these cases happen, so you can foresee the strengths and the weakness of the code/design/solution. That's a good checkpoint for you to make better decisions in the future toward new features or possible enhancements. Anyways, if they don't contribute to code coverage, you can just disable them.
    – Laiv
    Commented Oct 23, 2020 at 11:09
  • Thanks, everyone for your input. I was just trying to make sure I'm not doing it wrong if and that doing a combined test of multiple features is still a "unit test". Commented Oct 25, 2020 at 17:19

2 Answers 2

1

To decide what makes most sense, it can be helpful to know about the inner code structure, and which coverage your tests will create (code and branch coverage).

  • If there are features which are completely independent of others, and their code is fully decoupled from each other, their is no need to write tests which combine the features in arbitrary ways

  • If there are features, however, where only a combination of them will lead to a full coverage, then you should add such tests.

If you don't have a coverage tool at hand, you can utilize a debugger to single-step through the code to find out which coverage a certain test will create. That will give you a certain feedback if your tests are sufficient. Using TDD, when you work at tests and production code in a quickly alternating fashion, you should usually know how precisely which tests will cause which paths through your code, you may often not even need a debugger then.

2

It depends

There is no strict rule, so choose what suits you best.

  1. In doubt, test : If the effort is not that big and reassure you then go for it and add the combinated test
  2. Rely on End to End tests : If each case is well tested independently, and the combination should work (e.g. by design) you can rely on E2E tests to cover the combined tests.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.