jest.config.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 {CI, JEST_TESTS, CI_NODE_TOTAL, CI_NODE_INDEX, GITHUB_PR_SHA, GITHUB_PR_REF} =
  8. process.env;
  9. /**
  10. * In CI we may need to shard our jest tests so that we can parellize the test runs
  11. *
  12. * `JEST_TESTS` is a list of all tests that will run, captured by `jest --listTests`
  13. * Then we split up the tests based on the total number of CI instances that will
  14. * be running the tests.
  15. */
  16. let testMatch: string[] | undefined;
  17. if (
  18. JEST_TESTS &&
  19. typeof CI_NODE_TOTAL !== 'undefined' &&
  20. typeof CI_NODE_INDEX !== 'undefined'
  21. ) {
  22. // Taken from https://github.com/facebook/jest/issues/6270#issue-326653779
  23. const envTestList = JSON.parse(JEST_TESTS) as string[];
  24. const tests = envTestList.sort((a, b) => b.localeCompare(a));
  25. const nodeTotal = Number(CI_NODE_TOTAL);
  26. const nodeIndex = Number(CI_NODE_INDEX);
  27. const length = tests.length;
  28. const size = Math.floor(length / nodeTotal);
  29. const remainder = length % nodeTotal;
  30. const offset = Math.min(nodeIndex, remainder) + nodeIndex * size;
  31. const chunk = size + (nodeIndex < remainder ? 1 : 0);
  32. testMatch = tests.slice(offset, offset + chunk);
  33. }
  34. const config: Config.InitialOptions = {
  35. verbose: false,
  36. collectCoverageFrom: [
  37. 'tests/js/spec/**/*.{js,jsx,tsx}',
  38. 'static/app/**/*.{js,jsx,ts,tsx}',
  39. ],
  40. coverageReporters: ['html', 'cobertura'],
  41. coverageDirectory: '.artifacts/coverage',
  42. moduleNameMapper: {
  43. '^sentry/(.*)': '<rootDir>/static/app/$1',
  44. '^sentry-test/(.*)': '<rootDir>/tests/js/sentry-test/$1',
  45. '^sentry-locale/(.*)': '<rootDir>/src/sentry/locale/$1',
  46. '\\.(css|less|png|jpg|mp4)$': '<rootDir>/tests/js/sentry-test/importStyleMock.js',
  47. '\\.(svg)$': '<rootDir>/tests/js/sentry-test/svgMock.js',
  48. 'integration-docs-platforms':
  49. '<rootDir>/tests/fixtures/integration-docs/_platforms.json',
  50. },
  51. setupFiles: [
  52. '<rootDir>/static/app/utils/silence-react-unsafe-warnings.ts',
  53. '<rootDir>/tests/js/throw-on-react-error.js',
  54. 'jest-canvas-mock',
  55. ],
  56. setupFilesAfterEnv: [
  57. '<rootDir>/tests/js/setup.ts',
  58. '<rootDir>/tests/js/setupFramework.ts',
  59. '@testing-library/jest-dom/extend-expect',
  60. ],
  61. testMatch: testMatch || ['<rootDir>/tests/js/**/*(*.)@(spec|test).(js|ts)?(x)'],
  62. testPathIgnorePatterns: ['<rootDir>/tests/sentry/lang/javascript/'],
  63. unmockedModulePathPatterns: [
  64. '<rootDir>/node_modules/react',
  65. '<rootDir>/node_modules/reflux',
  66. ],
  67. transform: {
  68. '^.+\\.jsx?$': ['babel-jest', babelConfig as any],
  69. '^.+\\.tsx?$': ['babel-jest', babelConfig as any],
  70. '^.+\\.pegjs?$': '<rootDir>/tests/js/jest-pegjs-transform.js',
  71. },
  72. transformIgnorePatterns: ['/node_modules/(?!echarts|zrender)'],
  73. moduleFileExtensions: ['js', 'ts', 'jsx', 'tsx'],
  74. globals: {},
  75. reporters: [
  76. 'default',
  77. [
  78. 'jest-junit',
  79. {
  80. outputDirectory: '.artifacts',
  81. outputName: 'jest.junit.xml',
  82. },
  83. ],
  84. ],
  85. testRunner: 'jest-circus/runner',
  86. testEnvironment: '<rootDir>/tests/js/instrumentedEnv',
  87. testEnvironmentOptions: {
  88. sentryConfig: {
  89. init: {
  90. dsn: 'https://3fe1dce93e3a4267979ebad67f3de327@sentry.io/4857230',
  91. environment: !!CI ? 'ci' : 'local',
  92. tracesSampleRate: 1.0,
  93. },
  94. transactionOptions: {
  95. tags: {
  96. branch: GITHUB_PR_REF,
  97. commit: GITHUB_PR_SHA,
  98. },
  99. },
  100. },
  101. output: path.resolve(__dirname, '.artifacts', 'visual-snapshots', 'jest'),
  102. },
  103. };
  104. export default config;