@modern-js/plugin-testing 2.0.0-beta.3 → 2.0.0-beta.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (106) hide show
  1. package/CHANGELOG.md +159 -0
  2. package/dist/js/modern/base/config/index.js +29 -9
  3. package/dist/js/modern/base/config/patches/assetsModule.js +7 -7
  4. package/dist/js/modern/base/config/patches/filemock.js +4 -1
  5. package/dist/js/modern/base/config/patches/index.js +30 -7
  6. package/dist/js/modern/base/config/patches/transformer.js +13 -18
  7. package/dist/js/modern/base/config/resolver.js +20 -11
  8. package/dist/js/modern/base/config/testConfigOperator.js +29 -23
  9. package/dist/js/modern/base/config/transformer/babelTransformer.js +16 -8
  10. package/dist/js/modern/base/hook.js +10 -5
  11. package/dist/js/modern/base/index.js +14 -6
  12. package/dist/js/modern/base/runJest.js +73 -62
  13. package/dist/js/modern/base/utils.js +40 -23
  14. package/dist/js/modern/cli/bff/app.js +37 -12
  15. package/dist/js/modern/cli/bff/constant.js +4 -1
  16. package/dist/js/modern/cli/bff/index.js +95 -52
  17. package/dist/js/modern/cli/bff/mockAPI.js +79 -64
  18. package/dist/js/modern/cli/bff/setup.js +71 -34
  19. package/dist/js/modern/cli/bff/utils/index.js +31 -15
  20. package/dist/js/modern/cli/index.js +65 -30
  21. package/dist/js/modern/cli/test.js +53 -18
  22. package/dist/js/modern/constant.js +4 -1
  23. package/dist/js/modern/index.js +3 -3
  24. package/dist/js/modern/modern-app.env.d.js +0 -0
  25. package/dist/js/modern/runtime-testing/app.js +25 -14
  26. package/dist/js/modern/runtime-testing/base.js +7 -3
  27. package/dist/js/modern/runtime-testing/customRender.js +24 -10
  28. package/dist/js/modern/runtime-testing/index.js +1 -1
  29. package/dist/js/modern/runtime-testing/reduck.js +41 -19
  30. package/dist/js/modern/runtime-testing/request.js +4 -4
  31. package/dist/js/modern/runtime-testing/resolvePlugins.js +24 -8
  32. package/dist/js/node/base/config/index.js +62 -18
  33. package/dist/js/node/base/config/patches/assetsModule.js +34 -12
  34. package/dist/js/node/base/config/patches/filemock.js +24 -7
  35. package/dist/js/node/base/config/patches/index.js +54 -15
  36. package/dist/js/node/base/config/patches/transformer.js +42 -25
  37. package/dist/js/node/base/config/resolver.js +25 -10
  38. package/dist/js/node/base/config/testConfigOperator.js +51 -30
  39. package/dist/js/node/base/config/transformer/babelTransformer.js +42 -15
  40. package/dist/js/node/base/hook.js +34 -13
  41. package/dist/js/node/base/index.js +39 -62
  42. package/dist/js/node/base/runJest.js +104 -71
  43. package/dist/js/node/base/types/index.js +15 -0
  44. package/dist/js/node/base/utils.js +70 -34
  45. package/dist/js/node/cli/bff/app.js +64 -22
  46. package/dist/js/node/cli/bff/constant.js +26 -7
  47. package/dist/js/node/cli/bff/index.js +123 -65
  48. package/dist/js/node/cli/bff/mockAPI.js +103 -71
  49. package/dist/js/node/cli/bff/setup.js +71 -28
  50. package/dist/js/node/cli/bff/utils/index.js +57 -22
  51. package/dist/js/node/cli/index.js +92 -46
  52. package/dist/js/node/cli/test.js +81 -27
  53. package/dist/js/node/constant.js +26 -7
  54. package/dist/js/node/index.js +19 -18
  55. package/dist/js/node/modern-app.env.d.js +0 -0
  56. package/dist/js/node/runtime-testing/app.js +53 -25
  57. package/dist/js/node/runtime-testing/base.js +34 -32
  58. package/dist/js/node/runtime-testing/customRender.js +49 -18
  59. package/dist/js/node/runtime-testing/index.js +18 -27
  60. package/dist/js/node/runtime-testing/reduck.js +72 -31
  61. package/dist/js/node/runtime-testing/request.js +36 -13
  62. package/dist/js/node/runtime-testing/resolvePlugins.js +41 -12
  63. package/dist/js/treeshaking/base/config/index.js +150 -31
  64. package/dist/js/treeshaking/base/config/patches/assetsModule.js +9 -11
  65. package/dist/js/treeshaking/base/config/patches/filemock.js +2 -1
  66. package/dist/js/treeshaking/base/config/patches/index.js +221 -64
  67. package/dist/js/treeshaking/base/config/patches/transformer.js +34 -37
  68. package/dist/js/treeshaking/base/config/resolver.js +36 -13
  69. package/dist/js/treeshaking/base/config/testConfigOperator.js +120 -69
  70. package/dist/js/treeshaking/base/config/transformer/babelTransformer.js +16 -10
  71. package/dist/js/treeshaking/base/hook.js +8 -7
  72. package/dist/js/treeshaking/base/index.js +7 -6
  73. package/dist/js/treeshaking/base/runJest.js +281 -164
  74. package/dist/js/treeshaking/base/types/index.js +1 -0
  75. package/dist/js/treeshaking/base/utils.js +72 -48
  76. package/dist/js/treeshaking/cli/bff/app.js +192 -68
  77. package/dist/js/treeshaking/cli/bff/constant.js +2 -1
  78. package/dist/js/treeshaking/cli/bff/index.js +291 -130
  79. package/dist/js/treeshaking/cli/bff/mockAPI.js +181 -91
  80. package/dist/js/treeshaking/cli/bff/setup.js +185 -59
  81. package/dist/js/treeshaking/cli/bff/utils/index.js +47 -21
  82. package/dist/js/treeshaking/cli/index.js +249 -108
  83. package/dist/js/treeshaking/cli/test.js +177 -44
  84. package/dist/js/treeshaking/constant.js +2 -1
  85. package/dist/js/treeshaking/index.js +3 -3
  86. package/dist/js/treeshaking/modern-app.env.d.js +1 -0
  87. package/dist/js/treeshaking/runtime-testing/app.js +85 -39
  88. package/dist/js/treeshaking/runtime-testing/base.js +4 -3
  89. package/dist/js/treeshaking/runtime-testing/customRender.js +38 -10
  90. package/dist/js/treeshaking/runtime-testing/index.js +1 -1
  91. package/dist/js/treeshaking/runtime-testing/reduck.js +85 -33
  92. package/dist/js/treeshaking/runtime-testing/request.js +49 -19
  93. package/dist/js/treeshaking/runtime-testing/resolvePlugins.js +45 -14
  94. package/dist/types/base/config/index.d.ts +1 -0
  95. package/dist/types/base/config/patches/assetsModule.d.ts +1 -0
  96. package/dist/types/base/config/patches/filemock.d.ts +1 -0
  97. package/dist/types/base/config/patches/transformer.d.ts +1 -0
  98. package/dist/types/base/config/testConfigOperator.d.ts +2 -0
  99. package/dist/types/base/runJest.d.ts +2 -0
  100. package/dist/types/base/utils.d.ts +1 -0
  101. package/dist/types/cli/bff/index.d.ts +2 -0
  102. package/dist/types/cli/bff/mockAPI.d.ts +1 -0
  103. package/dist/types/cli/index.d.ts +2 -0
  104. package/dist/types/runtime-testing/app.d.ts +3 -0
  105. package/dist/types/runtime-testing/customRender.d.ts +2 -0
  106. package/package.json +21 -25
@@ -1,24 +1,36 @@
1
- /**
2
- * @file run jest by nodejs API
3
- * @description
4
- * Jest does not provide node API to run jest.
5
- * The followed code is inspired by
6
- * https://github.com/facebook/jest/blob/fdc74af37235354e077edeeee8aa2d1a4a863032/packages/jest-cli/src/cli/index.ts#L21
7
- */
8
-
9
- import yargs from 'yargs/yargs';
10
- import { runCLI } from 'jest';
11
- import { chalk } from '@modern-js/utils';
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";
12
24
  import { getJestUtils, patchConfig } from "./config";
13
25
  import { debug } from "./utils";
14
- const buildArgv = async (rawArgv, config) => {
15
- const argv = await yargs(rawArgv).argv;
26
+ const buildArgv = (rawArgv, config) => __async(void 0, null, function* () {
27
+ const argv = yield yargs(rawArgv).argv;
16
28
  const result = {
17
29
  $0: argv.$0,
18
30
  _: argv._.slice(1)
19
31
  };
20
- Object.keys(argv).forEach(key => {
21
- if (key.includes('-') || key === '_') {
32
+ Object.keys(argv).forEach((key) => {
33
+ if (key.includes("-") || key === "_") {
22
34
  return;
23
35
  }
24
36
  result[key] = argv[key];
@@ -27,65 +39,64 @@ const buildArgv = async (rawArgv, config) => {
27
39
  result.config = JSON.stringify(config);
28
40
  }
29
41
  return result;
30
- };
42
+ });
31
43
  const readResultsAndExit = (result, globalConfig) => {
32
44
  const code = !result || result.success ? 0 : globalConfig.testFailureExitCode;
33
-
34
- // Only exit if needed
35
- process.on('exit', () => {
36
- if (typeof code === 'number' && code !== 0) {
45
+ process.on("exit", () => {
46
+ if (typeof code === "number" && code !== 0) {
37
47
  process.exitCode = code;
38
48
  }
39
49
  });
40
50
  if (globalConfig.forceExit) {
41
51
  if (!globalConfig.detectOpenHandles) {
42
- console.warn(`${chalk.bold('Force exiting Jest: ')}Have you considered using \`--detectOpenHandles\` to detect ` + `async operations that kept running after all tests finished?`);
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
+ );
43
57
  }
44
-
45
- // eslint-disable-next-line no-process-exit
46
58
  process.exit(code);
47
59
  } else if (!globalConfig.detectOpenHandles) {
48
60
  setTimeout(() => {
49
- console.warn(chalk.yellow.bold('Jest did not exit one second after the test run has completed.\n\n') + chalk.yellow('This usually means that there are asynchronous operations that ' + "weren't stopped in your tests. Consider running Jest with " + '`--detectOpenHandles` to troubleshoot this issue.'));
50
- }, 1000).unref();
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();
51
69
  }
52
70
  };
53
-
54
- /**
55
- * Node API: execute jest
56
- */
57
- export async function runJest(config, pwd = process.cwd()) {
58
- try {
59
- const argvConfig = await buildArgv(process.argv.slice(2), config);
60
- const {
61
- results,
62
- globalConfig
63
- } = await runCLI(argvConfig, [pwd]);
64
- readResultsAndExit(results, globalConfig);
65
- } catch (e) {
66
- console.error(chalk.red((e === null || e === void 0 ? void 0 : e.stack) || e));
67
-
68
- // eslint-disable-next-line no-process-exit
69
- process.exit(1);
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
+ });
71
82
  }
72
-
73
- /**
74
- * Node API: run test
75
- */
76
- export async function runTest(api, config, pwd = process.cwd()) {
77
- process.env.NODE_ENV = 'test';
78
- const jestUtils = getJestUtils(config);
79
- await patchConfig(jestUtils);
80
-
81
- // 确保用户设置的配置可以被插件处理,比如设置在 projects 中
82
- jestUtils.setJestUserConfig();
83
- const hookRunners = api.useHookRunners();
84
- const testConfigOperator = await hookRunners.jestConfig(jestUtils, {
85
- onLast: input => input
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();
86
97
  });
87
- const finalConfig = testConfigOperator.getFinalConfig();
88
- debug('Jest config:', finalConfig);
89
- await runJest(finalConfig, pwd);
90
- await hookRunners.afterTest();
91
- }
98
+ }
99
+ export {
100
+ runJest,
101
+ runTest
102
+ };
@@ -1,17 +1,27 @@
1
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
2
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
3
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
4
- import fs from 'fs';
5
- import path from 'path';
6
- import { createDebugger } from '@modern-js/utils';
7
- export const debug = createDebugger('test');
8
- /**
9
- * Read `compilerOptions` in the current pwd's tsconfig.json file
10
- */
11
- export const readCompilerOptions = (pwd = process.cwd(), filename = 'tsconfig.json') => {
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
- config: tsConfig
24
- } = require('typescript').parseConfigFileTextToJson(tsconfigFile, fs.readFileSync(tsconfigFile, 'utf8')));
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(path.dirname(tsconfigFile), tsConfig.extends);
40
+ extendedCompilerOptions = readCompilerOptions(
41
+ path.dirname(tsconfigFile),
42
+ tsConfig.extends
43
+ );
30
44
  }
31
- return _objectSpread(_objectSpread({}, extendedCompilerOptions), tsConfig.compilerOptions);
45
+ return __spreadValues(__spreadValues({}, extendedCompilerOptions), tsConfig.compilerOptions);
32
46
  };
33
- export const getModuleNameMapper = alias => Object.keys(alias).reduce((memo, cur) => {
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('.js'));
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
- import { AsyncLocalStorage } from 'async_hooks';
2
- import { Server } from '@modern-js/prod-server';
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
- export const isInHandler = () => Boolean(store.getStore());
24
+ const isInHandler = () => Boolean(store.getStore());
5
25
  let server = null;
6
- const createApp = async (pwd, config, plugins, routes) => {
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
- await server.init();
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('please createApp first');
43
+ throw new Error("please createApp first");
24
44
  }
25
45
  return server.getRequestHandler();
26
46
  };
27
- const closeServer = async () => {
47
+ const closeServer = () => __async(void 0, null, function* () {
28
48
  if (!server) {
29
- throw new Error('please createApp first');
49
+ throw new Error("please createApp first");
30
50
  }
31
- await server.close();
51
+ yield server.close();
52
+ });
53
+ export {
54
+ closeServer,
55
+ createApp,
56
+ getApp,
57
+ isInHandler
32
58
  };
33
- export { createApp, getApp, closeServer };
@@ -1 +1,4 @@
1
- export const bff_info_key = 'modern_bff_info';
1
+ const bff_info_key = "modern_bff_info";
2
+ export {
3
+ bff_info_key
4
+ };
@@ -1,26 +1,62 @@
1
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
2
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
3
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
4
- import path from 'path';
5
- import { isApiOnly } from '@modern-js/utils';
6
- import { getModuleNameMapper, DEFAULT_RESOLVER_PATH } from "../../base";
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
- export const setJestConfigForBFF = async ({
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 _userConfig$source;
51
+ }) {
52
+ var _a;
17
53
  const bffConfig = {
18
- rootDir: path.join(pwd, './api'),
54
+ rootDir: path.join(pwd, "./api"),
19
55
  setupFilesAfterEnv: [require.resolve("./setup")],
20
- testEnvironment: 'node',
56
+ testEnvironment: "node",
21
57
  testMatch: [`**/api/**/*.test.[jt]s`],
22
58
  globals: {
23
- 'ts-jest': {
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
- jestConfig
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
- '\\.[jt]sx?$': require.resolve('ts-jest')
77
+ "\\.[jt]sx?$": require.resolve("ts-jest")
48
78
  };
49
- const apiOnly = await isApiOnly(pwd);
50
- const mergedModuleNameMapper = _objectSpread(_objectSpread({}, moduleNameMapper), aliasMapper);
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
- projects: [_objectSpread({}, jestConfig), _objectSpread({
64
- transform,
65
- moduleNameMapper: mergedModuleNameMapper,
66
- resolver
67
- }, bffConfig)]
68
- }, {
69
- force: true
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
- projects: [_objectSpread({
74
- transform,
75
- moduleNameMapper: mergedModuleNameMapper,
76
- resolver
77
- }, bffConfig)]
78
- }, {
79
- force: true
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
- export default (() => ({
85
- name: '@modern-js/testing-plugin-bff',
122
+ });
123
+ var bff_default = () => ({
124
+ name: "@modern-js/testing-plugin-bff",
86
125
  setup(api) {
87
126
  return {
88
- jestConfig: async (utils, next) => {
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
- await setJestConfigForBFF({
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
+ };