-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjest.config.js
199 lines (186 loc) · 8.01 KB
/
jest.config.js
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
//@flow
/* eslint-disable no-console */
const fs = require('fs');
const { CHANGED_PACKAGES } = process.env;
const { COVERAGE_PACKAGES } = process.env;
const { INTEGRATION_TESTS } = process.env;
const { VISUAL_REGRESSION } = process.env;
const { PARALLELIZE_TESTS } = process.env;
const { PARALLELIZE_TESTS_FILE } = process.env;
// These are set by Pipelines if you are running in a parallel steps
const STEP_IDX = Number(process.env.STEP_IDX);
const STEPS = Number(process.env.STEPS);
/**
* USAGE for parallelizing: setting PARALLELIZE_TESTS to an array of globs or an array of test files when you
* have the STEPS and STEP_IDX vars set will automatically distribute them evenly.
* It is important that **ALL** parallel steps are running the same command with the same number of tests and that **ALL**
* parallel steps are running the command (i.e you can have 3 steps running jest and one running linting) as this will throw
* the calculations off
*
* Run all the tests, but in parallel
* PARALLELIZE_TESTS="$(yarn --silent jest --listTests)" yarn jest --listTests
*
* Run only tests for changed packages (in parallel)
* PARALLELIZE_TESTS="$(CHANGED_PACKAGES=$(node build/ci-scripts/get.changed.packages.since.master.js) yarn --silent jest --listTests)" yarn jest --listTests
*
* If the number of tests to run is too long for bash, you can pass a file containing the tests instead
* TMPFILE="$(mktemp /tmp/jest.XXXXX)"
* yarn --silent jest --listTests > $TMPFILE
* PARALLELIZE_TESTS_FILE="$TMPFILE" yarn jest --listTests
*/
const config = {
testMatch: [`${__dirname}/**/__tests__/**/*.(js|tsx|ts)`],
// NOTE: all options with 'pattern' in the name are javascript regex's that will match if they match
// anywhere in the string. Where-ever there are an array of patterns, jest simply 'or's all of them
// i.e /\/__tests__\/_.*?|\/__tests__\/.*?\/_.*?|\/__tests__\/integration\//
testPathIgnorePatterns: [
// ignore files that are under a directory starting with "_" at the root of __tests__
'/__tests__\\/_.*?',
// ignore files under __tests__ that start with an underscore
'/__tests__\\/.*?\\/_.*?',
// ignore tests under __tests__/flow
'/__tests__\\/flow/',
// ignore tests under __tests__/integration (we override this if the INTEGRATION_TESTS flag is set)
'/__tests__\\/integration/',
// ignore tests under __tests__/vr (we override this if the VISUAL_REGRESSION flag is set)
'/__tests__\\/visual-regression/',
],
// NOTE: This ignored list is required because the script is bundling `@atlaskit/navigation-next`
// which causes infinite loop if run tests in watch mode
watchPathIgnorePatterns: [
'\\/packages\\/core\\/navigation-next\\/[^\\/]*\\.js$',
],
watchPlugins: [
'jest-watch-typeahead/filename',
'jest-watch-typeahead/testname',
],
modulePathIgnorePatterns: ['/__fixtures__/', './node_modules', '/dist/'],
// don't transform any files under node_modules except @atlaskit/* and react-syntax-highlighter (it
// uses dynamic imports which are not valid in node)
transformIgnorePatterns: [
'\\/node_modules\\/(?!@atlaskit|react-syntax-highlighter)',
],
resolver: `${__dirname}/resolver.js`,
transform: {
'^.+\\.tsx?$': 'ts-jest',
'^.+\\.js$': 'babel-jest',
},
globals: {
'ts-jest': {
tsConfigFile: './tsconfig.jest.json',
},
__BASEURL__: 'http://localhost:9000',
},
moduleFileExtensions: ['js', 'ts', 'tsx', 'json'],
moduleNameMapper: {
'\\.(jpg|jpeg|png|gif|svg)$': '<rootDir>/fileMock.js',
},
snapshotSerializers: ['enzyme-to-json/serializer'],
setupFiles: ['./build/jest-config/setup.js', 'jest-canvas-mock'],
setupFilesAfterEnv: [`${__dirname}/jestFrameworkSetup.js`],
testResultsProcessor: 'jest-junit',
testEnvironmentOptions: {
// Need this to have jsdom loading images.
resources: 'usable',
},
coverageReporters: ['lcov', 'html', 'text-summary'],
collectCoverage: false,
collectCoverageFrom: [],
coverageThreshold: {},
globalSetup: undefined,
globalTeardown: undefined,
// Jest's default test environment 'jsdom' uses JSDOM 11 to support Node 6. Here we upgrade to JSDOM 14, which supports Node >= 8
testEnvironment: 'jest-environment-jsdom-fourteen',
// The modules below cause problems when automocking.
unmockedModulePathPatterns: [
'tslib',
'babel-runtime',
'es-abstract',
'graceful-fs',
'any-promise',
'globby',
'chalk',
'fs-extra',
'meow',
],
};
// If the CHANGED_PACKAGES variable is set, we parse it to get an array of changed packages and only
// run the tests for those packages
if (CHANGED_PACKAGES) {
const changedPackages = JSON.parse(CHANGED_PACKAGES);
const changedPackagesTestGlobs = changedPackages.map(
pkgPath => `${__dirname}/${pkgPath}/**/__tests__/**/*.(js|tsx|ts)`,
);
config.testMatch = changedPackagesTestGlobs;
}
// Adding code coverage thresold configuration for unit test only
// This should add only the packages with code coverage threshold available
// If not it will keep the same flow without code coverage check
if (COVERAGE_PACKAGES) {
const coveragePackages = JSON.parse(COVERAGE_PACKAGES);
if (coveragePackages.collectCoverageFrom.length > 0) {
config.collectCoverage = true;
config.collectCoverageFrom = coveragePackages.collectCoverageFrom;
config.coverageThreshold = coveragePackages.coverageThreshold;
}
}
// If the INTEGRATION_TESTS / VISUAL_REGRESSION flag is set we need to
if (INTEGRATION_TESTS || VISUAL_REGRESSION) {
const testPattern = process.env.VISUAL_REGRESSION
? 'visual-regression'
: 'integration';
const testPathIgnorePatterns /*: string[] */ = config.testPathIgnorePatterns.filter(
pattern => pattern !== `/__tests__\\/${testPattern}/`,
);
config.testPathIgnorePatterns = testPathIgnorePatterns;
// If the CHANGED_PACKAGES variable is set, only integration tests from changed packages will run
if (CHANGED_PACKAGES) {
const changedPackages = JSON.parse(CHANGED_PACKAGES);
const changedPackagesTestGlobs = changedPackages.map(
pkgPath =>
`${__dirname}/${pkgPath}/**/__tests__/${testPattern}/**/*.(js|tsx|ts)`,
);
config.testMatch = changedPackagesTestGlobs;
} else {
config.testMatch = [`**/__tests__/${testPattern}/**/*.(js|tsx|ts)`];
}
}
/**
* Batching.
* In CI we want to be able to split out tests into multiple parallel steps that can be run concurrently.
* We do this by passing in a list of test files (PARALLELIZE_TESTS), the number of a parallel steps (STEPS)
* and the (0 indexed) index of the current step (STEP_IDX). Using these we can split the test up evenly
*/
if (PARALLELIZE_TESTS || PARALLELIZE_TESTS_FILE) {
const testStr = PARALLELIZE_TESTS_FILE
? fs.readFileSync(PARALLELIZE_TESTS_FILE, 'utf8')
: PARALLELIZE_TESTS;
if (!testStr) {
throw new Error('Cannot read parallelized tests');
}
const allTests = JSON.parse(testStr);
config.testMatch = allTests.filter((_, i) => (i % STEPS) - STEP_IDX === 0);
console.log('Parallelising jest tests.');
console.log(`Parallel step ${String(STEP_IDX)} of ${String(STEPS)}`);
console.log('Total test files', allTests.length);
console.log('Number of test files in this step', config.testMatch.length);
}
// Annoyingly, if the array is empty, jest will fallback to its defaults and run everything
if (config.testMatch.length === 0) {
config.testMatch = ['DONT-RUN-ANYTHING'];
config.collectCoverage = false;
// only log this message if we are running in an actual terminal (output not being piped to a file
// or a subshell)
if (process.stdout.isTTY) {
console.log('No packages were changed, so no tests should be run.');
}
}
if (process.env.VISUAL_REGRESSION) {
config.globalSetup = `${__dirname}/build/visual-regression/config/jest/globalSetup.js`;
config.globalTeardown = `${__dirname}/build/visual-regression/config/jest/globalTeardown.js`;
config.testEnvironment = `${__dirname}/build/visual-regression/config/jest/jsdomEnvironment.js`;
if (!process.env.CI && !process.env.DEBUG) {
config.globals.__BASEURL__ = 'http://testing.local.com:9000';
}
}
module.exports = config;