diff --git a/packages/grafana-toolkit/src/config/jest.plugin.config.ts b/packages/grafana-toolkit/src/config/jest.plugin.config.ts index 86da5b70de6..f8a34d5b819 100644 --- a/packages/grafana-toolkit/src/config/jest.plugin.config.ts +++ b/packages/grafana-toolkit/src/config/jest.plugin.config.ts @@ -50,7 +50,7 @@ export const jestConfig = (baseDir: string = process.cwd()) => { const setupFile = getSetupFile(setupFilePath); const shimsFile = getSetupFile(shimsFilePath); - const setupFiles = [setupFile, shimsFile, 'jest-canvas-mock'].filter((f) => f); + const setupFiles = [setupFile, shimsFile, `${__dirname}/matchMedia.js`, 'jest-canvas-mock'].filter((f) => f); const defaultJestConfig = { preset: 'ts-jest', verbose: false, diff --git a/packages/grafana-toolkit/src/config/matchMedia.js b/packages/grafana-toolkit/src/config/matchMedia.js new file mode 100644 index 00000000000..f4489dec143 --- /dev/null +++ b/packages/grafana-toolkit/src/config/matchMedia.js @@ -0,0 +1,14 @@ +// https://jestjs.io/docs/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom +Object.defineProperty(global, 'matchMedia', { + writable: true, + value: jest.fn().mockImplementation((query) => ({ + matches: false, + media: query, + onchange: null, + addListener: jest.fn(), // deprecated + removeListener: jest.fn(), // deprecated + addEventListener: jest.fn(), + removeEventListener: jest.fn(), + dispatchEvent: jest.fn(), + })), +}); diff --git a/public/app/features/alerting/unified/AmRoutes.test.tsx b/public/app/features/alerting/unified/AmRoutes.test.tsx index 0f103cc2964..91a379a106e 100644 --- a/public/app/features/alerting/unified/AmRoutes.test.tsx +++ b/public/app/features/alerting/unified/AmRoutes.test.tsx @@ -15,20 +15,6 @@ import { DataSourceType, GRAFANA_RULES_SOURCE_NAME } from './utils/datasource'; import userEvent from '@testing-library/user-event'; import { selectOptionInTest } from '@grafana/ui'; -Object.defineProperty(window, 'matchMedia', { - writable: true, - value: jest.fn().mockImplementation((query) => ({ - matches: false, - media: query, - onchange: null, - addListener: jest.fn(), // deprecated - removeListener: jest.fn(), // deprecated - addEventListener: jest.fn(), - removeEventListener: jest.fn(), - dispatchEvent: jest.fn(), - })), -}); - jest.mock('./api/alertmanager'); jest.mock('./utils/config');