@@ -87,30 +87,29 @@ export default defineAddon({
87
87
import '@testing-library/jest-dom/vitest';
88
88
import { vi } from 'vitest';
89
89
90
- // add global mocks here, i.e. for sveltekit '$app/state'
91
-
92
- // needed for svelte/motion that exports new MediaQuery which calls window. matchMedia eagerly
93
- Object.defineProperty(window, "matchMedia", {
94
- writable : true,
95
- enumerable: true,
96
- value: vi.fn().mockImplementation((query) => ({
97
- matches: false ,
98
- media: query ,
99
- onchange: null ,
100
- addListener : vi.fn(), // deprecated
101
- removeListener : vi.fn(), // deprecated
102
- addEventListener: vi.fn( ),
103
- removeEventListener: vi.fn(),
104
- dispatchEvent: vi.fn(),
105
- })),
106
- });
90
+ // required for svelte5 + jsdom as jsdom does not support matchMedia
91
+ if(typeof window != undefined && !window.matchMedia) {
92
+ Object.defineProperty(window, ' matchMedia', {
93
+ writable: true,
94
+ enumerable : true,
95
+ value: vi.fn().mockImplementation(query => ({
96
+ matches: false,
97
+ media: query ,
98
+ onchange: null ,
99
+ addEventListener: vi.fn() ,
100
+ removeEventListener : vi.fn(),
101
+ dispatchEvent : vi.fn(),
102
+ }) ),
103
+ })
104
+ }
105
+
106
+ // add more mocks here if you need them
107
107
` ;
108
108
} ) ;
109
109
110
- sv . file ( `vitest.workspace .${ ext } ` , ( content ) => {
110
+ sv . file ( `vite.config .${ ext } ` , ( content ) => {
111
111
const { ast, generateCode } = parseScript ( content ) ;
112
112
113
- imports . addNamed ( ast , 'vitest/config' , { defineWorkspace : 'defineWorkspace' } ) ;
114
113
imports . addNamed ( ast , '@testing-library/svelte/vite' , { svelteTesting : 'svelteTesting' } ) ;
115
114
116
115
const clientObjectExpression = object . create ( {
@@ -135,13 +134,16 @@ export default defineAddon({
135
134
} )
136
135
} ) ;
137
136
138
- const defineWorkspaceFallback = functions . call ( 'defineWorkspace ' , [ ] ) ;
139
- const { value : defineWorkspaceCall } = exports . defaultExport ( ast , defineWorkspaceFallback ) ;
137
+ const defineConfigFallback = functions . call ( 'defineConfig ' , [ ] ) ;
138
+ const { value : defineWorkspaceCall } = exports . defaultExport ( ast , defineConfigFallback ) ;
140
139
if ( defineWorkspaceCall . type !== 'CallExpression' ) {
141
- log . warn ( 'Unexpected vite config for vitest add-on . Could not update.' ) ;
140
+ log . warn ( 'Unexpected vite config. Could not update.' ) ;
142
141
}
143
142
144
- const workspaceArray = functions . argumentByIndex ( defineWorkspaceCall , 0 , array . createEmpty ( ) ) ;
143
+ const vitestConfig = functions . argumentByIndex ( defineWorkspaceCall , 0 , object . createEmpty ( ) ) ;
144
+ const testObject = object . property ( vitestConfig , 'test' , object . createEmpty ( ) ) ;
145
+
146
+ const workspaceArray = object . property ( testObject , 'workspace' , array . createEmpty ( ) ) ;
145
147
array . push ( workspaceArray , clientObjectExpression ) ;
146
148
array . push ( workspaceArray , serverObjectExpression ) ;
147
149
0 commit comments