jest.config.ts 3.2 KB

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