import util from 'node:util';
import '@testing-library/jest-dom';

import fetchMock from 'jest-fetch-mock';

global.fetch = fetchMock as unknown as typeof global.fetch;

Object.defineProperty(global, 'TextEncoder', {
  value: util.TextEncoder,
});

// https://github.com/esphen/jest-prop-type-error/blob/master/index.js
// This mock will make tests fail when props error occurs.
const { error, warn } = console;
// eslint-disable-next-line no-console
console.error = (message: string, ...args: unknown[]): void => {
  if (/(Invalid prop|Failed prop type)/i.test(message)) {
    throw new Error(message);
  }

  error.apply(console, [message, ...args]);
};

// eslint-disable-next-line no-console
console.warn = (message: string, ...args: unknown[]): void => {
  if (/(Call to useTheme outside a ThemeProvider)/i.test(message)) {
    return;
  }

  warn.apply(console, [message, ...args]);
};
