Spy and stub everything!

When in doubt, the safest thing you can do when writing your tests is spy on everything and stub everything else. There are always going to be times that you'll want a function to execute naturally; in that case, leave it alone. Ultimately, you never want your tests to be dependent on any other system—that includes database servers, other network servers, other APIs, and so on. You only want to test that your own code works, nothing more. If your code is expected to make a call to an API, spy on the actual call and just assert that your code attempted to make the call. Likewise, fake the response from the server via a stub and ensure that your code handles the response properly.

The easiest way to check for dependencies in your code is to stop any other services from running (your local node app, and so on), as well as possibly even disabling your network connection. If your tests timeout or fail somewhere unexpectedly, it's likely because you missed a function you needed to spy on or stub along the way.

Don't get stuck going down a rabbit hole when writing your tests. It's easy to get carried away and start testing functionalities that should safely be assumed works. An example of this is writing tests to ensure a third-party module is performing correctly. If it's not a module you wrote, don't test it. Test that your code uses it properly, makes calls to it with the correct parameters, and handles any returns, but don't worry about writing tests to prove the module does what it says it should.

To learn more about TDD specifically when writing JavaScript, I would highly recommend Christian Johansen's beast of a book Test-Driven JavaScript Development. This book is huge and speaks to the sheer volume of information related to TDD. In some circles, TDD truly is a way of life and it will define the style with which you write your code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.237.115