Skip to content

Commit c9ba76d

Browse files
committed
Mock matchMedia
1 parent 5006fce commit c9ba76d

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

jest-setup.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@ import nock from 'nock';
44
if ( typeof window !== 'undefined' ) {
55
// The ipcListener global is usually defined in preload.ts
66
window.ipcListener = { subscribe: jest.fn() };
7+
8+
// Mock `matchMedia` as it's not implemented in JSDOM
9+
// Reference: https://jestjs.io/docs/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom
10+
Object.defineProperty( window, 'matchMedia', {
11+
writable: true,
12+
value: jest.fn().mockImplementation( ( query ) => ( {
13+
matches: false,
14+
media: query,
15+
onchange: null,
16+
addListener: jest.fn(), // deprecated
17+
removeListener: jest.fn(), // deprecated
18+
addEventListener: jest.fn(),
19+
removeEventListener: jest.fn(),
20+
dispatchEvent: jest.fn(),
21+
} ) ),
22+
} );
723
}
824

925
nock.disableNetConnect();

0 commit comments

Comments
 (0)