For component and unit tests we are using Vitest. For a general overview on how to write these tests, see the Nuxt testing guide
There are some differences between our approach to testing and the official Nuxt recommendations:
This directory's structure should mirror the frontend directory structure as a whole. If the corresponding directory for the component you are testing does not exist in frontend/test
yet, please create one and add your new test there.
This render method has been customized to allow our major Nuxt plugins to work in the Vitest environment. If you are curious you can check render.ts and setup.ts in the frontend/test
directory to see how this works.
The Nuxt guide recommends a classic approach to testing where you mock every dependency in the component / function you are testing. This approach has some serious drawbacks: it can make it harder to refactor code and maintain tests over time.
We recommend using mocking for dependencies that are unreliable and/or slow, like network requests. You can find a more in-depth explanation on the pros and cons of mocking on Martin Fowler's blog.
Mocks we do recommend:
- We provide a global mock for
useColorMode
. This is necessary becauseuseColorMode
currently does not work in Vitest. See example use in useColorModeImages.spec.ts. - Nuxt provides a
registerEndpoint
function to mock network requests. See example use in sign-in.spec.ts.