Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

3.8 KB
Raw Blame
/* eslint-disable @typescript-eslint/explicit-function-return-type */
/* eslint-disable object-shorthand */
/* eslint-disable func-names */

/**
 * Adds custom matchers from the react testing library to all tests
 */
import '@testing-library/jest-dom';
import '@testing-library/jest-dom/extend-expect';
import 'jest-styled-components';

import { server } from './src/mocks-server/server';

import './src/styles.scss';
// Establish API mocking before all tests.

// Mock window.matchMedia
window.matchMedia =
	window.matchMedia ||
	function (): any {
		return {
			matches: false,
			addListener: function () {},
			removeListener: function () {},
		};
	};

if (!HTMLElement.prototype.scrollIntoView) {
	HTMLElement.prototype.scrollIntoView = function (): void {};
}

// jsdom doesn't implement the Pointer Capture API, which Radix UI primitives
// (e.g. @signozhq/ui Select) call when opening. Stub them so those components
// can be exercised in tests.
if (!HTMLElement.prototype.hasPointerCapture) {
	HTMLElement.prototype.hasPointerCapture = function (): boolean {
		return false;
	};
}
if (!HTMLElement.prototype.releasePointerCapture) {
	HTMLElement.prototype.releasePointerCapture = function (): void {};
}

// jsdom has no PointerEvent; Base UI Switch constructs one on click.
if (typeof window.PointerEvent === 'undefined') {
	class PointerEventMock extends MouseEvent {
		pointerId: number;
		pointerType: string;
		constructor(type: string, init: PointerEventInit = {}) {
			super(type, init);
			this.pointerId = init.pointerId ?? 0;
			this.pointerType = init.pointerType ?? '';
		}
	}
	(window as any).PointerEvent = PointerEventMock;
}

if (typeof window.IntersectionObserver === 'undefined') {
	class IntersectionObserverMock {
		observe(): void {}
		unobserve(): void {}
		disconnect(): void {}
		takeRecords(): IntersectionObserverEntry[] {
			return [];
		}
	}
	(window as any).IntersectionObserver = IntersectionObserverMock;
}

if (typeof window.ResizeObserver === 'undefined') {
	class ResizeObserverMock {
		observe(): void {}
		unobserve(): void {}
		disconnect(): void {}
	}
	(window as any).ResizeObserver = ResizeObserverMock;
}

if (typeof globalThis.DOMRect === 'undefined') {
	(globalThis as any).DOMRect = class DOMRect {
		x = 0;
		y = 0;
		width = 0;
		height = 0;
		top = 0;
		right = 0;
		bottom = 0;
		left = 0;
		constructor(x = 0, y = 0, width = 0, height = 0) {
			this.x = x;
			this.y = y;
			this.width = width;
			this.height = height;
			this.top = y;
			this.right = x + width;
			this.bottom = y + height;
			this.left = x;
		}
		toJSON(): any {
			return { x: this.x, y: this.y, width: this.width, height: this.height };
		}
		static fromRect(rect?: {
			x?: number;
			y?: number;
			width?: number;
			height?: number;
		}): DOMRect {
			return new DOMRect(rect?.x, rect?.y, rect?.width, rect?.height);
		}
	};
}

// Patch getComputedStyle to handle CSS parsing errors from @signozhq/* packages.
// These packages inject CSS at import time via style-inject / vite-plugin-css-injected-by-js.
// jsdom's nwsapi cannot parse some of the injected selectors (e.g. Tailwind's :animate-in),
// causing SyntaxErrors during getComputedStyle / getByRole calls.
const _origGetComputedStyle = window.getComputedStyle;
window.getComputedStyle = function (
	elt: Element,
	pseudoElt?: string | null,
): CSSStyleDeclaration {
	try {
		return _origGetComputedStyle.call(window, elt, pseudoElt);
	} catch {
		// Return a minimal CSSStyleDeclaration so callers (testing-library, Radix UI)
		// see the element as visible and without animations.
		return {
			display: '',
			visibility: '',
			opacity: '1',
			animationName: 'none',
			getPropertyValue: () => '',
		} as unknown as CSSStyleDeclaration;
	}
};

beforeAll(() => server.listen());

afterEach(() => server.resetHandlers());

afterAll(() => server.close());