@modern-js/plugin-testing 2.0.0-beta.0 → 2.0.0-beta.2

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 (83) hide show
  1. package/CHANGELOG.md +109 -0
  2. package/dist/js/modern/base/config/index.js +1 -3
  3. package/dist/js/modern/base/config/patches/index.js +0 -2
  4. package/dist/js/modern/base/config/patches/transformer.js +1 -7
  5. package/dist/js/modern/base/config/resolver.js +0 -3
  6. package/dist/js/modern/base/config/testConfigOperator.js +0 -23
  7. package/dist/js/modern/base/config/transformer/babelTransformer.js +0 -1
  8. package/dist/js/modern/base/runJest.js +11 -14
  9. package/dist/js/modern/base/utils.js +2 -12
  10. package/dist/js/modern/cli/bff/app.js +0 -7
  11. package/dist/js/modern/cli/bff/index.js +4 -15
  12. package/dist/js/modern/cli/bff/mockAPI.js +4 -20
  13. package/dist/js/modern/cli/bff/setup.js +0 -5
  14. package/dist/js/modern/cli/bff/utils/index.js +0 -4
  15. package/dist/js/modern/cli/index.js +2 -9
  16. package/dist/js/modern/cli/test.js +0 -4
  17. package/dist/js/modern/runtime-testing/app.js +0 -15
  18. package/dist/js/modern/runtime-testing/customRender.js +0 -6
  19. package/dist/js/modern/runtime-testing/reduck.js +0 -10
  20. package/dist/js/modern/runtime-testing/request.js +1 -4
  21. package/dist/js/modern/runtime-testing/resolvePlugins.js +0 -6
  22. package/dist/js/node/base/config/index.js +0 -8
  23. package/dist/js/node/base/config/patches/assetsModule.js +0 -2
  24. package/dist/js/node/base/config/patches/index.js +0 -6
  25. package/dist/js/node/base/config/patches/transformer.js +1 -9
  26. package/dist/js/node/base/config/resolver.js +0 -5
  27. package/dist/js/node/base/config/testConfigOperator.js +0 -24
  28. package/dist/js/node/base/config/transformer/babelTransformer.js +0 -4
  29. package/dist/js/node/base/hook.js +0 -2
  30. package/dist/js/node/base/index.js +0 -7
  31. package/dist/js/node/base/runJest.js +11 -21
  32. package/dist/js/node/base/utils.js +2 -22
  33. package/dist/js/node/cli/bff/app.js +0 -14
  34. package/dist/js/node/cli/bff/index.js +4 -25
  35. package/dist/js/node/cli/bff/mockAPI.js +2 -25
  36. package/dist/js/node/cli/bff/setup.js +0 -11
  37. package/dist/js/node/cli/bff/utils/index.js +0 -9
  38. package/dist/js/node/cli/index.js +2 -21
  39. package/dist/js/node/cli/test.js +0 -10
  40. package/dist/js/node/index.js +0 -4
  41. package/dist/js/node/runtime-testing/app.js +0 -20
  42. package/dist/js/node/runtime-testing/base.js +0 -5
  43. package/dist/js/node/runtime-testing/customRender.js +0 -10
  44. package/dist/js/node/runtime-testing/index.js +0 -4
  45. package/dist/js/node/runtime-testing/reduck.js +0 -24
  46. package/dist/js/node/runtime-testing/request.js +1 -6
  47. package/dist/js/node/runtime-testing/resolvePlugins.js +0 -8
  48. package/dist/js/treeshaking/base/config/index.js +1 -6
  49. package/dist/js/treeshaking/base/config/patches/index.js +0 -17
  50. package/dist/js/treeshaking/base/config/patches/transformer.js +1 -7
  51. package/dist/js/treeshaking/base/config/resolver.js +0 -3
  52. package/dist/js/treeshaking/base/config/testConfigOperator.js +0 -15
  53. package/dist/js/treeshaking/base/config/transformer/babelTransformer.js +0 -1
  54. package/dist/js/treeshaking/base/runJest.js +19 -38
  55. package/dist/js/treeshaking/base/utils.js +2 -10
  56. package/dist/js/treeshaking/cli/bff/app.js +0 -13
  57. package/dist/js/treeshaking/cli/bff/index.js +0 -16
  58. package/dist/js/treeshaking/cli/bff/mockAPI.js +8 -24
  59. package/dist/js/treeshaking/cli/bff/setup.js +0 -9
  60. package/dist/js/treeshaking/cli/bff/utils/index.js +1 -2
  61. package/dist/js/treeshaking/cli/index.js +2 -11
  62. package/dist/js/treeshaking/cli/test.js +0 -7
  63. package/dist/js/treeshaking/runtime-testing/app.js +2 -15
  64. package/dist/js/treeshaking/runtime-testing/customRender.js +0 -3
  65. package/dist/js/treeshaking/runtime-testing/reduck.js +0 -7
  66. package/dist/js/treeshaking/runtime-testing/request.js +2 -7
  67. package/dist/js/treeshaking/runtime-testing/resolvePlugins.js +0 -3
  68. package/dist/types/base/config/index.d.ts +10 -1
  69. package/dist/types/base/config/patches/assetsModule.d.ts +0 -1
  70. package/dist/types/base/config/patches/filemock.d.ts +0 -1
  71. package/dist/types/base/config/patches/transformer.d.ts +0 -1
  72. package/dist/types/base/config/testConfigOperator.d.ts +1 -117
  73. package/dist/types/base/hook.d.ts +4 -6
  74. package/dist/types/base/runJest.d.ts +7 -3
  75. package/dist/types/base/utils.d.ts +0 -1
  76. package/dist/types/cli/bff/index.d.ts +7 -3
  77. package/dist/types/cli/bff/mockAPI.d.ts +0 -1
  78. package/dist/types/cli/index.d.ts +7 -3
  79. package/dist/types/cli/test.d.ts +7 -1
  80. package/dist/types/runtime-testing/app.d.ts +12 -5
  81. package/dist/types/runtime-testing/customRender.d.ts +0 -2
  82. package/dist/types/runtime-testing/resolvePlugins.d.ts +5 -1
  83. package/package.json +18 -18
package/CHANGELOG.md CHANGED
@@ -1,5 +1,114 @@
1
1
  # @modern-js/plugin-testing
2
2
 
3
+ ## 2.0.0-beta.2
4
+
5
+ ### Major Changes
6
+
7
+ - dda38c9c3e: chore: v2
8
+
9
+ ### Patch Changes
10
+
11
+ - cc971eabfc: refactor: move server plugin load logic in `@modern-js/core`
12
+ refactor:移除在 `@modern-js/core` 中的 server 插件加载逻辑
13
+ - 14b712da84: fix: use consistent alias type and default value across packages
14
+
15
+ fix: 在各个包中使用一致的 alias 类型定义和默认值
16
+
17
+ - Updated dependencies [2344eb2]
18
+ - Updated dependencies [a11fcf8]
19
+ - Updated dependencies [b18fa8f3ed]
20
+ - Updated dependencies [c9e800d39a]
21
+ - Updated dependencies [d032d49]
22
+ - Updated dependencies [15bf09d9c8]
23
+ - Updated dependencies [3e57f2b]
24
+ - Updated dependencies [fbf5eed]
25
+ - Updated dependencies [a2509bfbdb]
26
+ - Updated dependencies [decfcd989d]
27
+ - Updated dependencies [e4357f1]
28
+ - Updated dependencies [4369648ae2]
29
+ - Updated dependencies [92f0ead]
30
+ - Updated dependencies [92c0994]
31
+ - Updated dependencies [edd1cfb1af]
32
+ - Updated dependencies [cc971eabfc]
33
+ - Updated dependencies [5b9049f2e9]
34
+ - Updated dependencies [6bda14ed71]
35
+ - Updated dependencies [92004d1]
36
+ - Updated dependencies [b8bbe036c7]
37
+ - Updated dependencies [40ed587]
38
+ - Updated dependencies [60d5378632]
39
+ - Updated dependencies [d5a31df781]
40
+ - Updated dependencies [dda38c9c3e]
41
+ - Updated dependencies [102d32e4ba]
42
+ - Updated dependencies [8b8e1bb571]
43
+ - Updated dependencies [3bbea92b2a]
44
+ - Updated dependencies [73cd29dd9f]
45
+ - Updated dependencies [18aaf42]
46
+ - Updated dependencies [f179749]
47
+ - Updated dependencies [fcace5b5b9]
48
+ - Updated dependencies [abf3421a75]
49
+ - Updated dependencies [543be9558e]
50
+ - Updated dependencies [14b712da84]
51
+ - @modern-js/runtime@2.0.0-beta.2
52
+ - @modern-js/prod-server@2.0.0-beta.2
53
+ - @modern-js/babel-preset-app@2.0.0-beta.2
54
+ - @modern-js/utils@2.0.0-beta.2
55
+ - @modern-js/plugin@2.0.0-beta.2
56
+ - @modern-js/babel-compiler@2.0.0-beta.2
57
+
58
+ ## 2.0.0-beta.1
59
+
60
+ ### Major Changes
61
+
62
+ - dda38c9: chore: v2
63
+
64
+ ### Patch Changes
65
+
66
+ - cc971eabfc: refactor: move server plugin load logic in `@modern-js/core`
67
+ refactor:移除在 `@modern-js/core` 中的 server 插件加载逻辑
68
+ - 14b712d: fix: use consistent alias type and default value across packages
69
+
70
+ fix: 在各个包中使用一致的 alias 类型定义和默认值
71
+
72
+ - Updated dependencies [2344eb2]
73
+ - Updated dependencies [a11fcf8]
74
+ - Updated dependencies [b18fa8f]
75
+ - Updated dependencies [c9e800d39a]
76
+ - Updated dependencies [d032d49]
77
+ - Updated dependencies [15bf09d9c8]
78
+ - Updated dependencies [3e57f2b]
79
+ - Updated dependencies [fbf5eed]
80
+ - Updated dependencies [a2509bfbdb]
81
+ - Updated dependencies [decfcd989d]
82
+ - Updated dependencies [4369648ae2]
83
+ - Updated dependencies [92f0ead]
84
+ - Updated dependencies [92c0994]
85
+ - Updated dependencies [edd1cfb1af]
86
+ - Updated dependencies [cc971eabfc]
87
+ - Updated dependencies [5b9049f]
88
+ - Updated dependencies [6bda14ed71]
89
+ - Updated dependencies [92004d1]
90
+ - Updated dependencies [b8bbe036c7]
91
+ - Updated dependencies [40ed587]
92
+ - Updated dependencies [60d5378632]
93
+ - Updated dependencies [d5a31df781]
94
+ - Updated dependencies [dda38c9]
95
+ - Updated dependencies [102d32e4ba]
96
+ - Updated dependencies [8b8e1bb571]
97
+ - Updated dependencies [3bbea92b2a]
98
+ - Updated dependencies [73cd29dd9f]
99
+ - Updated dependencies [18aaf42]
100
+ - Updated dependencies [f179749]
101
+ - Updated dependencies [fcace5b5b9]
102
+ - Updated dependencies [abf3421]
103
+ - Updated dependencies [543be9558e]
104
+ - Updated dependencies [14b712d]
105
+ - @modern-js/runtime@2.0.0-beta.1
106
+ - @modern-js/prod-server@2.0.0-beta.1
107
+ - @modern-js/babel-preset-app@2.0.0-beta.1
108
+ - @modern-js/utils@2.0.0-beta.1
109
+ - @modern-js/plugin@2.0.0-beta.1
110
+ - @modern-js/babel-compiler@2.0.0-beta.1
111
+
3
112
  ## 2.0.0-beta.0
4
113
 
5
114
  ### Major Changes
@@ -1,18 +1,16 @@
1
1
  import { applyPatches } from "./patches";
2
2
  import { TestConfigOperator } from "./testConfigOperator";
3
+
3
4
  /**
4
5
  * Parse jest config
5
6
  */
6
-
7
7
  const getJestUtils = testConfig => {
8
8
  const testOperator = new TestConfigOperator(testConfig);
9
9
  return testOperator;
10
10
  };
11
-
12
11
  const patchConfig = async testOperator => {
13
12
  await applyPatches(testOperator);
14
13
  return testOperator.jestConfig;
15
14
  };
16
-
17
15
  export const DEFAULT_RESOLVER_PATH = require.resolve("./resolver");
18
16
  export { getJestUtils, patchConfig };
@@ -1,12 +1,10 @@
1
1
  import { patchTransformer } from "./transformer";
2
2
  import { patchAssetsModule } from "./assetsModule";
3
-
4
3
  const _applyPatches = async (patches, testOperator) => {
5
4
  for (const patch of patches) {
6
5
  await patch(testOperator);
7
6
  }
8
7
  };
9
-
10
8
  const patches = [patchTransformer, patchAssetsModule];
11
9
  export const applyPatches = async testConfig => {
12
10
  await _applyPatches(patches, testConfig);
@@ -1,28 +1,23 @@
1
1
  import { readCompilerOptions } from "../../utils";
2
-
3
2
  const resolveTsCompilerOptions = () => {
4
3
  const tsCompilerOptions = readCompilerOptions() || {};
5
4
  const {
6
5
  jsx
7
6
  } = tsCompilerOptions;
8
-
9
7
  if (!jsx) {
10
8
  return null;
11
9
  }
12
-
13
10
  tsCompilerOptions.jsx = 'react-jsx';
14
11
  return tsCompilerOptions;
15
12
  };
13
+
16
14
  /**
17
15
  * Map `TestConfig.transformer` to jest config
18
16
  */
19
-
20
-
21
17
  export const patchTransformer = testOperator => {
22
18
  const {
23
19
  transformer
24
20
  } = testOperator.testConfig;
25
-
26
21
  if (transformer === 'babel-jest') {
27
22
  testOperator.mergeJestConfig({
28
23
  transform: {
@@ -30,7 +25,6 @@ export const patchTransformer = testOperator => {
30
25
  }
31
26
  });
32
27
  }
33
-
34
28
  if (transformer === 'ts-jest') {
35
29
  testOperator.mergeJestConfig({
36
30
  transform: {
@@ -3,13 +3,10 @@ const resolver = enhanceResolve.create.sync({
3
3
  conditionNames: ['require', 'node', 'default'],
4
4
  extensions: ['.js', '.json', '.node', '.ts', '.tsx']
5
5
  });
6
-
7
6
  const shouldResolveByEnhance = url => /^@[^/]+\/[^/]+\/.*/.test(url);
8
-
9
7
  module.exports = function (request, options) {
10
8
  if (shouldResolveByEnhance(request)) {
11
9
  return resolver(options.basedir, request);
12
10
  }
13
-
14
11
  return options.defaultResolver(request, options);
15
12
  };
@@ -1,86 +1,63 @@
1
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
-
3
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; }
4
-
5
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; }
6
-
7
4
  import { merge } from '@modern-js/utils/lodash';
8
-
9
5
  class TestConfigOperator {
10
6
  constructor(testConfig) {
11
7
  _defineProperty(this, "_testConfig", void 0);
12
-
13
8
  _defineProperty(this, "_jestConfig", void 0);
14
-
15
9
  _defineProperty(this, "userJestConfig", void 0);
16
-
17
10
  _defineProperty(this, "defaultTestConfig", {
18
11
  transformer: 'babel-jest'
19
12
  });
20
-
21
13
  this._testConfig = testConfig;
22
14
  this._jestConfig = {};
23
15
  this.userJestConfig = testConfig.jest;
24
16
  this.initial();
25
17
  }
26
-
27
18
  initial() {
28
19
  this._testConfig = merge({}, this.defaultTestConfig, this.testConfig);
29
20
  }
30
-
31
21
  get jestConfig() {
32
22
  return this._jestConfig;
33
23
  }
34
-
35
24
  get testConfig() {
36
25
  return this._testConfig;
37
26
  }
38
-
39
27
  mergeJestConfig(sourceConfig) {
40
28
  this._jestConfig = merge({}, this._jestConfig, sourceConfig);
41
29
  }
42
-
43
30
  setJestUserConfig() {
44
31
  const {
45
32
  userJestConfig
46
33
  } = this;
47
-
48
34
  if (typeof userJestConfig === 'object') {
49
35
  this.setJestConfig(userJestConfig);
50
36
  }
51
37
  }
52
-
53
38
  setJestConfig(sourceConfig, options) {
54
39
  if (options) {
55
40
  const {
56
41
  force
57
42
  } = options;
58
-
59
43
  if (force) {
60
44
  this._jestConfig = sourceConfig;
61
45
  return;
62
46
  }
63
47
  }
64
-
65
48
  this._jestConfig = _objectSpread(_objectSpread({}, this._jestConfig), sourceConfig);
66
49
  }
67
-
68
50
  getFinalConfig() {
69
51
  const {
70
52
  userJestConfig
71
53
  } = this;
72
-
73
54
  if (!userJestConfig) {
74
55
  return this._jestConfig;
75
56
  }
76
-
77
57
  if (typeof userJestConfig === 'function') {
78
58
  return userJestConfig(this._jestConfig);
79
59
  }
80
-
81
60
  return this.jestConfig;
82
61
  }
83
-
84
62
  }
85
-
86
63
  export { TestConfigOperator };
@@ -1,5 +1,4 @@
1
1
  var _babelJest$createTran;
2
-
3
2
  import babelJest from 'babel-jest';
4
3
  const babelTransformer = (_babelJest$createTran = babelJest.createTransformer) === null || _babelJest$createTran === void 0 ? void 0 : _babelJest$createTran.call(babelJest, {
5
4
  presets: [[require.resolve('@modern-js/babel-preset-app'), {
@@ -5,12 +5,12 @@
5
5
  * The followed code is inspired by
6
6
  * https://github.com/facebook/jest/blob/fdc74af37235354e077edeeee8aa2d1a4a863032/packages/jest-cli/src/cli/index.ts#L21
7
7
  */
8
+
8
9
  import yargs from 'yargs/yargs';
9
10
  import { runCLI } from 'jest';
10
11
  import { chalk } from '@modern-js/utils';
11
12
  import { getJestUtils, patchConfig } from "./config";
12
13
  import { debug } from "./utils";
13
-
14
14
  const buildArgv = async (rawArgv, config) => {
15
15
  const argv = await yargs(rawArgv).argv;
16
16
  const result = {
@@ -21,32 +21,28 @@ const buildArgv = async (rawArgv, config) => {
21
21
  if (key.includes('-') || key === '_') {
22
22
  return;
23
23
  }
24
-
25
24
  result[key] = argv[key];
26
25
  });
27
-
28
26
  if (config) {
29
27
  result.config = JSON.stringify(config);
30
28
  }
31
-
32
29
  return result;
33
30
  };
34
-
35
31
  const readResultsAndExit = (result, globalConfig) => {
36
- const code = !result || result.success ? 0 : globalConfig.testFailureExitCode; // Only exit if needed
32
+ const code = !result || result.success ? 0 : globalConfig.testFailureExitCode;
37
33
 
34
+ // Only exit if needed
38
35
  process.on('exit', () => {
39
36
  if (typeof code === 'number' && code !== 0) {
40
37
  process.exitCode = code;
41
38
  }
42
39
  });
43
-
44
40
  if (globalConfig.forceExit) {
45
41
  if (!globalConfig.detectOpenHandles) {
46
42
  console.warn(`${chalk.bold('Force exiting Jest: ')}Have you considered using \`--detectOpenHandles\` to detect ` + `async operations that kept running after all tests finished?`);
47
- } // eslint-disable-next-line no-process-exit
48
-
43
+ }
49
44
 
45
+ // eslint-disable-next-line no-process-exit
50
46
  process.exit(code);
51
47
  } else if (!globalConfig.detectOpenHandles) {
52
48
  setTimeout(() => {
@@ -54,11 +50,10 @@ const readResultsAndExit = (result, globalConfig) => {
54
50
  }, 1000).unref();
55
51
  }
56
52
  };
53
+
57
54
  /**
58
55
  * Node API: execute jest
59
56
  */
60
-
61
-
62
57
  export async function runJest(config, pwd = process.cwd()) {
63
58
  try {
64
59
  const argvConfig = await buildArgv(process.argv.slice(2), config);
@@ -68,20 +63,22 @@ export async function runJest(config, pwd = process.cwd()) {
68
63
  } = await runCLI(argvConfig, [pwd]);
69
64
  readResultsAndExit(results, globalConfig);
70
65
  } catch (e) {
71
- console.error(chalk.red((e === null || e === void 0 ? void 0 : e.stack) || e)); // eslint-disable-next-line no-process-exit
66
+ console.error(chalk.red((e === null || e === void 0 ? void 0 : e.stack) || e));
72
67
 
68
+ // eslint-disable-next-line no-process-exit
73
69
  process.exit(1);
74
70
  }
75
71
  }
72
+
76
73
  /**
77
74
  * Node API: run test
78
75
  */
79
-
80
76
  export async function runTest(api, config, pwd = process.cwd()) {
81
77
  process.env.NODE_ENV = 'test';
82
78
  const jestUtils = getJestUtils(config);
83
- await patchConfig(jestUtils); // 确保用户设置的配置可以被插件处理,比如设置在 projects 中
79
+ await patchConfig(jestUtils);
84
80
 
81
+ // 确保用户设置的配置可以被插件处理,比如设置在 projects 中
85
82
  jestUtils.setJestUserConfig();
86
83
  const hookRunners = api.useHookRunners();
87
84
  const testConfigOperator = await hookRunners.jestConfig(jestUtils, {
@@ -1,14 +1,10 @@
1
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
-
3
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; }
4
-
5
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; }
6
-
7
4
  import fs from 'fs';
8
5
  import path from 'path';
9
6
  import { createDebugger } from '@modern-js/utils';
10
7
  export const debug = createDebugger('test');
11
-
12
8
  /**
13
9
  * Read `compilerOptions` in the current pwd's tsconfig.json file
14
10
  */
@@ -16,42 +12,36 @@ export const readCompilerOptions = (pwd = process.cwd(), filename = 'tsconfig.js
16
12
  let tsConfig = {};
17
13
  let extendedCompilerOptions = {};
18
14
  let tsconfigFile = '';
19
-
20
15
  try {
21
16
  const maybeTsconfigFile = path.join(pwd, filename);
22
-
23
17
  if (fs.existsSync(maybeTsconfigFile)) {
24
18
  tsconfigFile = maybeTsconfigFile;
25
19
  } else {
26
20
  tsconfigFile = require.resolve(filename);
27
21
  }
28
-
29
22
  ({
30
23
  config: tsConfig
31
24
  } = require('typescript').parseConfigFileTextToJson(tsconfigFile, fs.readFileSync(tsconfigFile, 'utf8')));
32
25
  } catch (e) {
33
26
  return {};
34
27
  }
35
-
36
28
  if (tsConfig.extends) {
37
29
  extendedCompilerOptions = readCompilerOptions(path.dirname(tsconfigFile), tsConfig.extends);
38
30
  }
39
-
40
31
  return _objectSpread(_objectSpread({}, extendedCompilerOptions), tsConfig.compilerOptions);
41
32
  };
42
33
  export const getModuleNameMapper = alias => Object.keys(alias).reduce((memo, cur) => {
43
34
  const aliasValue = Array.isArray(alias[cur]) ? alias[cur] : [alias[cur]];
44
- const isFile = aliasValue.some(s => s.endsWith('.js')); // It's special for if using @modern-js/runtime alias other module @modern-js/runtime/model would not work.
35
+ const isFile = aliasValue.some(s => s.endsWith('.js'));
45
36
 
37
+ // It's special for if using @modern-js/runtime alias other module @modern-js/runtime/model would not work.
46
38
  if (cur === '@modern-js/runtime$') {
47
39
  memo[`.+${cur}`] = aliasValue[0];
48
40
  return memo;
49
41
  }
50
-
51
42
  if (isFile) {
52
43
  memo[cur] = aliasValue[0];
53
44
  }
54
-
55
45
  const key = `^${cur}/(.*)$`;
56
46
  const value = path.normalize(`${aliasValue}/$1`);
57
47
  memo[key] = value;
@@ -3,7 +3,6 @@ import { Server } from '@modern-js/prod-server';
3
3
  const store = new AsyncLocalStorage();
4
4
  export const isInHandler = () => Boolean(store.getStore());
5
5
  let server = null;
6
-
7
6
  const createApp = async (pwd, config, plugins, routes) => {
8
7
  if (!server) {
9
8
  config.output.path = './';
@@ -16,25 +15,19 @@ const createApp = async (pwd, config, plugins, routes) => {
16
15
  });
17
16
  await server.init();
18
17
  }
19
-
20
18
  const app = server.getRequestHandler();
21
19
  return app;
22
20
  };
23
-
24
21
  const getApp = () => {
25
22
  if (!server) {
26
23
  throw new Error('please createApp first');
27
24
  }
28
-
29
25
  return server.getRequestHandler();
30
26
  };
31
-
32
27
  const closeServer = async () => {
33
28
  if (!server) {
34
29
  throw new Error('please createApp first');
35
30
  }
36
-
37
31
  await server.close();
38
32
  };
39
-
40
33
  export { createApp, getApp, closeServer };
@@ -1,9 +1,6 @@
1
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
-
3
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; }
4
-
5
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; }
6
-
7
4
  import path from 'path';
8
5
  import { isApiOnly } from '@modern-js/utils';
9
6
  import { getModuleNameMapper, DEFAULT_RESOLVER_PATH } from "../../base";
@@ -17,7 +14,6 @@ export const setJestConfigForBFF = async ({
17
14
  utils
18
15
  }) => {
19
16
  var _userConfig$source;
20
-
21
17
  const bffConfig = {
22
18
  rootDir: path.join(pwd, './api'),
23
19
  setupFilesAfterEnv: [require.resolve("./setup")],
@@ -44,26 +40,24 @@ export const setJestConfigForBFF = async ({
44
40
  const aliasMapper = getModuleNameMapper(alias);
45
41
  const {
46
42
  moduleNameMapper
47
- } = jestConfig; // 服务端统一使用 ts-jest
43
+ } = jestConfig;
48
44
 
45
+ // 服务端统一使用 ts-jest
49
46
  const transform = {
50
47
  '\\.[jt]sx?$': require.resolve('ts-jest')
51
48
  };
52
49
  const apiOnly = await isApiOnly(pwd);
53
-
54
50
  const mergedModuleNameMapper = _objectSpread(_objectSpread({}, moduleNameMapper), aliasMapper);
51
+ const resolver = jestConfig.resolver || DEFAULT_RESOLVER_PATH;
55
52
 
56
- const resolver = jestConfig.resolver || DEFAULT_RESOLVER_PATH; // 这三个配置不能设置在 projects 中,需要设置在外层(https://github.com/facebook/jest/issues/9696)
57
-
53
+ // 这三个配置不能设置在 projects 中,需要设置在外层(https://github.com/facebook/jest/issues/9696)
58
54
  const configFields = ['coverage', 'collectCoverage', 'testTimeout'];
59
55
  const commonConfig = configFields.reduce((obj, field) => {
60
56
  if (jestConfig.hasOwnProperty(field)) {
61
57
  obj[field] = jestConfig[field];
62
58
  }
63
-
64
59
  return obj;
65
60
  }, {});
66
-
67
61
  if (!apiOnly) {
68
62
  utils.setJestConfig({
69
63
  projects: [_objectSpread({}, jestConfig), _objectSpread({
@@ -85,22 +79,18 @@ export const setJestConfigForBFF = async ({
85
79
  force: true
86
80
  });
87
81
  }
88
-
89
82
  utils.setJestConfig(commonConfig);
90
83
  };
91
84
  export default (() => ({
92
85
  name: '@modern-js/testing-plugin-bff',
93
-
94
86
  setup(api) {
95
87
  return {
96
88
  jestConfig: async (utils, next) => {
97
89
  const appContext = api.useAppContext();
98
90
  const pwd = appContext.appDirectory;
99
-
100
91
  if (!isBFFProject(pwd)) {
101
92
  return next(utils);
102
93
  }
103
-
104
94
  const userConfig = api.useResolvedConfigContext();
105
95
  await setJestConfigForBFF({
106
96
  pwd,
@@ -113,5 +103,4 @@ export default (() => ({
113
103
  }
114
104
  };
115
105
  }
116
-
117
106
  }));
@@ -1,12 +1,10 @@
1
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
-
3
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; }
4
-
5
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; }
6
-
7
4
  import * as ptr from 'path-to-regexp';
8
- import * as mockAppModule from "./app"; // eslint-disable-next-line @typescript-eslint/naming-convention
5
+ import * as mockAppModule from "./app";
9
6
 
7
+ // eslint-disable-next-line @typescript-eslint/naming-convention
10
8
  const mock_replaceUrlWithParams = (url, paramValues, payload) => {
11
9
  const keys = [];
12
10
  ptr.pathToRegexp(url, keys);
@@ -14,28 +12,24 @@ const mock_replaceUrlWithParams = (url, paramValues, payload) => {
14
12
  if (paramValues[index]) {
15
13
  cur[key.name] = paramValues[index];
16
14
  }
17
-
18
15
  return cur;
19
16
  }, {});
20
17
  const getFinalPath = ptr.compile(url, {
21
18
  encode: encodeURIComponent
22
19
  });
23
20
  return getFinalPath(_objectSpread(_objectSpread({}, params), payload));
24
- }; // eslint-disable-next-line @typescript-eslint/naming-convention
25
-
21
+ };
26
22
 
23
+ // eslint-disable-next-line @typescript-eslint/naming-convention
27
24
  const mock_getParamsAndPayload = args => {
28
25
  if (args.length === 0) {
29
26
  return [[], {}];
30
27
  }
31
-
32
28
  const head = args[0];
33
-
34
29
  if (typeof head === 'object') {
35
30
  return [[], head];
36
31
  } else {
37
32
  const latest = args[args.length - 1];
38
-
39
33
  if (typeof latest === 'object') {
40
34
  return [args.slice(0, args.length - 1), latest];
41
35
  } else {
@@ -43,20 +37,17 @@ const mock_getParamsAndPayload = args => {
43
37
  }
44
38
  }
45
39
  };
46
-
47
40
  export default (mockApiInfosByFile => {
48
41
  const files = Object.keys(mockApiInfosByFile);
49
42
  files.forEach(mockedFile => {
50
43
  jest.mock(mockedFile, () => {
51
44
  const supertest = require('supertest');
52
-
53
45
  return mockApiInfosByFile[mockedFile].reduce((res, info) => {
54
46
  const module = {
55
47
  [info.name]: (...args) => {
56
48
  if (mockAppModule.isInHandler()) {
57
49
  return info.handler(...args);
58
50
  }
59
-
60
51
  const [params, payload] = mock_getParamsAndPayload(args);
61
52
  const {
62
53
  returnHttp
@@ -64,33 +55,26 @@ export default (mockApiInfosByFile => {
64
55
  const url = mock_replaceUrlWithParams(info.routePath, params, payload.params);
65
56
  const app = mockAppModule.getApp();
66
57
  let test = supertest(app)[info.httpMethod.toLowerCase()](url);
67
-
68
58
  if (payload.query) {
69
59
  test = test.query(payload.query);
70
60
  }
71
-
72
61
  if (payload.body) {
73
62
  test = test.send(payload.body);
74
63
  }
75
-
76
64
  if (payload.data) {
77
65
  test = test.send(payload.data);
78
66
  }
79
-
80
67
  if (payload.headers) {
81
68
  for (const name in payload.headers) {
82
69
  test = test.set(name, payload.headers[name]);
83
70
  }
84
71
  }
85
-
86
72
  if (payload.cookies) {
87
73
  test = test.set('Cookie', [payload.cookies]);
88
74
  }
89
-
90
75
  if (returnHttp) {
91
76
  return test;
92
77
  }
93
-
94
78
  return test.then(value => {
95
79
  try {
96
80
  return JSON.parse(value.text);
@@ -4,14 +4,11 @@ import { bff_info_key } from "./constant";
4
4
  import mockAPI from "./mockAPI";
5
5
  import { createApp, closeServer } from "./app";
6
6
  let uped = false;
7
-
8
7
  const setup = () => {
9
8
  var _bff_info$modernUserC, _bff_info$modernUserC2;
10
-
11
9
  if (uped) {
12
10
  return;
13
11
  }
14
-
15
12
  uped = true;
16
13
  const bff_info = global[bff_info_key];
17
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;
@@ -24,7 +21,6 @@ const setup = () => {
24
21
  if (!res[apiInfo.filename]) {
25
22
  res[apiInfo.filename] = [];
26
23
  }
27
-
28
24
  res[apiInfo.filename].push(apiInfo);
29
25
  return res;
30
26
  }, {});
@@ -39,5 +35,4 @@ const setup = () => {
39
35
  });
40
36
  mockAPI(apiInfosByFile);
41
37
  };
42
-
43
38
  setup();