-
Notifications
You must be signed in to change notification settings - Fork 19
/
jest.config.js
60 lines (52 loc) · 1.89 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
const {dirname, resolve, join} = require('node:path');
const {readdirSync, existsSync} = require('node:fs');
const micromatch = require('micromatch');
const glob = require('glob');
function readConfig(filePath) {
const absFilePath = resolve(filePath);
const prevCwd = process.cwd();
process.chdir(dirname(absFilePath));
Object.keys(require.cache).forEach(key => {
delete require.cache[key];
});
// eslint-disable-next-line import/no-dynamic-require, global-require
const result = require(absFilePath);
process.chdir(prevCwd);
return result;
}
function escapeRegexWord(filePath) {
return filePath.replace(/[^a-z0-9]/gi, $ => `\\${$}`);
}
const packages = readdirSync(join(__dirname, 'packages')).filter(name =>
existsSync(join(__dirname, 'packages', name, 'jest.config.js'))
);
const projects = packages.map(name => ({
...readConfig(join(__dirname, 'packages', name, 'jest.config.js')),
rootDir: join(__dirname, 'packages', name),
}));
const files = glob.sync(`${join(__dirname, 'packages')}/**`, {ignore: '**/node_modules/**'});
const coveragePatterns = projects.map(project =>
(project.collectCoverageFrom || []).map(pattern => {
if (pattern.includes('<rootDir>')) {
return pattern.replace('<rootDir>', project.rootDir);
}
if (/^!?\//.test(pattern)) {
return pattern;
}
return pattern.startsWith('!') ? `!${project.rootDir}/${pattern.substring(1)}` : `${project.rootDir}/${pattern}`;
})
);
const coverageFiles = coveragePatterns.flatMap(patterns =>
files.filter(filePath => micromatch.isMatch(filePath, patterns))
);
module.exports = {
projects,
// Hack to inherit coverage patterns from projects
collectCoverageFrom: ['**/*.{ts,tsx}'],
coveragePathIgnorePatterns: [
`^(?!${escapeRegexWord(__dirname)}\\/${coverageFiles
.map(filePath => filePath.replace(`${__dirname}/`, ''))
.map(escapeRegexWord)
.join('|')})$).*$`,
],
};