Files
grafana/packages/grafana-plugin-configs/jest/jest-setup.js
T
Jack Westbrook b707cd28f1 Frontend: Move toEmit jest matchers to shared workspace (#108610)
* feat(test-utils): move the toEmitValue/s jest matchers to test-utils and add test script

* chore(jest): update configs to use matchers from test-utils package

* ci(frontend-tests): hook up packages tests

* fix(test-utils): re-export matchers from index.ts so packages that include setupTests don't error

* ci(pr-frontend-unit-tests): add frontend-packages-unit-tests to list of required unit tests

* Update packages/grafana-test-utils/README.md

Co-authored-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com>

---------

Co-authored-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com>
2025-07-30 14:39:44 +02:00

87 lines
1.8 KiB
JavaScript

import '@testing-library/jest-dom';
import { TextEncoder, TextDecoder } from 'util';
import { matchers } from '@grafana/test-utils/matchers';
expect.extend(matchers);
Object.assign(global, { TextDecoder, TextEncoder });
// https://jestjs.io/docs/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom
Object.defineProperty(global, 'matchMedia', {
writable: true,
value: (query) => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(), // deprecated
removeListener: jest.fn(), // deprecated
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
}),
});
const mockIntersectionObserver = jest.fn().mockImplementation((callback) => ({
observe: jest.fn().mockImplementation((elem) => {
callback([{ target: elem, isIntersecting: true }]);
}),
unobserve: jest.fn(),
disconnect: jest.fn(),
}));
global.IntersectionObserver = mockIntersectionObserver;
Object.defineProperty(document, 'fonts', {
value: { ready: Promise.resolve({}) },
});
// Used by useMeasure
global.ResizeObserver = class ResizeObserver {
static #observationEntry = {
contentRect: {
x: 1,
y: 2,
width: 500,
height: 500,
top: 100,
bottom: 0,
left: 100,
right: 0,
},
target: {
// Needed for react-virtual to work in tests
getAttribute: () => 1,
},
};
#isObserving = false;
#callback;
constructor(callback) {
this.#callback = callback;
}
#emitObservation() {
setTimeout(() => {
if (!this.#isObserving) {
return;
}
this.#callback([ResizeObserver.#observationEntry], this);
});
}
observe() {
this.#isObserving = true;
this.#emitObservation();
}
disconnect() {
this.#isObserving = false;
}
unobserve() {
this.#isObserving = false;
}
};