Implementing Test Driven Development in Svelte

  1. Write Failing Test
  2. Write functional codes that pass the test
  3. Improve the functional code
  1. Plan and describe the components needed.
  2. Plan the behavioral aspect of each component (whether it’s only going to be rendered or whether it needs to run a function, like buttons, etc.)
  3. For static component, I made a unit test to test whether the component is rendered in the DOM (that is not yet made, so the test would go red/fail).
  4. For components like buttons, where it’s intended to run a function when clicked, I made a simulation unit test with an input/initial value, and assert it with the value outputted after the function is being ran.
Unit test examples.
Testcase for HomeView.svelte
  • Less debugging time.
  • You’d only write the code that’s needed.
  • The code becomes easier to check.
  • It documents your code.
  • Easier to refactor.
  • Higher test coverage.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store