setupTests.jsx 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. const localStorageMock = {
  2. getItem: jest.fn(),
  3. setItem: jest.fn(),
  4. removeItem: jest.fn(),
  5. clear: jest.fn(),
  6. };
  7. global.localStorage = localStorageMock;
  8. Object.defineProperty(URL, 'createObjectURL', {
  9. writable: true,
  10. value: jest.fn(),
  11. });
  12. class Worker {
  13. constructor(stringUrl) {
  14. this.url = stringUrl;
  15. this.onmessage = () => {};
  16. }
  17. postMessage(msg) {
  18. this.onmessage(msg);
  19. }
  20. }
  21. window.Worker = Worker;
  22. /* eslint-disable global-require */
  23. if (typeof window !== 'undefined') {
  24. // ref: https://github.com/ant-design/ant-design/issues/18774
  25. if (!window.matchMedia) {
  26. Object.defineProperty(global.window, 'matchMedia', {
  27. writable: true,
  28. configurable: true,
  29. value: jest.fn(() => ({
  30. matches: false,
  31. addListener: jest.fn(),
  32. removeListener: jest.fn(),
  33. })),
  34. });
  35. }
  36. if (!window.matchMedia) {
  37. Object.defineProperty(global.window, 'matchMedia', {
  38. writable: true,
  39. configurable: true,
  40. value: jest.fn((query) => ({
  41. matches: query.includes('max-width'),
  42. addListener: jest.fn(),
  43. removeListener: jest.fn(),
  44. })),
  45. });
  46. }
  47. }
  48. const errorLog = console.error;
  49. Object.defineProperty(global.window.console, 'error', {
  50. writable: true,
  51. configurable: true,
  52. value: (...rest) => {
  53. const logStr = rest.join('');
  54. if (logStr.includes('Warning: An update to %s inside a test was not wrapped in act(...)')) {
  55. return;
  56. }
  57. errorLog(...rest);
  58. },
  59. });