setup.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* global __dirname */
  2. import jQuery from 'jquery';
  3. import Adapter from 'enzyme-adapter-react-16';
  4. import Enzyme from 'enzyme'; // eslint-disable-line no-restricted-imports
  5. import MockDate from 'mockdate';
  6. import PropTypes from 'prop-types';
  7. import fromEntries from 'object.fromentries';
  8. import ConfigStore from 'app/stores/configStore';
  9. import {loadFixtures} from './sentry-test/loadFixtures';
  10. export * from './sentry-test/select';
  11. // We need this polyfill for testing only because
  12. // typescript handles it for main application
  13. fromEntries.shim();
  14. /**
  15. * Enzyme configuration
  16. */
  17. Enzyme.configure({adapter: new Adapter()});
  18. /**
  19. * Mock (current) date to always be National Pasta Day
  20. * 2017-10-17T02:41:20.000Z
  21. */
  22. const constantDate = new Date(1508208080000);
  23. MockDate.set(constantDate);
  24. /**
  25. * Load all files in `tests/js/fixtures/*` as a module.
  26. * These will then be added to the `TestStubs` global below
  27. */
  28. const fixturesPath = `${__dirname}/sentry-test/fixtures`;
  29. const fixtures = loadFixtures(fixturesPath);
  30. /**
  31. * Global testing configuration
  32. */
  33. ConfigStore.loadInitialData({
  34. messages: [],
  35. user: fixtures.User(),
  36. });
  37. /**
  38. * Mocks
  39. */
  40. jest.mock('lodash/debounce', () => jest.fn(fn => fn));
  41. jest.mock('app/utils/recreateRoute');
  42. jest.mock('app/translations');
  43. jest.mock('app/api');
  44. jest.mock('app/utils/domId');
  45. jest.mock('app/utils/withOrganization');
  46. jest.mock('scroll-to-element', () => jest.fn());
  47. jest.mock('react-router', () => {
  48. const ReactRouter = jest.requireActual('react-router');
  49. return {
  50. IndexRedirect: ReactRouter.IndexRedirect,
  51. IndexRoute: ReactRouter.IndexRoute,
  52. Link: ReactRouter.Link,
  53. Redirect: ReactRouter.Redirect,
  54. Route: ReactRouter.Route,
  55. withRouter: ReactRouter.withRouter,
  56. browserHistory: {
  57. goBack: jest.fn(),
  58. push: jest.fn(),
  59. replace: jest.fn(),
  60. listen: jest.fn(() => {}),
  61. },
  62. };
  63. });
  64. jest.mock('react-lazyload', () => {
  65. const LazyLoadMock = ({children}) => children;
  66. return LazyLoadMock;
  67. });
  68. jest.mock('react-virtualized', () => {
  69. const ActualReactVirtualized = jest.requireActual('react-virtualized');
  70. return {
  71. ...ActualReactVirtualized,
  72. AutoSizer: ({children}) => children({width: 100, height: 100}),
  73. };
  74. });
  75. jest.mock('echarts-for-react/lib/core', () => {
  76. // We need to do this because `jest.mock` gets hoisted by babel and `React` is not
  77. // guaranteed to be in scope
  78. const ReactActual = require('react');
  79. // We need a class component here because `BaseChart` passes `ref` which will
  80. // error if we return a stateless/functional component
  81. return class extends ReactActual.Component {
  82. render() {
  83. return null;
  84. }
  85. };
  86. });
  87. jest.mock('@sentry/react', () => {
  88. const SentryReact = jest.requireActual('@sentry/react');
  89. return {
  90. init: jest.fn(),
  91. configureScope: jest.fn(),
  92. setTag: jest.fn(),
  93. setTags: jest.fn(),
  94. setExtra: jest.fn(),
  95. setExtras: jest.fn(),
  96. captureBreadcrumb: jest.fn(),
  97. addBreadcrumb: jest.fn(),
  98. captureMessage: jest.fn(),
  99. captureException: jest.fn(),
  100. showReportDialog: jest.fn(),
  101. startSpan: jest.fn(),
  102. finishSpan: jest.fn(),
  103. lastEventId: jest.fn(),
  104. getCurrentHub: jest.spyOn(SentryReact, 'getCurrentHub'),
  105. withScope: jest.spyOn(SentryReact, 'withScope'),
  106. Severity: SentryReact.Severity,
  107. withProfiler: SentryReact.withProfiler,
  108. };
  109. });
  110. jest.mock('popper.js', () => {
  111. const PopperJS = jest.requireActual('popper.js');
  112. return class {
  113. static placements = PopperJS.placements;
  114. constructor() {
  115. return {
  116. destroy: () => {},
  117. scheduleUpdate: () => {},
  118. };
  119. }
  120. };
  121. });
  122. // We generally use actual jQuery, and jest mocks takes precedence over node_modules.
  123. jest.unmock('jquery');
  124. /**
  125. * Test Globals
  126. */
  127. // This is so we can use async/await in tests instead of wrapping with `setTimeout`.
  128. window.tick = () => new Promise(resolve => setTimeout(resolve));
  129. window.$ = window.jQuery = jQuery;
  130. window.scrollTo = jest.fn();
  131. // This is very commonly used, so expose it globally.
  132. window.MockApiClient = jest.requireMock('app/api').Client;
  133. window.TestStubs = {
  134. // react-router's 'router' context
  135. router: (params = {}) => ({
  136. push: jest.fn(),
  137. replace: jest.fn(),
  138. go: jest.fn(),
  139. goBack: jest.fn(),
  140. goForward: jest.fn(),
  141. listen: jest.fn(),
  142. setRouteLeaveHook: jest.fn(),
  143. isActive: jest.fn(),
  144. createHref: jest.fn(),
  145. location: {query: {}},
  146. ...params,
  147. }),
  148. location: (params = {}) => ({
  149. query: {},
  150. pathame: '/mock-pathname/',
  151. ...params,
  152. }),
  153. routes: () => [
  154. {path: '/'},
  155. {path: '/:orgId/'},
  156. {name: 'this should be skipped'},
  157. {path: '/organizations/:orgId/'},
  158. {path: 'api-keys/', name: 'API Key'},
  159. ],
  160. routerProps: (params = {}) => ({
  161. location: TestStubs.location(),
  162. params: {},
  163. routes: [],
  164. stepBack: () => {},
  165. ...params,
  166. }),
  167. routerContext: ([context, childContextTypes] = []) => ({
  168. context: {
  169. location: TestStubs.location(),
  170. router: TestStubs.router(),
  171. organization: fixtures.Organization(),
  172. project: fixtures.Project(),
  173. ...context,
  174. },
  175. childContextTypes: {
  176. router: PropTypes.object,
  177. location: PropTypes.object,
  178. organization: PropTypes.object,
  179. project: PropTypes.object,
  180. ...childContextTypes,
  181. },
  182. }),
  183. AllAuthenticators: () => Object.values(fixtures.Authenticators()).map(x => x()),
  184. ...fixtures,
  185. };