-
Notifications
You must be signed in to change notification settings - Fork 614
/
jest.config.base.js
187 lines (169 loc) · 6.38 KB
/
jest.config.base.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
const { basename, join, dirname } = require("path");
const fs = require("fs");
const merge = require("deepmerge");
const findUp = require("find-up");
const tsPreset = require("ts-jest/presets/js-with-babel/jest-preset");
const { version } = require("@webiny/cli/package.json");
module.exports = function ({ path }, presets = []) {
const name = basename(path);
// Enables us to run tests of only a specific type (for example "integration" or "e2e").
let type = "";
if (process.env.TEST_TYPE) {
type = `.${process.env.TEST_TYPE}`;
}
process.env.JEST_DYNALITE_CONFIG_DIRECTORY = path;
const merged = merge.all([
{ setupFilesAfterEnv: [] },
tsPreset,
{
displayName: name,
modulePaths: [`${path}/src`],
testMatch: [`${path}/**/*${type}.test.[jt]s?(x)`],
transform: {
"^.+\\.[jt]sx?$": [
"ts-jest",
{
isolatedModules: true,
babelConfig: `${path}/.babelrc.js`,
diagnostics: false
}
]
},
transformIgnorePatterns: ["/node_modules/(?!(nanoid)/)"],
moduleDirectories: ["node_modules"],
moduleNameMapper: {
"~tests/(.*)": `${path}/__tests__/$1`,
"~/(.*)": `${path}/src/$1`
},
modulePathIgnorePatterns: [
"<rootDir>/.verdaccio",
"<rootDir>/.webiny",
"<rootDir>/apps",
"<rootDir>/packages/.*/dist"
],
globals: {
WEBINY_VERSION: version
}
}
]);
merged.setupFiles = [
...(merged.setupFiles || []),
...presets
.map(preset => preset.setupFiles)
.flat()
.filter(Boolean)
];
const setupAfterEnv = join(path, "__tests__", "setup", "setupAfterEnv.js");
const setupAfterEnvExists = fs.existsSync(setupAfterEnv);
merged.setupFilesAfterEnv = [
"jest-extended/all",
join(__dirname, "jest.config.base.setup.js"),
setupAfterEnvExists ? setupAfterEnv : null,
...merged.setupFilesAfterEnv,
...presets.map(preset => preset.setupFilesAfterEnv || []).flat()
].filter(Boolean);
// IMPORTANT!
// We need to delete the following keys to let our rules be the only ones applied.
delete merged.transform["^.+\\.jsx?$"];
delete merged.transform["^.+\\.tsx?$"];
process.stdout.write(`Loading test setup files from the following packages:\n`);
merged.setupFilesAfterEnv.forEach(setupFile => {
const filePkg = findUp.sync("package.json", { cwd: dirname(setupFile) });
const pkg = require(filePkg);
if (pkg.name) {
process.stdout.write(`- ${pkg.name}\n`);
}
});
process.stdout.write(`\n---------------------------\n`);
return merged;
};
process.env.DB_TABLE = "DynamoDB";
process.env.DB_TABLE_ELASTICSEARCH = "ElasticsearchStream";
process.env.DB_TABLE_LOG = "DynamoDBLog";
process.env.WEBINY_VERSION = version;
process.env.WEBINY_ELASTICSEARCH_INDEX_LOCALE = "true";
const createGlobalSecondaryIndexesAttributeDefinitions = amount => {
const attributes = [];
for (let current = 1; current <= amount; current++) {
attributes.push({ AttributeName: `GSI${current}_PK`, AttributeType: "S" });
attributes.push({ AttributeName: `GSI${current}_SK`, AttributeType: "S" });
}
return attributes;
};
const createGlobalSecondaryIndexes = options => {
if (!options.amount) {
return [];
}
const indexes = [];
for (let current = 1; current <= options.amount; current++) {
indexes.push({
IndexName: `GSI${current}`,
KeySchema: [
{ AttributeName: `GSI${current}_PK`, KeyType: "HASH" },
{ AttributeName: `GSI${current}_SK`, KeyType: "RANGE" }
],
Projection: {
ProjectionType: "ALL"
},
ProvisionedThroughput: {
ReadCapacityUnits: 1,
WriteCapacityUnits: 1
}
});
}
return indexes;
};
const createDynaliteTables = (options = {}) => {
return {
tables: [
{
TableName: process.env.DB_TABLE,
KeySchema: [
{ AttributeName: "PK", KeyType: "HASH" },
{ AttributeName: "SK", KeyType: "RANGE" }
],
AttributeDefinitions: [
{ AttributeName: "PK", AttributeType: "S" },
{ AttributeName: "SK", AttributeType: "S" },
...createGlobalSecondaryIndexesAttributeDefinitions(2)
],
ProvisionedThroughput: { ReadCapacityUnits: 1, WriteCapacityUnits: 1 },
GlobalSecondaryIndexes: createGlobalSecondaryIndexes({
amount: 2
}),
data: options.data || []
},
{
TableName: process.env.DB_TABLE_ELASTICSEARCH,
KeySchema: [
{ AttributeName: "PK", KeyType: "HASH" },
{ AttributeName: "SK", KeyType: "RANGE" }
],
AttributeDefinitions: [
{ AttributeName: "PK", AttributeType: "S" },
{ AttributeName: "SK", AttributeType: "S" }
],
ProvisionedThroughput: { ReadCapacityUnits: 1, WriteCapacityUnits: 1 }
},
{
TableName: process.env.DB_TABLE_LOG,
KeySchema: [
{ AttributeName: "PK", KeyType: "HASH" },
{ AttributeName: "SK", KeyType: "RANGE" }
],
AttributeDefinitions: [
{ AttributeName: "PK", AttributeType: "S" },
{ AttributeName: "SK", AttributeType: "S" },
...createGlobalSecondaryIndexesAttributeDefinitions(5)
],
ProvisionedThroughput: { ReadCapacityUnits: 1, WriteCapacityUnits: 1 },
GlobalSecondaryIndexes: createGlobalSecondaryIndexes({
amount: 5
}),
data: options.data || []
}
],
basePort: 8000
};
};
module.exports.createDynaliteTables = createDynaliteTables;