How do you write concise tests?

You need to have a clear vision of what exactly it is that you're testing. In that test you showed, the expectations indicate that the only thing the test checks is that the spawn is passed the correct argument values in command and options. This, in turn, seems to be, mostly about checking a couple of paths.

So, first thing: Use the appropriate type of test double you need. If you just need to check whether something has been called with the correct argument values, what you want is a Spy, not a Mock. The good thing is generally speaking, spies don't need to care about actually "doing things". So there's much less to build in the test.

Second thing: You're using a lot of external entities just to build the test conditions. I mean, you want to test "someUser/testService/start", and "someUser/testService/process". You know you want to test that. Bringing external facilities -and I could bet they are the same external facilities that your test subject uses- makes you lose control of the test conditions. The test should define explicitly the test conditions. So just hard-code those values. Hard-coding values to define test conditions may sound weird at first but is exactly what you need to do to keep your tests correct and precise.

And finally, it looks like you're actually doing the things! Don't do the things!! I mean, you're mocking some big things just to test the correct generation of a string, but then you're actually creating paths in the file-system, and running the process. You don't need to do all that just to test a string. That is exactly the idea of Test Doubles. You substitute the real entity for something that doesn't do whatever the real one does. Just simulates the precise thing you need it to do and nothing more.

/r/javascript Thread