forked from getsentry/sentry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jest.config.ts
236 lines (207 loc) · 6.58 KB
/
jest.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* eslint-env node */
/* eslint import/no-nodejs-modules:0 */
import path from 'path';
import process from 'process';
import type {Config} from '@jest/types';
import babelConfig from './babel.config';
const {
CI,
JEST_TESTS,
JEST_TEST_BALANCER,
CI_NODE_TOTAL,
CI_NODE_INDEX,
GITHUB_PR_SHA,
GITHUB_PR_REF,
GITHUB_RUN_ID,
GITHUB_RUN_ATTEMPT,
} = process.env;
/**
* In CI we may need to shard our jest tests so that we can parellize the test runs
*
* `JEST_TESTS` is a list of all tests that will run, captured by `jest --listTests`
* Then we split up the tests based on the total number of CI instances that will
* be running the tests.
*/
let testMatch: string[] | undefined;
const BALANCE_RESULTS_PATH = path.resolve(
__dirname,
'tests',
'js',
'test-balancer',
'jest-balance.json'
);
/**
* Given a Map of <testName, testRunTime> and a number of total groups, split the
* tests into n groups whose total test run times should be roughly equal
*
* The source results should be sorted with the slowest tests first. We insert
* the test into the smallest group on each iteration. This isn't perfect, but
* should be good enough.
*
* Returns a map of <testName, groupIndex>
*/
function balancer(
allTests: string[],
source: Record<string, number>,
numberGroups: number
) {
const results = new Map<string, number>();
const totalRunTimes = Array(numberGroups).fill(0);
/**
* Find the index of the smallest group (totalRunTimes)
*/
function findSmallestGroup() {
let index = 0;
let smallestRunTime = null;
for (let i = 0; i < totalRunTimes.length; i++) {
const runTime = totalRunTimes[i];
if (!smallestRunTime || runTime <= smallestRunTime) {
smallestRunTime = totalRunTimes[i];
index = i;
}
if (runTime === 0) {
break;
}
}
return index;
}
/**
* We may not have a duration for all tests (e.g. a test that was just added)
* as the `source` needs to be generated
*/
for (const test of allTests) {
const index = findSmallestGroup();
results.set(test, index);
if (source[test] !== undefined) {
totalRunTimes[index] = totalRunTimes[index] + source[test];
}
}
return results;
}
if (
JEST_TESTS &&
typeof CI_NODE_TOTAL !== 'undefined' &&
typeof CI_NODE_INDEX !== 'undefined'
) {
let balance: null | Record<string, number> = null;
try {
balance = require(BALANCE_RESULTS_PATH);
} catch (err) {
// Just ignore if balance results doesn't exist
}
// Taken from https://github.com/facebook/jest/issues/6270#issue-326653779
const envTestList = JSON.parse(JEST_TESTS).map(file =>
file.replace(__dirname, '')
) as string[];
const tests = envTestList.sort((a, b) => b.localeCompare(a));
const nodeTotal = Number(CI_NODE_TOTAL);
const nodeIndex = Number(CI_NODE_INDEX);
if (balance) {
const results = balancer(envTestList, balance, nodeTotal);
testMatch = [
// First, we only want the tests that we have test durations for and belong
// to the current node's index
...Object.entries(Object.fromEntries(results))
.filter(([, index]) => index === nodeIndex)
.map(([test]) => `${path.join(__dirname, test)}`),
];
} else {
const length = tests.length;
const size = Math.floor(length / nodeTotal);
const remainder = length % nodeTotal;
const offset = Math.min(nodeIndex, remainder) + nodeIndex * size;
const chunk = size + (nodeIndex < remainder ? 1 : 0);
testMatch = tests.slice(offset, offset + chunk);
}
}
/**
* For performance we don't want to try and compile everything in the
* node_modules, but some packages which use ES6 syntax only NEED to be
* transformed.
*/
const ESM_NODE_MODULES = ['copy-text-to-clipboard'];
const config: Config.InitialOptions = {
verbose: false,
collectCoverageFrom: [
'tests/js/spec/**/*.{js,jsx,tsx}',
'static/app/**/*.{js,jsx,ts,tsx}',
],
coverageReporters: ['html', 'cobertura'],
coverageDirectory: '.artifacts/coverage',
moduleNameMapper: {
'^sentry/(.*)': '<rootDir>/static/app/$1',
'^sentry-test/(.*)': '<rootDir>/tests/js/sentry-test/$1',
'^sentry-locale/(.*)': '<rootDir>/src/sentry/locale/$1',
'\\.(css|less|png|jpg|mp4)$': '<rootDir>/tests/js/sentry-test/importStyleMock.js',
'\\.(svg)$': '<rootDir>/tests/js/sentry-test/svgMock.js',
'integration-docs-platforms': '<rootDir>/fixtures/integration-docs/_platforms.json',
// Disable echarts in test, since they're very slow and take time to
// transform
'^echarts/(.*)': '<rootDir>/tests/js/sentry-test/echartsMock.js',
'^zrender/(.*)': '<rootDir>/tests/js/sentry-test/echartsMock.js',
},
setupFiles: [
'<rootDir>/static/app/utils/silence-react-unsafe-warnings.ts',
'jest-canvas-mock',
],
setupFilesAfterEnv: [
'<rootDir>/tests/js/setup.ts',
'<rootDir>/tests/js/setupFramework.ts',
'@testing-library/jest-dom/extend-expect',
],
testMatch: testMatch || [
'<rootDir>/static/**/?(*.)+(spec|test).[jt]s?(x)',
'<rootDir>/tests/js/**/*(*.)@(spec|test).(js|ts)?(x)',
],
testPathIgnorePatterns: ['<rootDir>/tests/sentry/lang/javascript/'],
unmockedModulePathPatterns: [
'<rootDir>/node_modules/react',
'<rootDir>/node_modules/reflux',
],
transform: {
'^.+\\.jsx?$': ['babel-jest', babelConfig as any],
'^.+\\.tsx?$': ['babel-jest', babelConfig as any],
'^.+\\.pegjs?$': '<rootDir>/tests/js/jest-pegjs-transform.js',
},
transformIgnorePatterns: [`/node_modules/(?!${ESM_NODE_MODULES.join('|')})`],
moduleFileExtensions: ['js', 'ts', 'jsx', 'tsx'],
globals: {},
reporters: [
'default',
[
'jest-junit',
{
outputDirectory: '.artifacts',
outputName: 'jest.junit.xml',
},
],
[
'<rootDir>/tests/js/test-balancer',
{
enabled: !!JEST_TEST_BALANCER,
resultsPath: BALANCE_RESULTS_PATH,
},
],
],
testEnvironment: '<rootDir>/tests/js/instrumentedEnv',
testEnvironmentOptions: {
sentryConfig: {
init: {
// jest project under Sentry organization (dev productivity team)
dsn: 'https://3fe1dce93e3a4267979ebad67f3de327@sentry.io/4857230',
environment: CI ? 'ci' : 'local',
tracesSampleRate: 1.0,
},
transactionOptions: {
tags: {
branch: GITHUB_PR_REF,
commit: GITHUB_PR_SHA,
github_run_attempt: GITHUB_RUN_ATTEMPT,
github_actions_run: `https://github.com/getsentry/sentry/actions/runs/${GITHUB_RUN_ID}`,
},
},
},
output: path.resolve(__dirname, '.artifacts', 'visual-snapshots', 'jest'),
},
};
export default config;