forked from webiny/webiny-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jest.config.js
165 lines (147 loc) · 4.79 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
const fs = require("fs");
const path = require("path");
const { allWorkspaces } = require("@webiny/project-utils/workspaces");
const createPackageJestConfigPath = pkg => {
const jestConfigPath = path.join(pkg, "jest.config.js");
if (!fs.existsSync(jestConfigPath)) {
return null;
}
return jestConfigPath;
};
const createPackageJestSetupPath = pkg => {
const setupPath = path.join(pkg, "jest.setup.js");
if (!fs.existsSync(setupPath)) {
return null;
}
return setupPath;
};
const getPackageKeywords = pkg => {
const file = path.join(pkg, "package.json");
if (!fs.existsSync(file)) {
throw new Error(`Missing package.json "${file}".`);
}
const content = fs.readFileSync(file).toString();
try {
const json = JSON.parse(content);
return Array.isArray(json.keywords) ? json.keywords : [];
} catch (ex) {
throw new Error(`Could not parse package.json "${file}".`);
}
};
const hasPackageJestConfig = pkg => {
return !!createPackageJestConfigPath(pkg);
};
const getPackageJestSetup = pkg => {
const setupPath = createPackageJestSetupPath(pkg);
if (!setupPath) {
return null;
}
return require(setupPath);
};
const identifiers = {};
const createPackageName = initialName => {
let name = initialName;
let current = 0;
while (identifiers[name]) {
name = `${initialName}-${current}`;
}
return name;
};
const createPackageFilter = (args = []) => {
const filters = args
.filter(arg => {
return arg.startsWith("--keyword=");
})
.map(arg => {
return arg.replace("--keyword=", "");
});
return (packageKeywords = []) => {
if (
!packageKeywords ||
filters.length === 0 ||
(packageKeywords.length === 0 && filters.length === 0)
) {
return true;
}
for (const filter of filters) {
// a single keyword in the argument
const result = packageKeywords.includes(filter);
if (result) {
return true;
}
}
return false;
};
};
const isPackageAllowed = createPackageFilter(process.argv);
// Extract positional arguments after --logHeapUsage (we assume those are package names/paths)
// --logHeapUsage is the last optional parameter defined in package.json "test" script.
const positionalParams = process.argv
.slice(process.argv.indexOf("--logHeapUsage"))
.filter(p => !p.startsWith("--"));
const packagesInParams = positionalParams
.map(p => {
if (p.includes("packages")) {
return path.resolve(p);
}
return path.resolve("packages", p);
})
.map(p => p.replace(/\\/g, "/"));
function isPackageInParams(pkg) {
if (!packagesInParams.length) {
return true;
}
return packagesInParams.some(p => p.startsWith(pkg));
}
const projects = allWorkspaces()
.map(p => p.replace(/\\/g, "/"))
.reduce((collection, pkg) => {
const hasConfig = hasPackageJestConfig(pkg);
const setup = getPackageJestSetup(pkg);
const basePackagePath = pkg.replace(process.cwd() + "/", "");
if (!hasConfig && !setup) {
return collection;
}
// we need to filter out the packages that do not match required keywords, if any
const keywords = getPackageKeywords(pkg);
if (!isPackageAllowed(keywords)) {
return collection;
}
if (!isPackageInParams(pkg)) {
return collection;
}
if (setup && (Array.isArray(setup) === true || setup["0"] !== undefined)) {
for (const key in setup) {
if (!setup.hasOwnProperty(key)) {
continue;
}
const subPackage = setup[key];
// we need to filter out the subpackage as well
if (!isPackageAllowed(subPackage.keywords)) {
continue;
}
// keywords does not exist in jest config so remove it
// there will be error if not removed
delete subPackage["keywords"];
const name = createPackageName(subPackage.name);
collection.push({
...subPackage,
name: name,
displayName: name,
rootDir: subPackage.rootDir || pkg
});
}
return collection;
}
return collection.concat([basePackagePath]);
}, [])
.filter(Boolean);
if (projects.length === 0) {
console.log(`There are no packages found. Please check the filters if you are using those.`);
process.exit(1);
}
module.exports = {
projects,
modulePathIgnorePatterns: ["dist"],
testTimeout: 30000
};