|
| 1 | +--- |
| 2 | +title: Vitest |
| 3 | +description: How to test with vitest |
| 4 | +section: Testing |
| 5 | +--- |
| 6 | + |
| 7 | +When testing components that use mode-watcher, you'll need to set up a proper testing environment that mocks the `matchMedia` API. This guide will show you how to properly configure your tests. |
| 8 | + |
| 9 | +## Setup |
| 10 | + |
| 11 | +Create a `vitest.setup.ts` file in your project root (or wherever you keep your test configuration) and add the following code: |
| 12 | + |
| 13 | +```ts |
| 14 | +import { vi } from "vitest"; |
| 15 | + |
| 16 | +const mockMatchMedia = vi.fn().mockImplementation((query) => ({ |
| 17 | + matches: false, |
| 18 | + media: query, |
| 19 | + onchange: null, |
| 20 | + addEventListener: vi.fn(), |
| 21 | + removeEventListener: vi.fn(), |
| 22 | + dispatchEvent: vi.fn(), |
| 23 | + // Additional properties to better match the MediaQueryList interface |
| 24 | + matchMedia: true, |
| 25 | + mediaQueryList: true, |
| 26 | + // Method to simulate media query changes |
| 27 | + simulateChange: (matches: boolean) => { |
| 28 | + mockMatchMedia.mock.results[0].value.matches = matches; |
| 29 | + if (mockMatchMedia.mock.results[0].value.onchange) { |
| 30 | + mockMatchMedia.mock.results[0].value.onchange(); |
| 31 | + } |
| 32 | + }, |
| 33 | +})); |
| 34 | + |
| 35 | +Object.defineProperty(window, "matchMedia", { |
| 36 | + writable: true, |
| 37 | + value: mockMatchMedia, |
| 38 | +}); |
| 39 | +``` |
| 40 | + |
| 41 | +Then, update your `vitest.config.ts` to include this setup file: |
| 42 | + |
| 43 | +```ts |
| 44 | +import { defineConfig } from "vitest/config"; |
| 45 | + |
| 46 | +export default defineConfig({ |
| 47 | + test: { |
| 48 | + setupFiles: ["./vitest.setup.ts"], |
| 49 | + // ... other config options |
| 50 | + }, |
| 51 | +}); |
| 52 | +``` |
| 53 | + |
| 54 | +## Usage in Tests |
| 55 | + |
| 56 | +With the setup in place, you can now write tests for components that use mode-watcher. Here's an example: |
| 57 | + |
| 58 | +```ts |
| 59 | +import { describe, it, expect } from "vitest"; |
| 60 | +import { render } from "@testing-library/svelte"; |
| 61 | +import YourComponent from "./YourComponent.svelte"; |
| 62 | + |
| 63 | +describe("YourComponent", () => { |
| 64 | + it("should render in light mode by default", () => { |
| 65 | + const { container } = render(YourComponent); |
| 66 | + // Your assertions here |
| 67 | + }); |
| 68 | +}); |
| 69 | +``` |
| 70 | + |
| 71 | +## Important Notes |
| 72 | + |
| 73 | +1. The mock implementation must be set up before any components are rendered that use mode-watcher. |
| 74 | +2. The `simulateChange` method allows you to programmatically trigger media query changes in your tests. |
| 75 | +3. Make sure to clean up any event listeners in your `afterEach` or `afterAll` blocks if needed. |
| 76 | + |
| 77 | +## Troubleshooting |
| 78 | + |
| 79 | +If you encounter issues with the `matchMedia` mock not working as expected: |
| 80 | + |
| 81 | +1. Verify that your `vitest.setup.ts` file is properly configured in your `vitest.config.ts` |
| 82 | +2. Ensure that the mock is set up before any components are rendered |
| 83 | +3. Check that you're using the latest version of mode-watcher, as the implementation details may change between versions |
0 commit comments