Question presented to candidate: "We have no tests on our Vue 3 front end. Set up component testing with Vitest and Vue Test Utils, and show me how you would test a component with props and events, one that fetches data, and one that uses the router or a Pinia store."
What a strong answer should cover:
- Setup: Vitest with
@vitejs/plugin-vue(so.vuefiles compile) andenvironment: "jsdom"(or happy-dom); Vue Test Utils providesmount. - The core API:
mount(Component, { props, global }),get/findwithdata-testselectors,triggerandsetValue(both awaited, because the DOM updates on the next tick),text(),emitted(). - Async: mock the API module with
vi.mock, thenawait flushPromises()before asserting. - Plugins: pass a real router on
createMemoryHistoryor a Pinia viaglobal.plugins;createTestingPiniastubs actions and seeds state. - Test behaviour, not implementation: what the user sees and what the component emits, not internal refs; stub only heavy children (
global.stubs/shallowMount).
Clarifying questions expected:
- "Are we after unit-level component tests, or end-to-end tests in a real browser too?"
Code / implementation expected: Yes β a Vitest config and two or three tests covering events, async data and a plugin.