@modern-js/plugin-testing 2.0.0-beta.2 → 2.0.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +145 -0
- package/dist/js/modern/base/config/index.js +29 -9
- package/dist/js/modern/base/config/patches/assetsModule.js +7 -7
- package/dist/js/modern/base/config/patches/filemock.js +4 -1
- package/dist/js/modern/base/config/patches/index.js +30 -7
- package/dist/js/modern/base/config/patches/transformer.js +13 -18
- package/dist/js/modern/base/config/resolver.js +11 -6
- package/dist/js/modern/base/config/testConfigOperator.js +29 -23
- package/dist/js/modern/base/config/transformer/babelTransformer.js +16 -8
- package/dist/js/modern/base/hook.js +10 -5
- package/dist/js/modern/base/index.js +14 -6
- package/dist/js/modern/base/runJest.js +73 -62
- package/dist/js/modern/base/utils.js +40 -23
- package/dist/js/modern/cli/bff/app.js +37 -12
- package/dist/js/modern/cli/bff/constant.js +4 -1
- package/dist/js/modern/cli/bff/index.js +95 -52
- package/dist/js/modern/cli/bff/mockAPI.js +79 -64
- package/dist/js/modern/cli/bff/setup.js +71 -34
- package/dist/js/modern/cli/bff/utils/index.js +31 -15
- package/dist/js/modern/cli/index.js +64 -30
- package/dist/js/modern/cli/test.js +53 -18
- package/dist/js/modern/constant.js +4 -1
- package/dist/js/modern/index.js +3 -3
- package/dist/js/modern/modern-app.env.d.js +0 -0
- package/dist/js/modern/runtime-testing/app.js +25 -14
- package/dist/js/modern/runtime-testing/base.js +7 -3
- package/dist/js/modern/runtime-testing/customRender.js +24 -10
- package/dist/js/modern/runtime-testing/index.js +1 -1
- package/dist/js/modern/runtime-testing/reduck.js +41 -19
- package/dist/js/modern/runtime-testing/request.js +4 -4
- package/dist/js/modern/runtime-testing/resolvePlugins.js +24 -8
- package/dist/js/node/base/config/index.js +56 -18
- package/dist/js/node/base/config/patches/assetsModule.js +30 -12
- package/dist/js/node/base/config/patches/filemock.js +22 -7
- package/dist/js/node/base/config/patches/index.js +50 -15
- package/dist/js/node/base/config/patches/transformer.js +38 -25
- package/dist/js/node/base/config/resolver.js +37 -10
- package/dist/js/node/base/config/testConfigOperator.js +49 -30
- package/dist/js/node/base/config/transformer/babelTransformer.js +40 -15
- package/dist/js/node/base/hook.js +28 -13
- package/dist/js/node/base/index.js +31 -63
- package/dist/js/node/base/runJest.js +99 -71
- package/dist/js/node/base/utils.js +66 -34
- package/dist/js/node/cli/bff/app.js +57 -22
- package/dist/js/node/cli/bff/constant.js +22 -7
- package/dist/js/node/cli/bff/index.js +121 -65
- package/dist/js/node/cli/bff/mockAPI.js +103 -71
- package/dist/js/node/cli/bff/setup.js +100 -38
- package/dist/js/node/cli/bff/utils/index.js +55 -22
- package/dist/js/node/cli/index.js +87 -46
- package/dist/js/node/cli/test.js +79 -27
- package/dist/js/node/constant.js +22 -7
- package/dist/js/node/index.js +19 -18
- package/dist/js/node/modern-app.env.d.js +0 -0
- package/dist/js/node/runtime-testing/app.js +53 -25
- package/dist/js/node/runtime-testing/base.js +30 -33
- package/dist/js/node/runtime-testing/customRender.js +49 -18
- package/dist/js/node/runtime-testing/index.js +18 -27
- package/dist/js/node/runtime-testing/reduck.js +67 -31
- package/dist/js/node/runtime-testing/request.js +32 -13
- package/dist/js/node/runtime-testing/resolvePlugins.js +41 -12
- package/dist/js/treeshaking/base/config/index.js +150 -31
- package/dist/js/treeshaking/base/config/patches/assetsModule.js +9 -11
- package/dist/js/treeshaking/base/config/patches/filemock.js +2 -1
- package/dist/js/treeshaking/base/config/patches/index.js +221 -64
- package/dist/js/treeshaking/base/config/patches/transformer.js +34 -37
- package/dist/js/treeshaking/base/config/resolver.js +24 -11
- package/dist/js/treeshaking/base/config/testConfigOperator.js +120 -69
- package/dist/js/treeshaking/base/config/transformer/babelTransformer.js +16 -10
- package/dist/js/treeshaking/base/hook.js +8 -7
- package/dist/js/treeshaking/base/index.js +7 -6
- package/dist/js/treeshaking/base/runJest.js +281 -164
- package/dist/js/treeshaking/base/types/index.js +1 -0
- package/dist/js/treeshaking/base/utils.js +72 -48
- package/dist/js/treeshaking/cli/bff/app.js +192 -68
- package/dist/js/treeshaking/cli/bff/constant.js +2 -1
- package/dist/js/treeshaking/cli/bff/index.js +291 -130
- package/dist/js/treeshaking/cli/bff/mockAPI.js +181 -91
- package/dist/js/treeshaking/cli/bff/setup.js +185 -59
- package/dist/js/treeshaking/cli/bff/utils/index.js +47 -21
- package/dist/js/treeshaking/cli/index.js +248 -108
- package/dist/js/treeshaking/cli/test.js +177 -44
- package/dist/js/treeshaking/constant.js +2 -1
- package/dist/js/treeshaking/index.js +3 -3
- package/dist/js/treeshaking/modern-app.env.d.js +1 -0
- package/dist/js/treeshaking/runtime-testing/app.js +85 -39
- package/dist/js/treeshaking/runtime-testing/base.js +4 -3
- package/dist/js/treeshaking/runtime-testing/customRender.js +38 -10
- package/dist/js/treeshaking/runtime-testing/index.js +1 -1
- package/dist/js/treeshaking/runtime-testing/reduck.js +85 -33
- package/dist/js/treeshaking/runtime-testing/request.js +49 -19
- package/dist/js/treeshaking/runtime-testing/resolvePlugins.js +45 -14
- package/dist/types/base/config/index.d.ts +1 -0
- package/dist/types/base/config/patches/assetsModule.d.ts +1 -0
- package/dist/types/base/config/patches/filemock.d.ts +1 -0
- package/dist/types/base/config/patches/transformer.d.ts +1 -0
- package/dist/types/base/config/resolver.d.ts +2 -1
- package/dist/types/base/config/testConfigOperator.d.ts +2 -0
- package/dist/types/base/runJest.d.ts +2 -0
- package/dist/types/base/utils.d.ts +1 -0
- package/dist/types/cli/bff/index.d.ts +2 -0
- package/dist/types/cli/bff/mockAPI.d.ts +1 -0
- package/dist/types/cli/index.d.ts +2 -0
- package/dist/types/runtime-testing/app.d.ts +3 -0
- package/dist/types/runtime-testing/customRender.d.ts +2 -0
- package/package.json +13 -13
|
@@ -1,17 +1,27 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
3
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
4
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __spreadValues = (a, b) => {
|
|
7
|
+
for (var prop in b || (b = {}))
|
|
8
|
+
if (__hasOwnProp.call(b, prop))
|
|
9
|
+
__defNormalProp(a, prop, b[prop]);
|
|
10
|
+
if (__getOwnPropSymbols)
|
|
11
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
12
|
+
if (__propIsEnum.call(b, prop))
|
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
|
14
|
+
}
|
|
15
|
+
return a;
|
|
16
|
+
};
|
|
17
|
+
import fs from "fs";
|
|
18
|
+
import path from "path";
|
|
19
|
+
import { createDebugger } from "@modern-js/utils";
|
|
20
|
+
const debug = createDebugger("test");
|
|
21
|
+
const readCompilerOptions = (pwd = process.cwd(), filename = "tsconfig.json") => {
|
|
12
22
|
let tsConfig = {};
|
|
13
23
|
let extendedCompilerOptions = {};
|
|
14
|
-
let tsconfigFile =
|
|
24
|
+
let tsconfigFile = "";
|
|
15
25
|
try {
|
|
16
26
|
const maybeTsconfigFile = path.join(pwd, filename);
|
|
17
27
|
if (fs.existsSync(maybeTsconfigFile)) {
|
|
@@ -19,23 +29,25 @@ export const readCompilerOptions = (pwd = process.cwd(), filename = 'tsconfig.js
|
|
|
19
29
|
} else {
|
|
20
30
|
tsconfigFile = require.resolve(filename);
|
|
21
31
|
}
|
|
22
|
-
({
|
|
23
|
-
|
|
24
|
-
|
|
32
|
+
({ config: tsConfig } = require("typescript").parseConfigFileTextToJson(
|
|
33
|
+
tsconfigFile,
|
|
34
|
+
fs.readFileSync(tsconfigFile, "utf8")
|
|
35
|
+
));
|
|
25
36
|
} catch (e) {
|
|
26
37
|
return {};
|
|
27
38
|
}
|
|
28
39
|
if (tsConfig.extends) {
|
|
29
|
-
extendedCompilerOptions = readCompilerOptions(
|
|
40
|
+
extendedCompilerOptions = readCompilerOptions(
|
|
41
|
+
path.dirname(tsconfigFile),
|
|
42
|
+
tsConfig.extends
|
|
43
|
+
);
|
|
30
44
|
}
|
|
31
|
-
return
|
|
45
|
+
return __spreadValues(__spreadValues({}, extendedCompilerOptions), tsConfig.compilerOptions);
|
|
32
46
|
};
|
|
33
|
-
|
|
47
|
+
const getModuleNameMapper = (alias) => Object.keys(alias).reduce((memo, cur) => {
|
|
34
48
|
const aliasValue = Array.isArray(alias[cur]) ? alias[cur] : [alias[cur]];
|
|
35
|
-
const isFile = aliasValue.some(s => s.endsWith(
|
|
36
|
-
|
|
37
|
-
// It's special for if using @modern-js/runtime alias other module @modern-js/runtime/model would not work.
|
|
38
|
-
if (cur === '@modern-js/runtime$') {
|
|
49
|
+
const isFile = aliasValue.some((s) => s.endsWith(".js"));
|
|
50
|
+
if (cur === "@modern-js/runtime$") {
|
|
39
51
|
memo[`.+${cur}`] = aliasValue[0];
|
|
40
52
|
return memo;
|
|
41
53
|
}
|
|
@@ -46,4 +58,9 @@ export const getModuleNameMapper = alias => Object.keys(alias).reduce((memo, cur
|
|
|
46
58
|
const value = path.normalize(`${aliasValue}/$1`);
|
|
47
59
|
memo[key] = value;
|
|
48
60
|
return memo;
|
|
49
|
-
}, {});
|
|
61
|
+
}, {});
|
|
62
|
+
export {
|
|
63
|
+
debug,
|
|
64
|
+
getModuleNameMapper,
|
|
65
|
+
readCompilerOptions
|
|
66
|
+
};
|
|
@@ -1,11 +1,31 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
22
|
+
import { Server } from "@modern-js/prod-server";
|
|
3
23
|
const store = new AsyncLocalStorage();
|
|
4
|
-
|
|
24
|
+
const isInHandler = () => Boolean(store.getStore());
|
|
5
25
|
let server = null;
|
|
6
|
-
const createApp =
|
|
26
|
+
const createApp = (pwd, config, plugins, routes) => __async(void 0, null, function* () {
|
|
7
27
|
if (!server) {
|
|
8
|
-
config.output.path =
|
|
28
|
+
config.output.path = "./";
|
|
9
29
|
server = new Server({
|
|
10
30
|
apiOnly: true,
|
|
11
31
|
pwd,
|
|
@@ -13,21 +33,26 @@ const createApp = async (pwd, config, plugins, routes) => {
|
|
|
13
33
|
internalPlugins: plugins,
|
|
14
34
|
routes
|
|
15
35
|
});
|
|
16
|
-
|
|
36
|
+
yield server.init();
|
|
17
37
|
}
|
|
18
38
|
const app = server.getRequestHandler();
|
|
19
39
|
return app;
|
|
20
|
-
};
|
|
40
|
+
});
|
|
21
41
|
const getApp = () => {
|
|
22
42
|
if (!server) {
|
|
23
|
-
throw new Error(
|
|
43
|
+
throw new Error("please createApp first");
|
|
24
44
|
}
|
|
25
45
|
return server.getRequestHandler();
|
|
26
46
|
};
|
|
27
|
-
const closeServer =
|
|
47
|
+
const closeServer = () => __async(void 0, null, function* () {
|
|
28
48
|
if (!server) {
|
|
29
|
-
throw new Error(
|
|
49
|
+
throw new Error("please createApp first");
|
|
30
50
|
}
|
|
31
|
-
|
|
51
|
+
yield server.close();
|
|
52
|
+
});
|
|
53
|
+
export {
|
|
54
|
+
closeServer,
|
|
55
|
+
createApp,
|
|
56
|
+
getApp,
|
|
57
|
+
isInHandler
|
|
32
58
|
};
|
|
33
|
-
export { createApp, getApp, closeServer };
|
|
@@ -1,26 +1,62 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
3
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
4
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __spreadValues = (a, b) => {
|
|
7
|
+
for (var prop in b || (b = {}))
|
|
8
|
+
if (__hasOwnProp.call(b, prop))
|
|
9
|
+
__defNormalProp(a, prop, b[prop]);
|
|
10
|
+
if (__getOwnPropSymbols)
|
|
11
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
12
|
+
if (__propIsEnum.call(b, prop))
|
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
|
14
|
+
}
|
|
15
|
+
return a;
|
|
16
|
+
};
|
|
17
|
+
var __async = (__this, __arguments, generator) => {
|
|
18
|
+
return new Promise((resolve, reject) => {
|
|
19
|
+
var fulfilled = (value) => {
|
|
20
|
+
try {
|
|
21
|
+
step(generator.next(value));
|
|
22
|
+
} catch (e) {
|
|
23
|
+
reject(e);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
var rejected = (value) => {
|
|
27
|
+
try {
|
|
28
|
+
step(generator.throw(value));
|
|
29
|
+
} catch (e) {
|
|
30
|
+
reject(e);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
34
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
import path from "path";
|
|
38
|
+
import { isApiOnly } from "@modern-js/utils";
|
|
39
|
+
import {
|
|
40
|
+
getModuleNameMapper,
|
|
41
|
+
DEFAULT_RESOLVER_PATH
|
|
42
|
+
} from "../../base";
|
|
7
43
|
import { bff_info_key } from "./constant";
|
|
8
44
|
import { isBFFProject } from "./utils";
|
|
9
|
-
|
|
45
|
+
const setJestConfigForBFF = (_0) => __async(void 0, [_0], function* ({
|
|
10
46
|
pwd,
|
|
11
47
|
userConfig,
|
|
12
48
|
plugins,
|
|
13
49
|
routes,
|
|
14
50
|
utils
|
|
15
|
-
})
|
|
16
|
-
var
|
|
51
|
+
}) {
|
|
52
|
+
var _a;
|
|
17
53
|
const bffConfig = {
|
|
18
|
-
rootDir: path.join(pwd,
|
|
54
|
+
rootDir: path.join(pwd, "./api"),
|
|
19
55
|
setupFilesAfterEnv: [require.resolve("./setup")],
|
|
20
|
-
testEnvironment:
|
|
56
|
+
testEnvironment: "node",
|
|
21
57
|
testMatch: [`**/api/**/*.test.[jt]s`],
|
|
22
58
|
globals: {
|
|
23
|
-
|
|
59
|
+
"ts-jest": {
|
|
24
60
|
diagnostics: {
|
|
25
61
|
warnOnly: true
|
|
26
62
|
}
|
|
@@ -33,25 +69,17 @@ export const setJestConfigForBFF = async ({
|
|
|
33
69
|
}
|
|
34
70
|
}
|
|
35
71
|
};
|
|
36
|
-
const {
|
|
37
|
-
|
|
38
|
-
} = utils;
|
|
39
|
-
const alias = (userConfig === null || userConfig === void 0 ? void 0 : (_userConfig$source = userConfig.source) === null || _userConfig$source === void 0 ? void 0 : _userConfig$source.alias) || {};
|
|
72
|
+
const { jestConfig } = utils;
|
|
73
|
+
const alias = ((_a = userConfig == null ? void 0 : userConfig.source) == null ? void 0 : _a.alias) || {};
|
|
40
74
|
const aliasMapper = getModuleNameMapper(alias);
|
|
41
|
-
const {
|
|
42
|
-
moduleNameMapper
|
|
43
|
-
} = jestConfig;
|
|
44
|
-
|
|
45
|
-
// 服务端统一使用 ts-jest
|
|
75
|
+
const { moduleNameMapper } = jestConfig;
|
|
46
76
|
const transform = {
|
|
47
|
-
|
|
77
|
+
"\\.[jt]sx?$": require.resolve("ts-jest")
|
|
48
78
|
};
|
|
49
|
-
const apiOnly =
|
|
50
|
-
const mergedModuleNameMapper =
|
|
79
|
+
const apiOnly = yield isApiOnly(pwd);
|
|
80
|
+
const mergedModuleNameMapper = __spreadValues(__spreadValues({}, moduleNameMapper), aliasMapper);
|
|
51
81
|
const resolver = jestConfig.resolver || DEFAULT_RESOLVER_PATH;
|
|
52
|
-
|
|
53
|
-
// 这三个配置不能设置在 projects 中,需要设置在外层(https://github.com/facebook/jest/issues/9696)
|
|
54
|
-
const configFields = ['coverage', 'collectCoverage', 'testTimeout'];
|
|
82
|
+
const configFields = ["coverage", "collectCoverage", "testTimeout"];
|
|
55
83
|
const commonConfig = configFields.reduce((obj, field) => {
|
|
56
84
|
if (jestConfig.hasOwnProperty(field)) {
|
|
57
85
|
obj[field] = jestConfig[field];
|
|
@@ -59,40 +87,51 @@ export const setJestConfigForBFF = async ({
|
|
|
59
87
|
return obj;
|
|
60
88
|
}, {});
|
|
61
89
|
if (!apiOnly) {
|
|
62
|
-
utils.setJestConfig(
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
90
|
+
utils.setJestConfig(
|
|
91
|
+
{
|
|
92
|
+
projects: [
|
|
93
|
+
__spreadValues({}, jestConfig),
|
|
94
|
+
__spreadValues({
|
|
95
|
+
transform,
|
|
96
|
+
moduleNameMapper: mergedModuleNameMapper,
|
|
97
|
+
resolver
|
|
98
|
+
}, bffConfig)
|
|
99
|
+
]
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
force: true
|
|
103
|
+
}
|
|
104
|
+
);
|
|
71
105
|
} else {
|
|
72
|
-
utils.setJestConfig(
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
106
|
+
utils.setJestConfig(
|
|
107
|
+
{
|
|
108
|
+
projects: [
|
|
109
|
+
__spreadValues({
|
|
110
|
+
transform,
|
|
111
|
+
moduleNameMapper: mergedModuleNameMapper,
|
|
112
|
+
resolver
|
|
113
|
+
}, bffConfig)
|
|
114
|
+
]
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
force: true
|
|
118
|
+
}
|
|
119
|
+
);
|
|
81
120
|
}
|
|
82
121
|
utils.setJestConfig(commonConfig);
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
name:
|
|
122
|
+
});
|
|
123
|
+
var bff_default = () => ({
|
|
124
|
+
name: "@modern-js/testing-plugin-bff",
|
|
86
125
|
setup(api) {
|
|
87
126
|
return {
|
|
88
|
-
jestConfig:
|
|
127
|
+
jestConfig: (utils, next) => __async(this, null, function* () {
|
|
89
128
|
const appContext = api.useAppContext();
|
|
90
129
|
const pwd = appContext.appDirectory;
|
|
91
130
|
if (!isBFFProject(pwd)) {
|
|
92
131
|
return next(utils);
|
|
93
132
|
}
|
|
94
133
|
const userConfig = api.useResolvedConfigContext();
|
|
95
|
-
|
|
134
|
+
yield setJestConfigForBFF({
|
|
96
135
|
pwd,
|
|
97
136
|
userConfig,
|
|
98
137
|
routes: appContext.serverRoutes,
|
|
@@ -100,7 +139,11 @@ export default (() => ({
|
|
|
100
139
|
utils
|
|
101
140
|
});
|
|
102
141
|
return next(utils);
|
|
103
|
-
}
|
|
142
|
+
})
|
|
104
143
|
};
|
|
105
144
|
}
|
|
106
|
-
})
|
|
145
|
+
});
|
|
146
|
+
export {
|
|
147
|
+
bff_default as default,
|
|
148
|
+
setJestConfigForBFF
|
|
149
|
+
};
|
|
@@ -1,10 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
3
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
4
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __spreadValues = (a, b) => {
|
|
7
|
+
for (var prop in b || (b = {}))
|
|
8
|
+
if (__hasOwnProp.call(b, prop))
|
|
9
|
+
__defNormalProp(a, prop, b[prop]);
|
|
10
|
+
if (__getOwnPropSymbols)
|
|
11
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
12
|
+
if (__propIsEnum.call(b, prop))
|
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
|
14
|
+
}
|
|
15
|
+
return a;
|
|
16
|
+
};
|
|
17
|
+
import * as ptr from "path-to-regexp";
|
|
5
18
|
import * as mockAppModule from "./app";
|
|
6
|
-
|
|
7
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
8
19
|
const mock_replaceUrlWithParams = (url, paramValues, payload) => {
|
|
9
20
|
const keys = [];
|
|
10
21
|
ptr.pathToRegexp(url, keys);
|
|
@@ -14,81 +25,85 @@ const mock_replaceUrlWithParams = (url, paramValues, payload) => {
|
|
|
14
25
|
}
|
|
15
26
|
return cur;
|
|
16
27
|
}, {});
|
|
17
|
-
const getFinalPath = ptr.compile(url, {
|
|
18
|
-
|
|
19
|
-
});
|
|
20
|
-
return getFinalPath(_objectSpread(_objectSpread({}, params), payload));
|
|
28
|
+
const getFinalPath = ptr.compile(url, { encode: encodeURIComponent });
|
|
29
|
+
return getFinalPath(__spreadValues(__spreadValues({}, params), payload));
|
|
21
30
|
};
|
|
22
|
-
|
|
23
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
24
|
-
const mock_getParamsAndPayload = args => {
|
|
31
|
+
const mock_getParamsAndPayload = (args) => {
|
|
25
32
|
if (args.length === 0) {
|
|
26
33
|
return [[], {}];
|
|
27
34
|
}
|
|
28
35
|
const head = args[0];
|
|
29
|
-
if (typeof head ===
|
|
36
|
+
if (typeof head === "object") {
|
|
30
37
|
return [[], head];
|
|
31
38
|
} else {
|
|
32
39
|
const latest = args[args.length - 1];
|
|
33
|
-
if (typeof latest ===
|
|
40
|
+
if (typeof latest === "object") {
|
|
34
41
|
return [args.slice(0, args.length - 1), latest];
|
|
35
42
|
} else {
|
|
36
43
|
return [args, {}];
|
|
37
44
|
}
|
|
38
45
|
}
|
|
39
46
|
};
|
|
40
|
-
|
|
47
|
+
var mockAPI_default = (mockApiInfosByFile) => {
|
|
41
48
|
const files = Object.keys(mockApiInfosByFile);
|
|
42
|
-
files.forEach(mockedFile => {
|
|
49
|
+
files.forEach((mockedFile) => {
|
|
43
50
|
jest.mock(mockedFile, () => {
|
|
44
|
-
const supertest = require(
|
|
45
|
-
return mockApiInfosByFile[mockedFile].reduce(
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
const [params, payload] = mock_getParamsAndPayload(args);
|
|
52
|
-
const {
|
|
53
|
-
returnHttp
|
|
54
|
-
} = module[info.name];
|
|
55
|
-
const url = mock_replaceUrlWithParams(info.routePath, params, payload.params);
|
|
56
|
-
const app = mockAppModule.getApp();
|
|
57
|
-
let test = supertest(app)[info.httpMethod.toLowerCase()](url);
|
|
58
|
-
if (payload.query) {
|
|
59
|
-
test = test.query(payload.query);
|
|
60
|
-
}
|
|
61
|
-
if (payload.body) {
|
|
62
|
-
test = test.send(payload.body);
|
|
63
|
-
}
|
|
64
|
-
if (payload.data) {
|
|
65
|
-
test = test.send(payload.data);
|
|
66
|
-
}
|
|
67
|
-
if (payload.headers) {
|
|
68
|
-
for (const name in payload.headers) {
|
|
69
|
-
test = test.set(name, payload.headers[name]);
|
|
51
|
+
const supertest = require("supertest");
|
|
52
|
+
return mockApiInfosByFile[mockedFile].reduce(
|
|
53
|
+
(res, info) => {
|
|
54
|
+
const module = {
|
|
55
|
+
[info.name]: (...args) => {
|
|
56
|
+
if (mockAppModule.isInHandler()) {
|
|
57
|
+
return info.handler(...args);
|
|
70
58
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
59
|
+
const [params, payload] = mock_getParamsAndPayload(args);
|
|
60
|
+
const { returnHttp } = module[info.name];
|
|
61
|
+
const url = mock_replaceUrlWithParams(
|
|
62
|
+
info.routePath,
|
|
63
|
+
params,
|
|
64
|
+
payload.params
|
|
65
|
+
);
|
|
66
|
+
const app = mockAppModule.getApp();
|
|
67
|
+
let test = supertest(app)[info.httpMethod.toLowerCase()](url);
|
|
68
|
+
if (payload.query) {
|
|
69
|
+
test = test.query(payload.query);
|
|
70
|
+
}
|
|
71
|
+
if (payload.body) {
|
|
72
|
+
test = test.send(payload.body);
|
|
73
|
+
}
|
|
74
|
+
if (payload.data) {
|
|
75
|
+
test = test.send(payload.data);
|
|
76
|
+
}
|
|
77
|
+
if (payload.headers) {
|
|
78
|
+
for (const name in payload.headers) {
|
|
79
|
+
test = test.set(name, payload.headers[name]);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (payload.cookies) {
|
|
83
|
+
test = test.set("Cookie", [payload.cookies]);
|
|
83
84
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
85
|
+
if (returnHttp) {
|
|
86
|
+
return test;
|
|
87
|
+
}
|
|
88
|
+
return test.then((value) => {
|
|
89
|
+
try {
|
|
90
|
+
return JSON.parse(value.text);
|
|
91
|
+
} catch (e) {
|
|
92
|
+
return value.text;
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
res[info.name] = module[info.name];
|
|
98
|
+
Object.assign(res[info.name], info.handler);
|
|
99
|
+
res.__esModule = true;
|
|
100
|
+
return res;
|
|
101
|
+
},
|
|
102
|
+
{}
|
|
103
|
+
);
|
|
92
104
|
});
|
|
93
105
|
});
|
|
94
|
-
}
|
|
106
|
+
};
|
|
107
|
+
export {
|
|
108
|
+
mockAPI_default as default
|
|
109
|
+
};
|
|
@@ -1,38 +1,75 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
2
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
3
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
4
|
+
};
|
|
5
|
+
var __async = (__this, __arguments, generator) => {
|
|
6
|
+
return new Promise((resolve, reject) => {
|
|
7
|
+
var fulfilled = (value) => {
|
|
8
|
+
try {
|
|
9
|
+
step(generator.next(value));
|
|
10
|
+
} catch (e) {
|
|
11
|
+
reject(e);
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
var rejected = (value) => {
|
|
15
|
+
try {
|
|
16
|
+
step(generator.throw(value));
|
|
17
|
+
} catch (e) {
|
|
18
|
+
reject(e);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
22
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
import path from "path";
|
|
26
|
+
import { ApiRouter } from "@modern-js/bff-core";
|
|
3
27
|
import { bff_info_key } from "./constant";
|
|
4
28
|
import mockAPI from "./mockAPI";
|
|
5
29
|
import { createApp, closeServer } from "./app";
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
30
|
+
var require_setup = __commonJS({
|
|
31
|
+
"src/cli/bff/setup.ts"(exports) {
|
|
32
|
+
let uped = false;
|
|
33
|
+
const setup = () => {
|
|
34
|
+
var _a, _b;
|
|
35
|
+
if (uped) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
uped = true;
|
|
39
|
+
const bff_info = global[bff_info_key];
|
|
40
|
+
const prefix = (_b = (_a = bff_info == null ? void 0 : bff_info.modernUserConfig) == null ? void 0 : _a.bff) == null ? void 0 : _b.prefix;
|
|
41
|
+
const apiRouter = new ApiRouter({
|
|
42
|
+
apiDir: path.join(bff_info.appDir, "./api"),
|
|
43
|
+
prefix
|
|
44
|
+
});
|
|
45
|
+
const apiInfos = apiRouter.getApiHandlers();
|
|
46
|
+
const apiInfosByFile = apiInfos.reduce(
|
|
47
|
+
(res, apiInfo) => {
|
|
48
|
+
if (!res[apiInfo.filename]) {
|
|
49
|
+
res[apiInfo.filename] = [];
|
|
50
|
+
}
|
|
51
|
+
res[apiInfo.filename].push(apiInfo);
|
|
52
|
+
return res;
|
|
53
|
+
},
|
|
54
|
+
{}
|
|
55
|
+
);
|
|
56
|
+
let app = null;
|
|
57
|
+
beforeAll(() => __async(exports, null, function* () {
|
|
58
|
+
if (!app) {
|
|
59
|
+
app = yield createApp(
|
|
60
|
+
bff_info.appDir,
|
|
61
|
+
bff_info.modernUserConfig,
|
|
62
|
+
bff_info.plugins,
|
|
63
|
+
bff_info.routes
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
}));
|
|
67
|
+
afterAll(() => __async(exports, null, function* () {
|
|
68
|
+
yield closeServer();
|
|
69
|
+
}));
|
|
70
|
+
mockAPI(apiInfosByFile);
|
|
71
|
+
};
|
|
72
|
+
setup();
|
|
11
73
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const prefix = bff_info === null || bff_info === void 0 ? void 0 : (_bff_info$modernUserC = bff_info.modernUserConfig) === null || _bff_info$modernUserC === void 0 ? void 0 : (_bff_info$modernUserC2 = _bff_info$modernUserC.bff) === null || _bff_info$modernUserC2 === void 0 ? void 0 : _bff_info$modernUserC2.prefix;
|
|
15
|
-
const apiRouter = new ApiRouter({
|
|
16
|
-
apiDir: path.join(bff_info.appDir, './api'),
|
|
17
|
-
prefix
|
|
18
|
-
});
|
|
19
|
-
const apiInfos = apiRouter.getApiHandlers();
|
|
20
|
-
const apiInfosByFile = apiInfos.reduce((res, apiInfo) => {
|
|
21
|
-
if (!res[apiInfo.filename]) {
|
|
22
|
-
res[apiInfo.filename] = [];
|
|
23
|
-
}
|
|
24
|
-
res[apiInfo.filename].push(apiInfo);
|
|
25
|
-
return res;
|
|
26
|
-
}, {});
|
|
27
|
-
let app = null;
|
|
28
|
-
beforeAll(async () => {
|
|
29
|
-
if (!app) {
|
|
30
|
-
app = await createApp(bff_info.appDir, bff_info.modernUserConfig, bff_info.plugins, bff_info.routes);
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
afterAll(async () => {
|
|
34
|
-
await closeServer();
|
|
35
|
-
});
|
|
36
|
-
mockAPI(apiInfosByFile);
|
|
37
|
-
};
|
|
38
|
-
setup();
|
|
74
|
+
});
|
|
75
|
+
export default require_setup();
|