@modern-js/plugin-testing 2.19.1 → 2.20.1-alpha.0
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +16 -0
- package/dist/cjs/base/config/resolver.js +2 -6
- package/dist/cjs/base/config/testConfigOperator.js +5 -17
- package/dist/cjs/base/config/transformer/babelTransformer.js +2 -6
- package/dist/cjs/base/index.js +2 -14
- package/dist/cjs/base/runJest.js +2 -6
- package/dist/cjs/base/utils.js +3 -7
- package/dist/cjs/cli/bff/index.js +5 -6
- package/dist/cjs/cli/bff/mockAPI.js +3 -42
- package/dist/cjs/cli/bff/setup.js +3 -7
- package/dist/cjs/cli/bff/utils/index.js +2 -6
- package/dist/cjs/cli/index.js +4 -8
- package/dist/cjs/cli/test.js +2 -6
- package/dist/cjs/index.js +2 -14
- package/dist/cjs/runtime-testing/app.js +5 -21
- package/dist/cjs/runtime-testing/base.js +4 -20
- package/dist/cjs/runtime-testing/customRender.js +3 -7
- package/dist/cjs/runtime-testing/index.js +3 -15
- package/dist/cjs/runtime-testing/reduck.js +4 -8
- package/dist/cjs/runtime-testing/request.js +2 -6
- package/dist/esm/base/config/index.js +3 -132
- package/dist/esm/base/config/patches/index.js +4 -133
- package/dist/esm/base/config/testConfigOperator.js +4 -50
- package/dist/esm/base/runJest.js +5 -134
- package/dist/esm/base/utils.js +1 -28
- package/dist/esm/cli/bff/app.js +4 -133
- package/dist/esm/cli/bff/index.js +9 -161
- package/dist/esm/cli/bff/mockAPI.js +4 -100
- package/dist/esm/cli/bff/setup.js +4 -133
- package/dist/esm/cli/bff/utils/index.js +1 -28
- package/dist/esm/cli/index.js +5 -146
- package/dist/esm/cli/test.js +3 -132
- package/dist/esm/runtime-testing/app.js +4 -50
- package/dist/esm/runtime-testing/customRender.js +1 -28
- package/dist/esm/runtime-testing/reduck.js +2 -52
- package/dist/esm/runtime-testing/request.js +2 -44
- package/dist/esm/runtime-testing/resolvePlugins.js +1 -28
- package/dist/esm-node/base/config/testConfigOperator.js +1 -13
- package/dist/esm-node/cli/bff/index.js +3 -0
- package/dist/esm-node/runtime-testing/app.js +1 -13
- package/dist/js/modern/base/config/index.js +36 -0
- package/dist/js/modern/base/config/patches/assetsModule.js +11 -0
- package/dist/js/modern/base/config/patches/filemock.js +4 -0
- package/dist/js/modern/base/config/patches/index.js +34 -0
- package/dist/js/modern/base/config/patches/transformer.js +38 -0
- package/dist/js/modern/base/config/resolver.js +21 -0
- package/dist/js/modern/base/config/testConfigOperator.js +69 -0
- package/dist/js/modern/base/config/transformer/babelTransformer.js +19 -0
- package/dist/js/modern/base/hook.js +12 -0
- package/dist/js/modern/base/index.js +14 -0
- package/dist/js/modern/base/runJest.js +102 -0
- package/dist/js/modern/base/types/index.js +0 -0
- package/dist/js/modern/base/utils.js +66 -0
- package/dist/js/modern/cli/bff/app.js +58 -0
- package/dist/js/modern/cli/bff/constant.js +4 -0
- package/dist/js/modern/cli/bff/index.js +149 -0
- package/dist/js/modern/cli/bff/mockAPI.js +109 -0
- package/dist/js/modern/cli/bff/setup.js +75 -0
- package/dist/js/modern/cli/bff/utils/index.js +37 -0
- package/dist/js/modern/cli/index.js +115 -0
- package/dist/js/modern/cli/test.js +61 -0
- package/dist/js/modern/constant.js +4 -0
- package/dist/js/modern/index.js +3 -0
- package/dist/js/modern/runtime-testing/app.js +48 -0
- package/dist/js/modern/runtime-testing/base.js +7 -0
- package/dist/js/modern/runtime-testing/customRender.js +25 -0
- package/dist/js/modern/runtime-testing/index.js +2 -0
- package/dist/js/modern/runtime-testing/reduck.js +55 -0
- package/dist/js/modern/runtime-testing/request.js +16 -0
- package/dist/js/modern/runtime-testing/resolvePlugins.js +33 -0
- package/dist/js/node/base/config/index.js +67 -0
- package/dist/js/node/base/config/patches/assetsModule.js +40 -0
- package/dist/js/node/base/config/patches/filemock.js +25 -0
- package/dist/js/node/base/config/patches/index.js +57 -0
- package/dist/js/node/base/config/patches/transformer.js +67 -0
- package/dist/js/node/base/config/resolver.js +30 -0
- package/dist/js/node/base/config/testConfigOperator.js +90 -0
- package/dist/js/node/base/config/transformer/babelTransformer.js +46 -0
- package/dist/js/node/base/hook.js +37 -0
- package/dist/js/node/base/index.js +43 -0
- package/dist/js/node/base/runJest.js +132 -0
- package/dist/js/node/base/types/index.js +15 -0
- package/dist/js/node/base/utils.js +95 -0
- package/dist/js/node/cli/bff/app.js +84 -0
- package/dist/js/node/cli/bff/constant.js +27 -0
- package/dist/js/node/cli/bff/index.js +173 -0
- package/dist/js/node/cli/bff/mockAPI.js +134 -0
- package/dist/js/node/cli/bff/setup.js +84 -0
- package/dist/js/node/cli/bff/utils/index.js +64 -0
- package/dist/js/node/cli/index.js +135 -0
- package/dist/js/node/cli/test.js +88 -0
- package/dist/js/node/constant.js +27 -0
- package/dist/js/node/index.js +19 -0
- package/dist/js/node/runtime-testing/app.js +73 -0
- package/dist/js/node/runtime-testing/base.js +38 -0
- package/dist/js/node/runtime-testing/customRender.js +50 -0
- package/dist/js/node/runtime-testing/index.js +18 -0
- package/dist/js/node/runtime-testing/reduck.js +85 -0
- package/dist/js/node/runtime-testing/request.js +45 -0
- package/dist/js/node/runtime-testing/resolvePlugins.js +52 -0
- package/dist/js/treeshaking/base/config/index.js +154 -0
- package/dist/js/treeshaking/base/config/patches/assetsModule.js +9 -0
- package/dist/js/treeshaking/base/config/patches/filemock.js +2 -0
- package/dist/js/treeshaking/base/config/patches/index.js +225 -0
- package/dist/js/treeshaking/base/config/patches/transformer.js +36 -0
- package/dist/js/treeshaking/base/config/resolver.js +37 -0
- package/dist/js/treeshaking/base/config/testConfigOperator.js +127 -0
- package/dist/js/treeshaking/base/config/transformer/babelTransformer.js +17 -0
- package/dist/js/treeshaking/base/hook.js +8 -0
- package/dist/js/treeshaking/base/index.js +7 -0
- package/dist/js/treeshaking/base/runJest.js +292 -0
- package/dist/js/treeshaking/base/types/index.js +1 -0
- package/dist/js/treeshaking/base/utils.js +76 -0
- package/dist/js/treeshaking/cli/bff/app.js +200 -0
- package/dist/js/treeshaking/cli/bff/constant.js +2 -0
- package/dist/js/treeshaking/cli/bff/index.js +297 -0
- package/dist/js/treeshaking/cli/bff/mockAPI.js +188 -0
- package/dist/js/treeshaking/cli/bff/setup.js +192 -0
- package/dist/js/treeshaking/cli/bff/utils/index.js +47 -0
- package/dist/js/treeshaking/cli/index.js +255 -0
- package/dist/js/treeshaking/cli/test.js +180 -0
- package/dist/js/treeshaking/constant.js +2 -0
- package/dist/js/treeshaking/index.js +3 -0
- package/dist/js/treeshaking/runtime-testing/app.js +90 -0
- package/dist/js/treeshaking/runtime-testing/base.js +4 -0
- package/dist/js/treeshaking/runtime-testing/customRender.js +41 -0
- package/dist/js/treeshaking/runtime-testing/index.js +2 -0
- package/dist/js/treeshaking/runtime-testing/reduck.js +89 -0
- package/dist/js/treeshaking/runtime-testing/request.js +51 -0
- package/dist/js/treeshaking/runtime-testing/resolvePlugins.js +46 -0
- package/dist/types/base/config/index.d.ts +0 -1
- package/dist/types/base/config/patches/assetsModule.d.ts +0 -1
- package/dist/types/base/config/patches/filemock.d.ts +0 -1
- package/dist/types/base/config/patches/transformer.d.ts +0 -1
- package/dist/types/base/config/testConfigOperator.d.ts +0 -2
- package/dist/types/base/runJest.d.ts +0 -2
- package/dist/types/base/utils.d.ts +0 -1
- package/dist/types/cli/bff/index.d.ts +0 -2
- package/dist/types/cli/bff/mockAPI.d.ts +0 -1
- package/dist/types/cli/index.d.ts +0 -2
- package/dist/types/runtime-testing/app.d.ts +0 -3
- package/dist/types/runtime-testing/customRender.d.ts +0 -2
- package/package.json +18 -18
@@ -0,0 +1,297 @@
|
|
1
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
2
|
+
try {
|
3
|
+
var info = gen[key](arg);
|
4
|
+
var value = info.value;
|
5
|
+
} catch (error) {
|
6
|
+
reject(error);
|
7
|
+
return;
|
8
|
+
}
|
9
|
+
if (info.done) {
|
10
|
+
resolve(value);
|
11
|
+
} else {
|
12
|
+
Promise.resolve(value).then(_next, _throw);
|
13
|
+
}
|
14
|
+
}
|
15
|
+
function _asyncToGenerator(fn) {
|
16
|
+
return function() {
|
17
|
+
var self = this, args = arguments;
|
18
|
+
return new Promise(function(resolve, reject) {
|
19
|
+
var gen = fn.apply(self, args);
|
20
|
+
function _next(value) {
|
21
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
22
|
+
}
|
23
|
+
function _throw(err) {
|
24
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
25
|
+
}
|
26
|
+
_next(undefined);
|
27
|
+
});
|
28
|
+
};
|
29
|
+
}
|
30
|
+
function _defineProperty(obj, key, value) {
|
31
|
+
if (key in obj) {
|
32
|
+
Object.defineProperty(obj, key, {
|
33
|
+
value: value,
|
34
|
+
enumerable: true,
|
35
|
+
configurable: true,
|
36
|
+
writable: true
|
37
|
+
});
|
38
|
+
} else {
|
39
|
+
obj[key] = value;
|
40
|
+
}
|
41
|
+
return obj;
|
42
|
+
}
|
43
|
+
function _objectSpread(target) {
|
44
|
+
for(var i = 1; i < arguments.length; i++){
|
45
|
+
var source = arguments[i] != null ? arguments[i] : {};
|
46
|
+
var ownKeys = Object.keys(source);
|
47
|
+
if (typeof Object.getOwnPropertySymbols === "function") {
|
48
|
+
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
|
49
|
+
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
|
50
|
+
}));
|
51
|
+
}
|
52
|
+
ownKeys.forEach(function(key) {
|
53
|
+
_defineProperty(target, key, source[key]);
|
54
|
+
});
|
55
|
+
}
|
56
|
+
return target;
|
57
|
+
}
|
58
|
+
var __generator = this && this.__generator || function(thisArg, body) {
|
59
|
+
var f, y, t, g, _ = {
|
60
|
+
label: 0,
|
61
|
+
sent: function() {
|
62
|
+
if (t[0] & 1) throw t[1];
|
63
|
+
return t[1];
|
64
|
+
},
|
65
|
+
trys: [],
|
66
|
+
ops: []
|
67
|
+
};
|
68
|
+
return(g = {
|
69
|
+
next: verb(0),
|
70
|
+
"throw": verb(1),
|
71
|
+
"return": verb(2)
|
72
|
+
}, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
73
|
+
return this;
|
74
|
+
}), g);
|
75
|
+
function verb(n) {
|
76
|
+
return function(v) {
|
77
|
+
return step([
|
78
|
+
n,
|
79
|
+
v
|
80
|
+
]);
|
81
|
+
};
|
82
|
+
}
|
83
|
+
function step(op) {
|
84
|
+
if (f) throw new TypeError("Generator is already executing.");
|
85
|
+
while(_)try {
|
86
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
87
|
+
if (y = 0, t) op = [
|
88
|
+
op[0] & 2,
|
89
|
+
t.value
|
90
|
+
];
|
91
|
+
switch(op[0]){
|
92
|
+
case 0:
|
93
|
+
case 1:
|
94
|
+
t = op;
|
95
|
+
break;
|
96
|
+
case 4:
|
97
|
+
_.label++;
|
98
|
+
return {
|
99
|
+
value: op[1],
|
100
|
+
done: false
|
101
|
+
};
|
102
|
+
case 5:
|
103
|
+
_.label++;
|
104
|
+
y = op[1];
|
105
|
+
op = [
|
106
|
+
0
|
107
|
+
];
|
108
|
+
continue;
|
109
|
+
case 7:
|
110
|
+
op = _.ops.pop();
|
111
|
+
_.trys.pop();
|
112
|
+
continue;
|
113
|
+
default:
|
114
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
115
|
+
_ = 0;
|
116
|
+
continue;
|
117
|
+
}
|
118
|
+
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
119
|
+
_.label = op[1];
|
120
|
+
break;
|
121
|
+
}
|
122
|
+
if (op[0] === 6 && _.label < t[1]) {
|
123
|
+
_.label = t[1];
|
124
|
+
t = op;
|
125
|
+
break;
|
126
|
+
}
|
127
|
+
if (t && _.label < t[2]) {
|
128
|
+
_.label = t[2];
|
129
|
+
_.ops.push(op);
|
130
|
+
break;
|
131
|
+
}
|
132
|
+
if (t[2]) _.ops.pop();
|
133
|
+
_.trys.pop();
|
134
|
+
continue;
|
135
|
+
}
|
136
|
+
op = body.call(thisArg, _);
|
137
|
+
} catch (e) {
|
138
|
+
op = [
|
139
|
+
6,
|
140
|
+
e
|
141
|
+
];
|
142
|
+
y = 0;
|
143
|
+
} finally{
|
144
|
+
f = t = 0;
|
145
|
+
}
|
146
|
+
if (op[0] & 5) throw op[1];
|
147
|
+
return {
|
148
|
+
value: op[0] ? op[1] : void 0,
|
149
|
+
done: true
|
150
|
+
};
|
151
|
+
}
|
152
|
+
};
|
153
|
+
import path from "path";
|
154
|
+
import { isApiOnly } from "@modern-js/utils";
|
155
|
+
import { getModuleNameMapper, DEFAULT_RESOLVER_PATH } from "../../base";
|
156
|
+
import { bff_info_key } from "./constant";
|
157
|
+
import { isBFFProject } from "./utils";
|
158
|
+
var setJestConfigForBFF = function() {
|
159
|
+
var _ref = _asyncToGenerator(function(param) {
|
160
|
+
var pwd, userConfig, plugins, routes, utils, ref, bffConfig, jestConfig, alias, aliasMapper, moduleNameMapper, transform, apiOnly, mergedModuleNameMapper, resolver, configFields, commonConfig;
|
161
|
+
return __generator(this, function(_state) {
|
162
|
+
switch(_state.label){
|
163
|
+
case 0:
|
164
|
+
pwd = param.pwd, userConfig = param.userConfig, plugins = param.plugins, routes = param.routes, utils = param.utils;
|
165
|
+
bffConfig = {
|
166
|
+
rootDir: path.join(pwd, "./api"),
|
167
|
+
setupFilesAfterEnv: [
|
168
|
+
require.resolve("./setup")
|
169
|
+
],
|
170
|
+
testEnvironment: "node",
|
171
|
+
testMatch: [
|
172
|
+
"**/api/**/*.test.[jt]s"
|
173
|
+
],
|
174
|
+
globals: _defineProperty({
|
175
|
+
"ts-jest": {
|
176
|
+
diagnostics: {
|
177
|
+
warnOnly: true
|
178
|
+
}
|
179
|
+
}
|
180
|
+
}, bff_info_key, {
|
181
|
+
appDir: pwd,
|
182
|
+
modernUserConfig: userConfig,
|
183
|
+
plugins: plugins,
|
184
|
+
routes: routes
|
185
|
+
})
|
186
|
+
};
|
187
|
+
jestConfig = utils.jestConfig;
|
188
|
+
alias = (userConfig === null || userConfig === void 0 ? void 0 : (ref = userConfig.source) === null || ref === void 0 ? void 0 : ref.alias) || {};
|
189
|
+
aliasMapper = getModuleNameMapper(alias);
|
190
|
+
moduleNameMapper = jestConfig.moduleNameMapper;
|
191
|
+
transform = {
|
192
|
+
"\\.[jt]sx?$": require.resolve("ts-jest")
|
193
|
+
};
|
194
|
+
return [
|
195
|
+
4,
|
196
|
+
isApiOnly(pwd)
|
197
|
+
];
|
198
|
+
case 1:
|
199
|
+
apiOnly = _state.sent();
|
200
|
+
mergedModuleNameMapper = _objectSpread({}, moduleNameMapper, aliasMapper);
|
201
|
+
resolver = jestConfig.resolver || DEFAULT_RESOLVER_PATH;
|
202
|
+
configFields = [
|
203
|
+
"coverage",
|
204
|
+
"collectCoverage",
|
205
|
+
"testTimeout"
|
206
|
+
];
|
207
|
+
commonConfig = configFields.reduce(function(obj, field) {
|
208
|
+
if (jestConfig.hasOwnProperty(field)) {
|
209
|
+
obj[field] = jestConfig[field];
|
210
|
+
}
|
211
|
+
return obj;
|
212
|
+
}, {});
|
213
|
+
if (!apiOnly) {
|
214
|
+
utils.setJestConfig({
|
215
|
+
projects: [
|
216
|
+
_objectSpread({}, jestConfig),
|
217
|
+
_objectSpread({
|
218
|
+
transform: transform,
|
219
|
+
moduleNameMapper: mergedModuleNameMapper,
|
220
|
+
resolver: resolver
|
221
|
+
}, bffConfig)
|
222
|
+
]
|
223
|
+
}, {
|
224
|
+
force: true
|
225
|
+
});
|
226
|
+
} else {
|
227
|
+
utils.setJestConfig({
|
228
|
+
projects: [
|
229
|
+
_objectSpread({
|
230
|
+
transform: transform,
|
231
|
+
moduleNameMapper: mergedModuleNameMapper,
|
232
|
+
resolver: resolver
|
233
|
+
}, bffConfig)
|
234
|
+
]
|
235
|
+
}, {
|
236
|
+
force: true
|
237
|
+
});
|
238
|
+
}
|
239
|
+
utils.setJestConfig(commonConfig);
|
240
|
+
return [
|
241
|
+
2
|
242
|
+
];
|
243
|
+
}
|
244
|
+
});
|
245
|
+
});
|
246
|
+
return function setJestConfigForBFF(_) {
|
247
|
+
return _ref.apply(this, arguments);
|
248
|
+
};
|
249
|
+
}();
|
250
|
+
var bff_default = function() {
|
251
|
+
return {
|
252
|
+
name: "@modern-js/testing-plugin-bff",
|
253
|
+
setup: function setup(api) {
|
254
|
+
return {
|
255
|
+
jestConfig: function() {
|
256
|
+
var _ref = _asyncToGenerator(function(utils, next) {
|
257
|
+
var appContext, pwd, userConfig;
|
258
|
+
return __generator(this, function(_state) {
|
259
|
+
switch(_state.label){
|
260
|
+
case 0:
|
261
|
+
appContext = api.useAppContext();
|
262
|
+
pwd = appContext.appDirectory;
|
263
|
+
if (!isBFFProject(pwd)) {
|
264
|
+
return [
|
265
|
+
2,
|
266
|
+
next(utils)
|
267
|
+
];
|
268
|
+
}
|
269
|
+
userConfig = api.useResolvedConfigContext();
|
270
|
+
return [
|
271
|
+
4,
|
272
|
+
setJestConfigForBFF({
|
273
|
+
pwd: pwd,
|
274
|
+
userConfig: userConfig,
|
275
|
+
routes: appContext.serverRoutes,
|
276
|
+
plugins: appContext.serverInternalPlugins,
|
277
|
+
utils: utils
|
278
|
+
})
|
279
|
+
];
|
280
|
+
case 1:
|
281
|
+
_state.sent();
|
282
|
+
return [
|
283
|
+
2,
|
284
|
+
next(utils)
|
285
|
+
];
|
286
|
+
}
|
287
|
+
});
|
288
|
+
});
|
289
|
+
return function(utils, next) {
|
290
|
+
return _ref.apply(this, arguments);
|
291
|
+
};
|
292
|
+
}()
|
293
|
+
};
|
294
|
+
}
|
295
|
+
};
|
296
|
+
};
|
297
|
+
export { bff_default as default, setJestConfigForBFF };
|
@@ -0,0 +1,188 @@
|
|
1
|
+
function _arrayLikeToArray(arr, len) {
|
2
|
+
if (len == null || len > arr.length) len = arr.length;
|
3
|
+
for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
|
4
|
+
return arr2;
|
5
|
+
}
|
6
|
+
function _arrayWithHoles(arr) {
|
7
|
+
if (Array.isArray(arr)) return arr;
|
8
|
+
}
|
9
|
+
function _arrayWithoutHoles(arr) {
|
10
|
+
if (Array.isArray(arr)) return _arrayLikeToArray(arr);
|
11
|
+
}
|
12
|
+
function _defineProperty(obj, key, value) {
|
13
|
+
if (key in obj) {
|
14
|
+
Object.defineProperty(obj, key, {
|
15
|
+
value: value,
|
16
|
+
enumerable: true,
|
17
|
+
configurable: true,
|
18
|
+
writable: true
|
19
|
+
});
|
20
|
+
} else {
|
21
|
+
obj[key] = value;
|
22
|
+
}
|
23
|
+
return obj;
|
24
|
+
}
|
25
|
+
function _iterableToArray(iter) {
|
26
|
+
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
|
27
|
+
}
|
28
|
+
function _iterableToArrayLimit(arr, i) {
|
29
|
+
var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
|
30
|
+
if (_i == null) return;
|
31
|
+
var _arr = [];
|
32
|
+
var _n = true;
|
33
|
+
var _d = false;
|
34
|
+
var _s, _e;
|
35
|
+
try {
|
36
|
+
for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
|
37
|
+
_arr.push(_s.value);
|
38
|
+
if (i && _arr.length === i) break;
|
39
|
+
}
|
40
|
+
} catch (err) {
|
41
|
+
_d = true;
|
42
|
+
_e = err;
|
43
|
+
} finally{
|
44
|
+
try {
|
45
|
+
if (!_n && _i["return"] != null) _i["return"]();
|
46
|
+
} finally{
|
47
|
+
if (_d) throw _e;
|
48
|
+
}
|
49
|
+
}
|
50
|
+
return _arr;
|
51
|
+
}
|
52
|
+
function _nonIterableRest() {
|
53
|
+
throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
54
|
+
}
|
55
|
+
function _nonIterableSpread() {
|
56
|
+
throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
57
|
+
}
|
58
|
+
function _objectSpread(target) {
|
59
|
+
for(var i = 1; i < arguments.length; i++){
|
60
|
+
var source = arguments[i] != null ? arguments[i] : {};
|
61
|
+
var ownKeys = Object.keys(source);
|
62
|
+
if (typeof Object.getOwnPropertySymbols === "function") {
|
63
|
+
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
|
64
|
+
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
|
65
|
+
}));
|
66
|
+
}
|
67
|
+
ownKeys.forEach(function(key) {
|
68
|
+
_defineProperty(target, key, source[key]);
|
69
|
+
});
|
70
|
+
}
|
71
|
+
return target;
|
72
|
+
}
|
73
|
+
function _slicedToArray(arr, i) {
|
74
|
+
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
|
75
|
+
}
|
76
|
+
function _toConsumableArray(arr) {
|
77
|
+
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
|
78
|
+
}
|
79
|
+
function _unsupportedIterableToArray(o, minLen) {
|
80
|
+
if (!o) return;
|
81
|
+
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
82
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
83
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
84
|
+
if (n === "Map" || n === "Set") return Array.from(n);
|
85
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
86
|
+
}
|
87
|
+
import * as ptr from "path-to-regexp";
|
88
|
+
import * as mockAppModule from "./app";
|
89
|
+
var mock_replaceUrlWithParams = function(url, paramValues, payload) {
|
90
|
+
var keys = [];
|
91
|
+
ptr.pathToRegexp(url, keys);
|
92
|
+
var params = keys.reduce(function(cur, key, index) {
|
93
|
+
if (paramValues[index]) {
|
94
|
+
cur[key.name] = paramValues[index];
|
95
|
+
}
|
96
|
+
return cur;
|
97
|
+
}, {});
|
98
|
+
var getFinalPath = ptr.compile(url, {
|
99
|
+
encode: encodeURIComponent
|
100
|
+
});
|
101
|
+
return getFinalPath(_objectSpread({}, params, payload));
|
102
|
+
};
|
103
|
+
var mock_getParamsAndPayload = function(args) {
|
104
|
+
if (args.length === 0) {
|
105
|
+
return [
|
106
|
+
[],
|
107
|
+
{}
|
108
|
+
];
|
109
|
+
}
|
110
|
+
var head = args[0];
|
111
|
+
if (typeof head === "object") {
|
112
|
+
return [
|
113
|
+
[],
|
114
|
+
head
|
115
|
+
];
|
116
|
+
} else {
|
117
|
+
var latest = args[args.length - 1];
|
118
|
+
if (typeof latest === "object") {
|
119
|
+
return [
|
120
|
+
args.slice(0, args.length - 1),
|
121
|
+
latest
|
122
|
+
];
|
123
|
+
} else {
|
124
|
+
return [
|
125
|
+
args,
|
126
|
+
{}
|
127
|
+
];
|
128
|
+
}
|
129
|
+
}
|
130
|
+
};
|
131
|
+
var mockAPI_default = function(mockApiInfosByFile) {
|
132
|
+
var files = Object.keys(mockApiInfosByFile);
|
133
|
+
files.forEach(function(mockedFile) {
|
134
|
+
jest.mock(mockedFile, function() {
|
135
|
+
var supertest = require("supertest");
|
136
|
+
return mockApiInfosByFile[mockedFile].reduce(function(res, info) {
|
137
|
+
var module = _defineProperty({}, info.name, function() {
|
138
|
+
for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
|
139
|
+
args[_key] = arguments[_key];
|
140
|
+
}
|
141
|
+
if (mockAppModule.isInHandler()) {
|
142
|
+
var _info;
|
143
|
+
return (_info = info).handler.apply(_info, _toConsumableArray(args));
|
144
|
+
}
|
145
|
+
var ref = _slicedToArray(mock_getParamsAndPayload(args), 2), params = ref[0], payload = ref[1];
|
146
|
+
var returnHttp = module[info.name].returnHttp;
|
147
|
+
var url = mock_replaceUrlWithParams(info.routePath, params, payload.params);
|
148
|
+
var app = mockAppModule.getApp();
|
149
|
+
var test = supertest(app)[info.httpMethod.toLowerCase()](url);
|
150
|
+
if (payload.query) {
|
151
|
+
test = test.query(payload.query);
|
152
|
+
}
|
153
|
+
if (payload.body) {
|
154
|
+
test = test.send(payload.body);
|
155
|
+
}
|
156
|
+
if (payload.data) {
|
157
|
+
test = test.send(payload.data);
|
158
|
+
}
|
159
|
+
if (payload.headers) {
|
160
|
+
for(var name in payload.headers){
|
161
|
+
test = test.set(name, payload.headers[name]);
|
162
|
+
}
|
163
|
+
}
|
164
|
+
if (payload.cookies) {
|
165
|
+
test = test.set("Cookie", [
|
166
|
+
payload.cookies
|
167
|
+
]);
|
168
|
+
}
|
169
|
+
if (returnHttp) {
|
170
|
+
return test;
|
171
|
+
}
|
172
|
+
return test.then(function(value) {
|
173
|
+
try {
|
174
|
+
return JSON.parse(value.text);
|
175
|
+
} catch (e) {
|
176
|
+
return value.text;
|
177
|
+
}
|
178
|
+
});
|
179
|
+
});
|
180
|
+
res[info.name] = module[info.name];
|
181
|
+
Object.assign(res[info.name], info.handler);
|
182
|
+
res.__esModule = true;
|
183
|
+
return res;
|
184
|
+
}, {});
|
185
|
+
});
|
186
|
+
});
|
187
|
+
};
|
188
|
+
export { mockAPI_default as default };
|
@@ -0,0 +1,192 @@
|
|
1
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
2
|
+
try {
|
3
|
+
var info = gen[key](arg);
|
4
|
+
var value = info.value;
|
5
|
+
} catch (error) {
|
6
|
+
reject(error);
|
7
|
+
return;
|
8
|
+
}
|
9
|
+
if (info.done) {
|
10
|
+
resolve(value);
|
11
|
+
} else {
|
12
|
+
Promise.resolve(value).then(_next, _throw);
|
13
|
+
}
|
14
|
+
}
|
15
|
+
function _asyncToGenerator(fn) {
|
16
|
+
return function() {
|
17
|
+
var self = this, args = arguments;
|
18
|
+
return new Promise(function(resolve, reject) {
|
19
|
+
var gen = fn.apply(self, args);
|
20
|
+
function _next(value) {
|
21
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
22
|
+
}
|
23
|
+
function _throw(err) {
|
24
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
25
|
+
}
|
26
|
+
_next(undefined);
|
27
|
+
});
|
28
|
+
};
|
29
|
+
}
|
30
|
+
var __generator = this && this.__generator || function(thisArg, body) {
|
31
|
+
var f, y, t, g, _ = {
|
32
|
+
label: 0,
|
33
|
+
sent: function() {
|
34
|
+
if (t[0] & 1) throw t[1];
|
35
|
+
return t[1];
|
36
|
+
},
|
37
|
+
trys: [],
|
38
|
+
ops: []
|
39
|
+
};
|
40
|
+
return g = {
|
41
|
+
next: verb(0),
|
42
|
+
"throw": verb(1),
|
43
|
+
"return": verb(2)
|
44
|
+
}, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
45
|
+
return this;
|
46
|
+
}), g;
|
47
|
+
function verb(n) {
|
48
|
+
return function(v) {
|
49
|
+
return step([
|
50
|
+
n,
|
51
|
+
v
|
52
|
+
]);
|
53
|
+
};
|
54
|
+
}
|
55
|
+
function step(op) {
|
56
|
+
if (f) throw new TypeError("Generator is already executing.");
|
57
|
+
while(_)try {
|
58
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
59
|
+
if (y = 0, t) op = [
|
60
|
+
op[0] & 2,
|
61
|
+
t.value
|
62
|
+
];
|
63
|
+
switch(op[0]){
|
64
|
+
case 0:
|
65
|
+
case 1:
|
66
|
+
t = op;
|
67
|
+
break;
|
68
|
+
case 4:
|
69
|
+
_.label++;
|
70
|
+
return {
|
71
|
+
value: op[1],
|
72
|
+
done: false
|
73
|
+
};
|
74
|
+
case 5:
|
75
|
+
_.label++;
|
76
|
+
y = op[1];
|
77
|
+
op = [
|
78
|
+
0
|
79
|
+
];
|
80
|
+
continue;
|
81
|
+
case 7:
|
82
|
+
op = _.ops.pop();
|
83
|
+
_.trys.pop();
|
84
|
+
continue;
|
85
|
+
default:
|
86
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
87
|
+
_ = 0;
|
88
|
+
continue;
|
89
|
+
}
|
90
|
+
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
91
|
+
_.label = op[1];
|
92
|
+
break;
|
93
|
+
}
|
94
|
+
if (op[0] === 6 && _.label < t[1]) {
|
95
|
+
_.label = t[1];
|
96
|
+
t = op;
|
97
|
+
break;
|
98
|
+
}
|
99
|
+
if (t && _.label < t[2]) {
|
100
|
+
_.label = t[2];
|
101
|
+
_.ops.push(op);
|
102
|
+
break;
|
103
|
+
}
|
104
|
+
if (t[2]) _.ops.pop();
|
105
|
+
_.trys.pop();
|
106
|
+
continue;
|
107
|
+
}
|
108
|
+
op = body.call(thisArg, _);
|
109
|
+
} catch (e) {
|
110
|
+
op = [
|
111
|
+
6,
|
112
|
+
e
|
113
|
+
];
|
114
|
+
y = 0;
|
115
|
+
} finally{
|
116
|
+
f = t = 0;
|
117
|
+
}
|
118
|
+
if (op[0] & 5) throw op[1];
|
119
|
+
return {
|
120
|
+
value: op[0] ? op[1] : void 0,
|
121
|
+
done: true
|
122
|
+
};
|
123
|
+
}
|
124
|
+
};
|
125
|
+
import path from "path";
|
126
|
+
import { ApiRouter } from "@modern-js/bff-core";
|
127
|
+
import { bff_info_key } from "./constant";
|
128
|
+
import mockAPI from "./mockAPI";
|
129
|
+
import { createApp, closeServer } from "./app";
|
130
|
+
var uped = false;
|
131
|
+
var setup = function() {
|
132
|
+
var ref, ref1;
|
133
|
+
if (uped) {
|
134
|
+
return;
|
135
|
+
}
|
136
|
+
uped = true;
|
137
|
+
var bff_info = global[bff_info_key];
|
138
|
+
var prefix = bff_info === null || bff_info === void 0 ? void 0 : (ref = bff_info.modernUserConfig) === null || ref === void 0 ? void 0 : (ref1 = ref.bff) === null || ref1 === void 0 ? void 0 : ref1.prefix;
|
139
|
+
var apiRouter = new ApiRouter({
|
140
|
+
apiDir: path.join(bff_info.appDir, "./api"),
|
141
|
+
prefix: prefix
|
142
|
+
});
|
143
|
+
var apiInfos = apiRouter.getApiHandlers();
|
144
|
+
var apiInfosByFile = apiInfos.reduce(function(res, apiInfo) {
|
145
|
+
if (!res[apiInfo.filename]) {
|
146
|
+
res[apiInfo.filename] = [];
|
147
|
+
}
|
148
|
+
res[apiInfo.filename].push(apiInfo);
|
149
|
+
return res;
|
150
|
+
}, {});
|
151
|
+
var app = null;
|
152
|
+
beforeAll(/*#__PURE__*/ _asyncToGenerator(function() {
|
153
|
+
return __generator(this, function(_state) {
|
154
|
+
switch(_state.label){
|
155
|
+
case 0:
|
156
|
+
if (!!app) return [
|
157
|
+
3,
|
158
|
+
2
|
159
|
+
];
|
160
|
+
return [
|
161
|
+
4,
|
162
|
+
createApp(bff_info.appDir, bff_info.modernUserConfig, bff_info.plugins, bff_info.routes)
|
163
|
+
];
|
164
|
+
case 1:
|
165
|
+
app = _state.sent();
|
166
|
+
_state.label = 2;
|
167
|
+
case 2:
|
168
|
+
return [
|
169
|
+
2
|
170
|
+
];
|
171
|
+
}
|
172
|
+
});
|
173
|
+
}));
|
174
|
+
afterAll(/*#__PURE__*/ _asyncToGenerator(function() {
|
175
|
+
return __generator(this, function(_state) {
|
176
|
+
switch(_state.label){
|
177
|
+
case 0:
|
178
|
+
return [
|
179
|
+
4,
|
180
|
+
closeServer()
|
181
|
+
];
|
182
|
+
case 1:
|
183
|
+
_state.sent();
|
184
|
+
return [
|
185
|
+
2
|
186
|
+
];
|
187
|
+
}
|
188
|
+
});
|
189
|
+
}));
|
190
|
+
mockAPI(apiInfosByFile);
|
191
|
+
};
|
192
|
+
setup();
|