jest.config.ts 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /* eslint-env node */
  2. /* eslint import/no-nodejs-modules:0 */
  3. import path from 'path';
  4. import process from 'process';
  5. import type {Config} from '@jest/types';
  6. import babelConfig from './babel.config';
  7. const {
  8. CI,
  9. JEST_TESTS,
  10. JEST_TEST_BALANCER,
  11. CI_NODE_TOTAL,
  12. CI_NODE_INDEX,
  13. GITHUB_PR_SHA,
  14. GITHUB_PR_REF,
  15. GITHUB_RUN_ID,
  16. GITHUB_RUN_ATTEMPT,
  17. } = process.env;
  18. const IS_MASTER_BRANCH = GITHUB_PR_REF === 'refs/heads/master';
  19. const BALANCE_RESULTS_PATH = path.resolve(
  20. __dirname,
  21. 'tests',
  22. 'js',
  23. 'test-balancer',
  24. 'jest-balance.json'
  25. );
  26. const optionalTags: {
  27. balancer?: boolean;
  28. balancer_strategy?: string;
  29. } = {
  30. balancer: false,
  31. };
  32. if (!!JEST_TEST_BALANCER && !CI) {
  33. throw new Error(
  34. '[Operation only allowed in CI]: Jest test balancer should never be ran manually as you risk skewing the numbers - please trigger the automated github workflow at https://github.com/getsentry/sentry/actions/workflows/jest-balance.yml'
  35. );
  36. }
  37. /**
  38. * In CI we may need to shard our jest tests so that we can parellize the test runs
  39. *
  40. * `JEST_TESTS` is a list of all tests that will run, captured by `jest --listTests`
  41. * Then we split up the tests based on the total number of CI instances that will
  42. * be running the tests.
  43. */
  44. let testMatch: string[] | undefined;
  45. function getTestsForGroup(
  46. nodeIndex: number,
  47. nodeTotal: number,
  48. allTests: ReadonlyArray<string>,
  49. testStats: Record<string, number>
  50. ): string[] {
  51. const speculatedSuiteDuration = Object.values(testStats).reduce((a, b) => a + b, 0);
  52. const targetDuration = speculatedSuiteDuration / nodeTotal;
  53. if (speculatedSuiteDuration <= 0) {
  54. throw new Error('Speculated suite duration is <= 0');
  55. }
  56. // We are going to take all of our tests and split them into groups.
  57. // If we have a test without a known duration, we will default it to 2 second
  58. // This is to ensure that we still assign some weight to the tests and still attempt to somewhat balance them.
  59. // The 1.5s default is selected as a p50 value of all of our JS tests in CI (as of 2022-10-26) taken from our sentry performance monitoring.
  60. const tests = new Map<string, number>();
  61. const SUITE_P50_DURATION_MS = 1500;
  62. // First, iterate over all of the tests we have stats for.
  63. for (const test in testStats) {
  64. if (testStats[test] <= 0) {
  65. throw new Error(`Test duration is <= 0 for ${test}`);
  66. }
  67. tests.set(test, testStats[test]);
  68. }
  69. // Then, iterate over all of the remaining tests and assign them a default duration.
  70. for (const test of allTests) {
  71. if (tests.has(test)) {
  72. continue;
  73. }
  74. tests.set(test, SUITE_P50_DURATION_MS);
  75. }
  76. // Sanity check to ensure that we have all of our tests accounted for, we need to fail
  77. // if this is not the case as we risk not executing some tests and passing the build.
  78. if (tests.size < allTests.length) {
  79. throw new Error(
  80. `All tests should be accounted for, missing ${allTests.length - tests.size}`
  81. );
  82. }
  83. const groups: string[][] = [];
  84. // We sort files by path so that we try and improve the transformer cache hit rate.
  85. // Colocated domain specific files are likely to require other domain specific files.
  86. const testsSortedByPath = Array.from(tests.entries()).sort((a, b) => {
  87. // WidgetBuilder tests are a special case as they can sometimes take a long time to run (3-5 minutes)
  88. // As such, we want to ensure that they are ran in the same group as other widget builder tests.
  89. // We do this by sorting them by the path of the widget builder test which ensures they are started by the first job
  90. // in the CI group and that all of the tests actually run in the same group.
  91. if (a[0].includes('widgetBuilder')) {
  92. return -1;
  93. }
  94. if (b[0].includes('widgetBuilder')) {
  95. return 1;
  96. }
  97. return a[0].localeCompare(b[0]);
  98. });
  99. for (let group = 0; group < nodeTotal; group++) {
  100. groups[group] = [];
  101. let duration = 0;
  102. // While we are under our target duration and there are tests in the group
  103. while (duration < targetDuration && testsSortedByPath.length > 0) {
  104. // We peek the next item to check that it is not some super long running
  105. // test that may exceed our target duration. For example, if target runtime for each group is
  106. // 10 seconds, we have currently accounted for 9 seconds, and the next test is 5 seconds, we
  107. // want to move that test to the next group so as to avoid a 40% imbalance.
  108. const peek = testsSortedByPath[testsSortedByPath.length - 1];
  109. if (duration + peek[1] > targetDuration && peek[1] > 30_000) {
  110. break;
  111. }
  112. const nextTest = testsSortedByPath.pop();
  113. if (!nextTest) {
  114. throw new TypeError('Received falsy test' + JSON.stringify(nextTest));
  115. }
  116. groups[group].push(nextTest[0]);
  117. duration += nextTest[1];
  118. }
  119. }
  120. // Whatever may be left over will get round robin'd into the groups.
  121. let i = 0;
  122. while (testsSortedByPath.length) {
  123. const nextTest = testsSortedByPath.pop();
  124. if (!nextTest) {
  125. throw new TypeError('Received falsy test' + JSON.stringify(nextTest));
  126. }
  127. groups[i % 4].push(nextTest[0]);
  128. i++;
  129. }
  130. // Make sure we exhausted all tests before proceeding.
  131. if (testsSortedByPath.length > 0) {
  132. throw new Error('All tests should be accounted for');
  133. }
  134. // We need to ensure that everything from jest --listTests is accounted for.
  135. for (const test of allTests) {
  136. if (!tests.has(test)) {
  137. throw new Error(`Test ${test} is not accounted for`);
  138. }
  139. }
  140. if (!groups[nodeIndex]) {
  141. throw new Error(`No tests found for node ${nodeIndex}`);
  142. }
  143. return groups[nodeIndex].map(test => `<rootDir>/${test}`);
  144. }
  145. if (
  146. JEST_TESTS &&
  147. typeof CI_NODE_TOTAL !== 'undefined' &&
  148. typeof CI_NODE_INDEX !== 'undefined'
  149. ) {
  150. let balance: null | Record<string, number> = null;
  151. try {
  152. balance = require(BALANCE_RESULTS_PATH);
  153. } catch (err) {
  154. // Just ignore if balance results doesn't exist
  155. }
  156. // Taken from https://github.com/facebook/jest/issues/6270#issue-326653779
  157. const envTestList: string[] = JSON.parse(JEST_TESTS).map(file =>
  158. file.replace(__dirname, '')
  159. );
  160. const nodeTotal = Number(CI_NODE_TOTAL);
  161. const nodeIndex = Number(CI_NODE_INDEX);
  162. if (balance) {
  163. optionalTags.balancer = true;
  164. optionalTags.balancer_strategy = 'by_path';
  165. testMatch = getTestsForGroup(nodeIndex, nodeTotal, envTestList, balance);
  166. } else {
  167. const tests = envTestList.sort((a, b) => b.localeCompare(a));
  168. const length = tests.length;
  169. const size = Math.floor(length / nodeTotal);
  170. const remainder = length % nodeTotal;
  171. const offset = Math.min(nodeIndex, remainder) + nodeIndex * size;
  172. const chunk = size + (nodeIndex < remainder ? 1 : 0);
  173. testMatch = tests.slice(offset, offset + chunk).map(test => '<rootDir>' + test);
  174. }
  175. }
  176. /**
  177. * For performance we don't want to try and compile everything in the
  178. * node_modules, but some packages which use ES6 syntax only NEED to be
  179. * transformed.
  180. */
  181. const ESM_NODE_MODULES = [];
  182. const config: Config.InitialOptions = {
  183. verbose: false,
  184. collectCoverageFrom: [
  185. 'static/app/**/*.{js,jsx,ts,tsx}',
  186. '!static/app/**/*.spec.{js,jsx,ts,tsx}',
  187. ],
  188. coverageReporters: ['html', 'cobertura'],
  189. coverageDirectory: '.artifacts/coverage',
  190. moduleNameMapper: {
  191. '^sentry/(.*)': '<rootDir>/static/app/$1',
  192. '^sentry-test/(.*)': '<rootDir>/tests/js/sentry-test/$1',
  193. '^sentry-locale/(.*)': '<rootDir>/src/sentry/locale/$1',
  194. '\\.(css|less|png|jpg|mp4)$': '<rootDir>/tests/js/sentry-test/importStyleMock.js',
  195. '\\.(svg)$': '<rootDir>/tests/js/sentry-test/svgMock.js',
  196. 'integration-docs-platforms': '<rootDir>/fixtures/integration-docs/_platforms.json',
  197. // Disable echarts in test, since they're very slow and take time to
  198. // transform
  199. '^echarts/(.*)': '<rootDir>/tests/js/sentry-test/echartsMock.js',
  200. '^zrender/(.*)': '<rootDir>/tests/js/sentry-test/echartsMock.js',
  201. },
  202. setupFiles: [
  203. '<rootDir>/static/app/utils/silence-react-unsafe-warnings.ts',
  204. 'jest-canvas-mock',
  205. ],
  206. setupFilesAfterEnv: [
  207. '<rootDir>/tests/js/setup.ts',
  208. '<rootDir>/tests/js/setupFramework.ts',
  209. '@testing-library/jest-dom/extend-expect',
  210. ],
  211. testMatch: testMatch || ['<rootDir>/static/**/?(*.)+(spec|test).[jt]s?(x)'],
  212. testPathIgnorePatterns: ['<rootDir>/tests/sentry/lang/javascript/'],
  213. unmockedModulePathPatterns: [
  214. '<rootDir>/node_modules/react',
  215. '<rootDir>/node_modules/reflux',
  216. ],
  217. transform: {
  218. '^.+\\.jsx?$': ['babel-jest', babelConfig as any],
  219. '^.+\\.tsx?$': ['babel-jest', babelConfig as any],
  220. '^.+\\.pegjs?$': '<rootDir>/tests/js/jest-pegjs-transform.js',
  221. },
  222. transformIgnorePatterns: [
  223. ESM_NODE_MODULES.length
  224. ? `/node_modules/(?!${ESM_NODE_MODULES.join('|')})`
  225. : '/node_modules/',
  226. ],
  227. moduleFileExtensions: ['js', 'ts', 'jsx', 'tsx'],
  228. globals: {},
  229. testResultsProcessor: JEST_TEST_BALANCER
  230. ? '<rootDir>/tests/js/test-balancer/index.js'
  231. : undefined,
  232. reporters: [
  233. 'default',
  234. [
  235. 'jest-junit',
  236. {
  237. outputDirectory: '.artifacts',
  238. outputName: 'jest.junit.xml',
  239. },
  240. ],
  241. ],
  242. /**
  243. * jest.clearAllMocks() automatically called before each test
  244. * @link - https://jestjs.io/docs/configuration#clearmocks-boolean
  245. */
  246. clearMocks: true,
  247. testEnvironment: 'jsdom',
  248. testEnvironmentOptions: {
  249. sentryConfig: {
  250. init: {
  251. // jest project under Sentry organization (dev productivity team)
  252. dsn: 'https://3fe1dce93e3a4267979ebad67f3de327@sentry.io/4857230',
  253. // Use production env to reduce sampling of commits on master
  254. environment: CI ? (IS_MASTER_BRANCH ? 'ci:master' : 'ci:pull_request') : 'local',
  255. tracesSampleRate: CI ? 1 : 0.5,
  256. profilesSampleRate: 0.1,
  257. transportOptions: {keepAlive: true},
  258. },
  259. transactionOptions: {
  260. tags: {
  261. ...optionalTags,
  262. branch: GITHUB_PR_REF,
  263. commit: GITHUB_PR_SHA,
  264. github_run_attempt: GITHUB_RUN_ATTEMPT,
  265. github_actions_run: `https://github.com/getsentry/sentry/actions/runs/${GITHUB_RUN_ID}`,
  266. },
  267. },
  268. },
  269. },
  270. };
  271. export default config;