@modern-js/plugin-testing 2.19.1 → 2.20.1-alpha.0

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.
Files changed (143) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/dist/cjs/base/config/resolver.js +2 -6
  3. package/dist/cjs/base/config/testConfigOperator.js +5 -17
  4. package/dist/cjs/base/config/transformer/babelTransformer.js +2 -6
  5. package/dist/cjs/base/index.js +2 -14
  6. package/dist/cjs/base/runJest.js +2 -6
  7. package/dist/cjs/base/utils.js +3 -7
  8. package/dist/cjs/cli/bff/index.js +5 -6
  9. package/dist/cjs/cli/bff/mockAPI.js +3 -42
  10. package/dist/cjs/cli/bff/setup.js +3 -7
  11. package/dist/cjs/cli/bff/utils/index.js +2 -6
  12. package/dist/cjs/cli/index.js +4 -8
  13. package/dist/cjs/cli/test.js +2 -6
  14. package/dist/cjs/index.js +2 -14
  15. package/dist/cjs/runtime-testing/app.js +5 -21
  16. package/dist/cjs/runtime-testing/base.js +4 -20
  17. package/dist/cjs/runtime-testing/customRender.js +3 -7
  18. package/dist/cjs/runtime-testing/index.js +3 -15
  19. package/dist/cjs/runtime-testing/reduck.js +4 -8
  20. package/dist/cjs/runtime-testing/request.js +2 -6
  21. package/dist/esm/base/config/index.js +3 -132
  22. package/dist/esm/base/config/patches/index.js +4 -133
  23. package/dist/esm/base/config/testConfigOperator.js +4 -50
  24. package/dist/esm/base/runJest.js +5 -134
  25. package/dist/esm/base/utils.js +1 -28
  26. package/dist/esm/cli/bff/app.js +4 -133
  27. package/dist/esm/cli/bff/index.js +9 -161
  28. package/dist/esm/cli/bff/mockAPI.js +4 -100
  29. package/dist/esm/cli/bff/setup.js +4 -133
  30. package/dist/esm/cli/bff/utils/index.js +1 -28
  31. package/dist/esm/cli/index.js +5 -146
  32. package/dist/esm/cli/test.js +3 -132
  33. package/dist/esm/runtime-testing/app.js +4 -50
  34. package/dist/esm/runtime-testing/customRender.js +1 -28
  35. package/dist/esm/runtime-testing/reduck.js +2 -52
  36. package/dist/esm/runtime-testing/request.js +2 -44
  37. package/dist/esm/runtime-testing/resolvePlugins.js +1 -28
  38. package/dist/esm-node/base/config/testConfigOperator.js +1 -13
  39. package/dist/esm-node/cli/bff/index.js +3 -0
  40. package/dist/esm-node/runtime-testing/app.js +1 -13
  41. package/dist/js/modern/base/config/index.js +36 -0
  42. package/dist/js/modern/base/config/patches/assetsModule.js +11 -0
  43. package/dist/js/modern/base/config/patches/filemock.js +4 -0
  44. package/dist/js/modern/base/config/patches/index.js +34 -0
  45. package/dist/js/modern/base/config/patches/transformer.js +38 -0
  46. package/dist/js/modern/base/config/resolver.js +21 -0
  47. package/dist/js/modern/base/config/testConfigOperator.js +69 -0
  48. package/dist/js/modern/base/config/transformer/babelTransformer.js +19 -0
  49. package/dist/js/modern/base/hook.js +12 -0
  50. package/dist/js/modern/base/index.js +14 -0
  51. package/dist/js/modern/base/runJest.js +102 -0
  52. package/dist/js/modern/base/types/index.js +0 -0
  53. package/dist/js/modern/base/utils.js +66 -0
  54. package/dist/js/modern/cli/bff/app.js +58 -0
  55. package/dist/js/modern/cli/bff/constant.js +4 -0
  56. package/dist/js/modern/cli/bff/index.js +149 -0
  57. package/dist/js/modern/cli/bff/mockAPI.js +109 -0
  58. package/dist/js/modern/cli/bff/setup.js +75 -0
  59. package/dist/js/modern/cli/bff/utils/index.js +37 -0
  60. package/dist/js/modern/cli/index.js +115 -0
  61. package/dist/js/modern/cli/test.js +61 -0
  62. package/dist/js/modern/constant.js +4 -0
  63. package/dist/js/modern/index.js +3 -0
  64. package/dist/js/modern/runtime-testing/app.js +48 -0
  65. package/dist/js/modern/runtime-testing/base.js +7 -0
  66. package/dist/js/modern/runtime-testing/customRender.js +25 -0
  67. package/dist/js/modern/runtime-testing/index.js +2 -0
  68. package/dist/js/modern/runtime-testing/reduck.js +55 -0
  69. package/dist/js/modern/runtime-testing/request.js +16 -0
  70. package/dist/js/modern/runtime-testing/resolvePlugins.js +33 -0
  71. package/dist/js/node/base/config/index.js +67 -0
  72. package/dist/js/node/base/config/patches/assetsModule.js +40 -0
  73. package/dist/js/node/base/config/patches/filemock.js +25 -0
  74. package/dist/js/node/base/config/patches/index.js +57 -0
  75. package/dist/js/node/base/config/patches/transformer.js +67 -0
  76. package/dist/js/node/base/config/resolver.js +30 -0
  77. package/dist/js/node/base/config/testConfigOperator.js +90 -0
  78. package/dist/js/node/base/config/transformer/babelTransformer.js +46 -0
  79. package/dist/js/node/base/hook.js +37 -0
  80. package/dist/js/node/base/index.js +43 -0
  81. package/dist/js/node/base/runJest.js +132 -0
  82. package/dist/js/node/base/types/index.js +15 -0
  83. package/dist/js/node/base/utils.js +95 -0
  84. package/dist/js/node/cli/bff/app.js +84 -0
  85. package/dist/js/node/cli/bff/constant.js +27 -0
  86. package/dist/js/node/cli/bff/index.js +173 -0
  87. package/dist/js/node/cli/bff/mockAPI.js +134 -0
  88. package/dist/js/node/cli/bff/setup.js +84 -0
  89. package/dist/js/node/cli/bff/utils/index.js +64 -0
  90. package/dist/js/node/cli/index.js +135 -0
  91. package/dist/js/node/cli/test.js +88 -0
  92. package/dist/js/node/constant.js +27 -0
  93. package/dist/js/node/index.js +19 -0
  94. package/dist/js/node/runtime-testing/app.js +73 -0
  95. package/dist/js/node/runtime-testing/base.js +38 -0
  96. package/dist/js/node/runtime-testing/customRender.js +50 -0
  97. package/dist/js/node/runtime-testing/index.js +18 -0
  98. package/dist/js/node/runtime-testing/reduck.js +85 -0
  99. package/dist/js/node/runtime-testing/request.js +45 -0
  100. package/dist/js/node/runtime-testing/resolvePlugins.js +52 -0
  101. package/dist/js/treeshaking/base/config/index.js +154 -0
  102. package/dist/js/treeshaking/base/config/patches/assetsModule.js +9 -0
  103. package/dist/js/treeshaking/base/config/patches/filemock.js +2 -0
  104. package/dist/js/treeshaking/base/config/patches/index.js +225 -0
  105. package/dist/js/treeshaking/base/config/patches/transformer.js +36 -0
  106. package/dist/js/treeshaking/base/config/resolver.js +37 -0
  107. package/dist/js/treeshaking/base/config/testConfigOperator.js +127 -0
  108. package/dist/js/treeshaking/base/config/transformer/babelTransformer.js +17 -0
  109. package/dist/js/treeshaking/base/hook.js +8 -0
  110. package/dist/js/treeshaking/base/index.js +7 -0
  111. package/dist/js/treeshaking/base/runJest.js +292 -0
  112. package/dist/js/treeshaking/base/types/index.js +1 -0
  113. package/dist/js/treeshaking/base/utils.js +76 -0
  114. package/dist/js/treeshaking/cli/bff/app.js +200 -0
  115. package/dist/js/treeshaking/cli/bff/constant.js +2 -0
  116. package/dist/js/treeshaking/cli/bff/index.js +297 -0
  117. package/dist/js/treeshaking/cli/bff/mockAPI.js +188 -0
  118. package/dist/js/treeshaking/cli/bff/setup.js +192 -0
  119. package/dist/js/treeshaking/cli/bff/utils/index.js +47 -0
  120. package/dist/js/treeshaking/cli/index.js +255 -0
  121. package/dist/js/treeshaking/cli/test.js +180 -0
  122. package/dist/js/treeshaking/constant.js +2 -0
  123. package/dist/js/treeshaking/index.js +3 -0
  124. package/dist/js/treeshaking/runtime-testing/app.js +90 -0
  125. package/dist/js/treeshaking/runtime-testing/base.js +4 -0
  126. package/dist/js/treeshaking/runtime-testing/customRender.js +41 -0
  127. package/dist/js/treeshaking/runtime-testing/index.js +2 -0
  128. package/dist/js/treeshaking/runtime-testing/reduck.js +89 -0
  129. package/dist/js/treeshaking/runtime-testing/request.js +51 -0
  130. package/dist/js/treeshaking/runtime-testing/resolvePlugins.js +46 -0
  131. package/dist/types/base/config/index.d.ts +0 -1
  132. package/dist/types/base/config/patches/assetsModule.d.ts +0 -1
  133. package/dist/types/base/config/patches/filemock.d.ts +0 -1
  134. package/dist/types/base/config/patches/transformer.d.ts +0 -1
  135. package/dist/types/base/config/testConfigOperator.d.ts +0 -2
  136. package/dist/types/base/runJest.d.ts +0 -2
  137. package/dist/types/base/utils.d.ts +0 -1
  138. package/dist/types/cli/bff/index.d.ts +0 -2
  139. package/dist/types/cli/bff/mockAPI.d.ts +0 -1
  140. package/dist/types/cli/index.d.ts +0 -2
  141. package/dist/types/runtime-testing/app.d.ts +0 -3
  142. package/dist/types/runtime-testing/customRender.d.ts +0 -2
  143. package/package.json +18 -18
@@ -0,0 +1,14 @@
1
+ import { runTest, runJest } from "./runJest";
2
+ import { getModuleNameMapper } from "./utils";
3
+ import { DEFAULT_RESOLVER_PATH } from "./config";
4
+ import { TestConfigOperator } from "./config/testConfigOperator";
5
+ import { testingHooks } from "./hook";
6
+ export * from "./types";
7
+ export {
8
+ DEFAULT_RESOLVER_PATH,
9
+ TestConfigOperator,
10
+ getModuleNameMapper,
11
+ runJest,
12
+ runTest,
13
+ testingHooks
14
+ };
@@ -0,0 +1,102 @@
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 yargs from "yargs/yargs";
22
+ import { runCLI } from "jest";
23
+ import { chalk } from "@modern-js/utils";
24
+ import { getJestUtils, patchConfig } from "./config";
25
+ import { debug } from "./utils";
26
+ const buildArgv = (rawArgv, config) => __async(void 0, null, function* () {
27
+ const argv = yield yargs(rawArgv).argv;
28
+ const result = {
29
+ $0: argv.$0,
30
+ _: argv._.slice(1)
31
+ };
32
+ Object.keys(argv).forEach((key) => {
33
+ if (key.includes("-") || key === "_") {
34
+ return;
35
+ }
36
+ result[key] = argv[key];
37
+ });
38
+ if (config) {
39
+ result.config = JSON.stringify(config);
40
+ }
41
+ return result;
42
+ });
43
+ const readResultsAndExit = (result, globalConfig) => {
44
+ const code = !result || result.success ? 0 : globalConfig.testFailureExitCode;
45
+ process.on("exit", () => {
46
+ if (typeof code === "number" && code !== 0) {
47
+ process.exitCode = code;
48
+ }
49
+ });
50
+ if (globalConfig.forceExit) {
51
+ if (!globalConfig.detectOpenHandles) {
52
+ console.warn(
53
+ `${chalk.bold(
54
+ "Force exiting Jest: "
55
+ )}Have you considered using \`--detectOpenHandles\` to detect async operations that kept running after all tests finished?`
56
+ );
57
+ }
58
+ process.exit(code);
59
+ } else if (!globalConfig.detectOpenHandles) {
60
+ setTimeout(() => {
61
+ console.warn(
62
+ chalk.yellow.bold(
63
+ "Jest did not exit one second after the test run has completed.\n\n"
64
+ ) + chalk.yellow(
65
+ "This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue."
66
+ )
67
+ );
68
+ }, 1e3).unref();
69
+ }
70
+ };
71
+ function runJest(_0) {
72
+ return __async(this, arguments, function* (config, pwd = process.cwd()) {
73
+ try {
74
+ const argvConfig = yield buildArgv(process.argv.slice(2), config);
75
+ const { results, globalConfig } = yield runCLI(argvConfig, [pwd]);
76
+ readResultsAndExit(results, globalConfig);
77
+ } catch (e) {
78
+ console.error(chalk.red((e == null ? void 0 : e.stack) || e));
79
+ process.exit(1);
80
+ }
81
+ });
82
+ }
83
+ function runTest(_0, _1) {
84
+ return __async(this, arguments, function* (api, config, pwd = process.cwd()) {
85
+ process.env.NODE_ENV = "test";
86
+ const jestUtils = getJestUtils(config);
87
+ yield patchConfig(jestUtils);
88
+ jestUtils.setJestUserConfig();
89
+ const hookRunners = api.useHookRunners();
90
+ const testConfigOperator = yield hookRunners.jestConfig(jestUtils, {
91
+ onLast: (input) => input
92
+ });
93
+ const finalConfig = testConfigOperator.getFinalConfig();
94
+ debug("Jest config:", finalConfig);
95
+ yield runJest(finalConfig, pwd);
96
+ yield hookRunners.afterTest();
97
+ });
98
+ }
99
+ export {
100
+ runJest,
101
+ runTest
102
+ };
File without changes
@@ -0,0 +1,66 @@
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") => {
22
+ let tsConfig = {};
23
+ let extendedCompilerOptions = {};
24
+ let tsconfigFile = "";
25
+ try {
26
+ const maybeTsconfigFile = path.join(pwd, filename);
27
+ if (fs.existsSync(maybeTsconfigFile)) {
28
+ tsconfigFile = maybeTsconfigFile;
29
+ } else {
30
+ tsconfigFile = require.resolve(filename);
31
+ }
32
+ ({ config: tsConfig } = require("typescript").parseConfigFileTextToJson(
33
+ tsconfigFile,
34
+ fs.readFileSync(tsconfigFile, "utf8")
35
+ ));
36
+ } catch (e) {
37
+ return {};
38
+ }
39
+ if (tsConfig.extends) {
40
+ extendedCompilerOptions = readCompilerOptions(
41
+ path.dirname(tsconfigFile),
42
+ tsConfig.extends
43
+ );
44
+ }
45
+ return __spreadValues(__spreadValues({}, extendedCompilerOptions), tsConfig.compilerOptions);
46
+ };
47
+ const getModuleNameMapper = (alias) => Object.keys(alias).reduce((memo, cur) => {
48
+ const aliasValue = Array.isArray(alias[cur]) ? alias[cur] : [alias[cur]];
49
+ const isFile = aliasValue.some((s) => s.endsWith(".js"));
50
+ if (cur === "@modern-js/runtime$") {
51
+ memo[`.+${cur}`] = aliasValue[0];
52
+ return memo;
53
+ }
54
+ if (isFile) {
55
+ memo[cur] = aliasValue[0];
56
+ }
57
+ const key = `^${cur}/(.*)$`;
58
+ const value = path.normalize(`${aliasValue}/$1`);
59
+ memo[key] = value;
60
+ return memo;
61
+ }, {});
62
+ export {
63
+ debug,
64
+ getModuleNameMapper,
65
+ readCompilerOptions
66
+ };
@@ -0,0 +1,58 @@
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";
23
+ const store = new AsyncLocalStorage();
24
+ const isInHandler = () => Boolean(store.getStore());
25
+ let server = null;
26
+ const createApp = (pwd, config, plugins, routes) => __async(void 0, null, function* () {
27
+ if (!server) {
28
+ config.output.path = "./";
29
+ server = new Server({
30
+ apiOnly: true,
31
+ pwd,
32
+ config,
33
+ internalPlugins: plugins,
34
+ routes
35
+ });
36
+ yield server.init();
37
+ }
38
+ const app = server.getRequestHandler();
39
+ return app;
40
+ });
41
+ const getApp = () => {
42
+ if (!server) {
43
+ throw new Error("please createApp first");
44
+ }
45
+ return server.getRequestHandler();
46
+ };
47
+ const closeServer = () => __async(void 0, null, function* () {
48
+ if (!server) {
49
+ throw new Error("please createApp first");
50
+ }
51
+ yield server.close();
52
+ });
53
+ export {
54
+ closeServer,
55
+ createApp,
56
+ getApp,
57
+ isInHandler
58
+ };
@@ -0,0 +1,4 @@
1
+ const bff_info_key = "modern_bff_info";
2
+ export {
3
+ bff_info_key
4
+ };
@@ -0,0 +1,149 @@
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";
43
+ import { bff_info_key } from "./constant";
44
+ import { isBFFProject } from "./utils";
45
+ const setJestConfigForBFF = (_0) => __async(void 0, [_0], function* ({
46
+ pwd,
47
+ userConfig,
48
+ plugins,
49
+ routes,
50
+ utils
51
+ }) {
52
+ var _a;
53
+ const bffConfig = {
54
+ rootDir: path.join(pwd, "./api"),
55
+ setupFilesAfterEnv: [require.resolve("./setup")],
56
+ testEnvironment: "node",
57
+ testMatch: [`**/api/**/*.test.[jt]s`],
58
+ globals: {
59
+ "ts-jest": {
60
+ diagnostics: {
61
+ warnOnly: true
62
+ }
63
+ },
64
+ [bff_info_key]: {
65
+ appDir: pwd,
66
+ modernUserConfig: userConfig,
67
+ plugins,
68
+ routes
69
+ }
70
+ }
71
+ };
72
+ const { jestConfig } = utils;
73
+ const alias = ((_a = userConfig == null ? void 0 : userConfig.source) == null ? void 0 : _a.alias) || {};
74
+ const aliasMapper = getModuleNameMapper(alias);
75
+ const { moduleNameMapper } = jestConfig;
76
+ const transform = {
77
+ "\\.[jt]sx?$": require.resolve("ts-jest")
78
+ };
79
+ const apiOnly = yield isApiOnly(pwd);
80
+ const mergedModuleNameMapper = __spreadValues(__spreadValues({}, moduleNameMapper), aliasMapper);
81
+ const resolver = jestConfig.resolver || DEFAULT_RESOLVER_PATH;
82
+ const configFields = ["coverage", "collectCoverage", "testTimeout"];
83
+ const commonConfig = configFields.reduce((obj, field) => {
84
+ if (jestConfig.hasOwnProperty(field)) {
85
+ obj[field] = jestConfig[field];
86
+ }
87
+ return obj;
88
+ }, {});
89
+ if (!apiOnly) {
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
+ );
105
+ } else {
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
+ );
120
+ }
121
+ utils.setJestConfig(commonConfig);
122
+ });
123
+ var bff_default = () => ({
124
+ name: "@modern-js/testing-plugin-bff",
125
+ setup(api) {
126
+ return {
127
+ jestConfig: (utils, next) => __async(this, null, function* () {
128
+ const appContext = api.useAppContext();
129
+ const pwd = appContext.appDirectory;
130
+ if (!isBFFProject(pwd)) {
131
+ return next(utils);
132
+ }
133
+ const userConfig = api.useResolvedConfigContext();
134
+ yield setJestConfigForBFF({
135
+ pwd,
136
+ userConfig,
137
+ routes: appContext.serverRoutes,
138
+ plugins: appContext.serverInternalPlugins,
139
+ utils
140
+ });
141
+ return next(utils);
142
+ })
143
+ };
144
+ }
145
+ });
146
+ export {
147
+ bff_default as default,
148
+ setJestConfigForBFF
149
+ };
@@ -0,0 +1,109 @@
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";
18
+ import * as mockAppModule from "./app";
19
+ const mock_replaceUrlWithParams = (url, paramValues, payload) => {
20
+ const keys = [];
21
+ ptr.pathToRegexp(url, keys);
22
+ const params = keys.reduce((cur, key, index) => {
23
+ if (paramValues[index]) {
24
+ cur[key.name] = paramValues[index];
25
+ }
26
+ return cur;
27
+ }, {});
28
+ const getFinalPath = ptr.compile(url, { encode: encodeURIComponent });
29
+ return getFinalPath(__spreadValues(__spreadValues({}, params), payload));
30
+ };
31
+ const mock_getParamsAndPayload = (args) => {
32
+ if (args.length === 0) {
33
+ return [[], {}];
34
+ }
35
+ const head = args[0];
36
+ if (typeof head === "object") {
37
+ return [[], head];
38
+ } else {
39
+ const latest = args[args.length - 1];
40
+ if (typeof latest === "object") {
41
+ return [args.slice(0, args.length - 1), latest];
42
+ } else {
43
+ return [args, {}];
44
+ }
45
+ }
46
+ };
47
+ var mockAPI_default = (mockApiInfosByFile) => {
48
+ const files = Object.keys(mockApiInfosByFile);
49
+ files.forEach((mockedFile) => {
50
+ jest.mock(mockedFile, () => {
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);
58
+ }
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]);
84
+ }
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
+ );
104
+ });
105
+ });
106
+ };
107
+ export {
108
+ mockAPI_default as default
109
+ };
@@ -0,0 +1,75 @@
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";
27
+ import { bff_info_key } from "./constant";
28
+ import mockAPI from "./mockAPI";
29
+ import { createApp, closeServer } from "./app";
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();
73
+ }
74
+ });
75
+ export default require_setup();
@@ -0,0 +1,37 @@
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 path from "path";
18
+ import { chalk } from "@modern-js/utils";
19
+ const isBFFProject = (pwd) => {
20
+ try {
21
+ const packageJson = require(path.join(pwd, "./package.json"));
22
+ const { dependencies, devDependencies } = packageJson;
23
+ const isBFF = Object.keys(__spreadValues(__spreadValues({}, dependencies), devDependencies)).some(
24
+ (dependency) => dependency.includes("plugin-bff")
25
+ );
26
+ const isMWA = Object.keys(devDependencies).some(
27
+ (devDependency) => devDependency.includes("app-tools")
28
+ );
29
+ return isMWA && isBFF;
30
+ } catch (error) {
31
+ console.log(chalk.red(error));
32
+ return false;
33
+ }
34
+ };
35
+ export {
36
+ isBFFProject
37
+ };