Summary
This chapter has taken a very detailed look at component mocks. We started by looking at how it’s often possible to avoid using component mocks in the majority of cases, which is important because component mocks are a big cause of complexity in test suites.
You then saw how to use hand-rolled mocks that take advantage of Vitest’s vi.mock
function together with component stubs in the specially named __mocks__
directory. You also saw how they can quickly become complex.
Finally, we looked at using the svelte-component-double
library to avoid using hand-rolled mocks. This provides a couple of simple matchers together with some helper functions to assist you in writing tests.
That completes all the unit testing topics within this book. The next chapter adds one more testing technique: using Cucumber.js to write Behavior-Driven Development (BDD) style tests for your team.