From 5622141d643190c5ac9947051603f58e2f00f861 Mon Sep 17 00:00:00 2001 From: Manuel Serret Date: Mon, 23 Dec 2024 09:41:41 +0100 Subject: [PATCH] add new code --- packages/addons/vitest-addon/index.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/addons/vitest-addon/index.ts b/packages/addons/vitest-addon/index.ts index c6dd095a..4ab55782 100644 --- a/packages/addons/vitest-addon/index.ts +++ b/packages/addons/vitest-addon/index.ts @@ -84,9 +84,26 @@ export default defineAddon({ if (content) return content; return dedent` - import '@testing-library/jest-dom/vitest' + import '@testing-library/jest-dom/vitest'; + import {vi} from 'vitest'; // add global mocks here, i.e. for sveltekit '$app/stores' + + // needed for svelte/motion that exports new MediaQuery which calls window.matchMedia eagerly + Object.defineProperty(window, "matchMedia", { + writable: true, + enumerable: true, + value: vi.fn().mockImplementation((query) => ({ + matches: false, + media: query, + onchange: null, + addListener: vi.fn(), // deprecated + removeListener: vi.fn(), // deprecated + addEventListener: vi.fn(), + removeEventListener: vi.fn(), + dispatchEvent: vi.fn(), + })), + }); `; });