jest-setup.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // This import has side effects, and must be at the top so jQuery is made global before
  2. // angular is imported.
  3. import './global-jquery-shim';
  4. import Adapter from '@wojtekmaj/enzyme-adapter-react-17';
  5. import angular from 'angular';
  6. import { configure } from 'enzyme';
  7. import { EventBusSrv } from '@grafana/data';
  8. import 'mutationobserver-shim';
  9. import './mocks/workers';
  10. import '../vendor/flot/jquery.flot';
  11. import '../vendor/flot/jquery.flot.time';
  12. const testAppEvents = new EventBusSrv();
  13. const global = window as any;
  14. global.$ = global.jQuery = $;
  15. // https://jestjs.io/docs/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom
  16. Object.defineProperty(global, 'matchMedia', {
  17. writable: true,
  18. value: jest.fn().mockImplementation((query) => ({
  19. matches: false,
  20. media: query,
  21. onchange: null,
  22. addListener: jest.fn(), // deprecated
  23. removeListener: jest.fn(), // deprecated
  24. addEventListener: jest.fn(),
  25. removeEventListener: jest.fn(),
  26. dispatchEvent: jest.fn(),
  27. })),
  28. });
  29. angular.module('grafana', ['ngRoute']);
  30. angular.module('grafana.services', ['ngRoute', '$strap.directives']);
  31. angular.module('grafana.panels', []);
  32. angular.module('grafana.controllers', []);
  33. angular.module('grafana.directives', []);
  34. angular.module('grafana.filters', []);
  35. angular.module('grafana.routes', ['ngRoute']);
  36. jest.mock('../app/core/core', () => ({ appEvents: testAppEvents }));
  37. jest.mock('../app/angular/partials', () => ({}));
  38. jest.mock('../app/features/plugins/plugin_loader', () => ({}));
  39. configure({ adapter: new Adapter() });
  40. const localStorageMock = (() => {
  41. let store: any = {};
  42. return {
  43. getItem: (key: string) => {
  44. return store[key];
  45. },
  46. setItem: (key: string, value: any) => {
  47. store[key] = value.toString();
  48. },
  49. clear: () => {
  50. store = {};
  51. },
  52. removeItem: (key: string) => {
  53. delete store[key];
  54. },
  55. };
  56. })();
  57. global.localStorage = localStorageMock;
  58. const throwUnhandledRejections = () => {
  59. process.on('unhandledRejection', (err) => {
  60. throw err;
  61. });
  62. };
  63. throwUnhandledRejections();