@modern-js/plugin-testing 0.0.0-next-20221207091327 → 0.0.0-next-20221207112009

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 (105) hide show
  1. package/CHANGELOG.md +55 -54
  2. package/dist/js/modern/base/config/index.js +9 -29
  3. package/dist/js/modern/base/config/patches/assetsModule.js +7 -7
  4. package/dist/js/modern/base/config/patches/filemock.js +1 -4
  5. package/dist/js/modern/base/config/patches/index.js +7 -30
  6. package/dist/js/modern/base/config/patches/transformer.js +18 -13
  7. package/dist/js/modern/base/config/resolver.js +11 -20
  8. package/dist/js/modern/base/config/testConfigOperator.js +23 -29
  9. package/dist/js/modern/base/config/transformer/babelTransformer.js +8 -16
  10. package/dist/js/modern/base/hook.js +5 -10
  11. package/dist/js/modern/base/index.js +6 -14
  12. package/dist/js/modern/base/runJest.js +62 -73
  13. package/dist/js/modern/base/utils.js +23 -40
  14. package/dist/js/modern/cli/bff/app.js +12 -37
  15. package/dist/js/modern/cli/bff/constant.js +1 -4
  16. package/dist/js/modern/cli/bff/index.js +52 -95
  17. package/dist/js/modern/cli/bff/mockAPI.js +64 -79
  18. package/dist/js/modern/cli/bff/setup.js +34 -71
  19. package/dist/js/modern/cli/bff/utils/index.js +15 -31
  20. package/dist/js/modern/cli/index.js +30 -65
  21. package/dist/js/modern/cli/test.js +16 -50
  22. package/dist/js/modern/constant.js +1 -4
  23. package/dist/js/modern/index.js +3 -3
  24. package/dist/js/modern/runtime-testing/app.js +14 -25
  25. package/dist/js/modern/runtime-testing/base.js +3 -7
  26. package/dist/js/modern/runtime-testing/customRender.js +10 -24
  27. package/dist/js/modern/runtime-testing/index.js +1 -1
  28. package/dist/js/modern/runtime-testing/reduck.js +19 -41
  29. package/dist/js/modern/runtime-testing/request.js +4 -4
  30. package/dist/js/modern/runtime-testing/resolvePlugins.js +8 -24
  31. package/dist/js/node/base/config/index.js +18 -56
  32. package/dist/js/node/base/config/patches/assetsModule.js +12 -30
  33. package/dist/js/node/base/config/patches/filemock.js +7 -22
  34. package/dist/js/node/base/config/patches/index.js +15 -50
  35. package/dist/js/node/base/config/patches/transformer.js +25 -38
  36. package/dist/js/node/base/config/resolver.js +13 -47
  37. package/dist/js/node/base/config/testConfigOperator.js +30 -49
  38. package/dist/js/node/base/config/transformer/babelTransformer.js +15 -40
  39. package/dist/js/node/base/hook.js +13 -28
  40. package/dist/js/node/base/index.js +63 -31
  41. package/dist/js/node/base/runJest.js +71 -99
  42. package/dist/js/node/base/utils.js +34 -66
  43. package/dist/js/node/cli/bff/app.js +22 -57
  44. package/dist/js/node/cli/bff/constant.js +7 -22
  45. package/dist/js/node/cli/bff/index.js +65 -121
  46. package/dist/js/node/cli/bff/mockAPI.js +71 -103
  47. package/dist/js/node/cli/bff/setup.js +38 -100
  48. package/dist/js/node/cli/bff/utils/index.js +22 -55
  49. package/dist/js/node/cli/index.js +46 -88
  50. package/dist/js/node/cli/test.js +25 -76
  51. package/dist/js/node/constant.js +7 -22
  52. package/dist/js/node/index.js +18 -19
  53. package/dist/js/node/runtime-testing/app.js +25 -53
  54. package/dist/js/node/runtime-testing/base.js +33 -30
  55. package/dist/js/node/runtime-testing/customRender.js +18 -49
  56. package/dist/js/node/runtime-testing/index.js +27 -18
  57. package/dist/js/node/runtime-testing/reduck.js +31 -67
  58. package/dist/js/node/runtime-testing/request.js +13 -32
  59. package/dist/js/node/runtime-testing/resolvePlugins.js +12 -41
  60. package/dist/js/treeshaking/base/config/index.js +31 -150
  61. package/dist/js/treeshaking/base/config/patches/assetsModule.js +11 -9
  62. package/dist/js/treeshaking/base/config/patches/filemock.js +1 -2
  63. package/dist/js/treeshaking/base/config/patches/index.js +64 -221
  64. package/dist/js/treeshaking/base/config/patches/transformer.js +37 -34
  65. package/dist/js/treeshaking/base/config/resolver.js +13 -36
  66. package/dist/js/treeshaking/base/config/testConfigOperator.js +69 -120
  67. package/dist/js/treeshaking/base/config/transformer/babelTransformer.js +10 -16
  68. package/dist/js/treeshaking/base/hook.js +7 -8
  69. package/dist/js/treeshaking/base/index.js +6 -7
  70. package/dist/js/treeshaking/base/runJest.js +164 -281
  71. package/dist/js/treeshaking/base/utils.js +48 -72
  72. package/dist/js/treeshaking/cli/bff/app.js +68 -192
  73. package/dist/js/treeshaking/cli/bff/constant.js +1 -2
  74. package/dist/js/treeshaking/cli/bff/index.js +130 -291
  75. package/dist/js/treeshaking/cli/bff/mockAPI.js +91 -181
  76. package/dist/js/treeshaking/cli/bff/setup.js +59 -185
  77. package/dist/js/treeshaking/cli/bff/utils/index.js +21 -47
  78. package/dist/js/treeshaking/cli/index.js +108 -249
  79. package/dist/js/treeshaking/cli/test.js +44 -177
  80. package/dist/js/treeshaking/constant.js +1 -2
  81. package/dist/js/treeshaking/index.js +3 -3
  82. package/dist/js/treeshaking/runtime-testing/app.js +39 -85
  83. package/dist/js/treeshaking/runtime-testing/base.js +3 -4
  84. package/dist/js/treeshaking/runtime-testing/customRender.js +10 -38
  85. package/dist/js/treeshaking/runtime-testing/index.js +1 -1
  86. package/dist/js/treeshaking/runtime-testing/reduck.js +33 -85
  87. package/dist/js/treeshaking/runtime-testing/request.js +19 -49
  88. package/dist/js/treeshaking/runtime-testing/resolvePlugins.js +14 -45
  89. package/dist/types/base/config/index.d.ts +1 -2
  90. package/dist/types/base/config/patches/assetsModule.d.ts +0 -1
  91. package/dist/types/base/config/patches/filemock.d.ts +0 -1
  92. package/dist/types/base/config/patches/transformer.d.ts +0 -1
  93. package/dist/types/base/config/testConfigOperator.d.ts +0 -2
  94. package/dist/types/base/hook.d.ts +1 -1
  95. package/dist/types/base/runJest.d.ts +1 -3
  96. package/dist/types/base/utils.d.ts +0 -1
  97. package/dist/types/cli/bff/index.d.ts +0 -2
  98. package/dist/types/cli/bff/mockAPI.d.ts +0 -1
  99. package/dist/types/cli/index.d.ts +0 -2
  100. package/dist/types/runtime-testing/app.d.ts +0 -3
  101. package/dist/types/runtime-testing/customRender.d.ts +0 -2
  102. package/package.json +13 -13
  103. package/dist/js/modern/modern-app.env.d.js +0 -0
  104. package/dist/js/node/modern-app.env.d.js +0 -0
  105. package/dist/js/treeshaking/modern-app.env.d.js +0 -0
package/CHANGELOG.md CHANGED
@@ -1,69 +1,70 @@
1
1
  # @modern-js/plugin-testing
2
2
 
3
- ## 0.0.0-next-20221207091327
3
+ ## 0.0.0-next-20221207112009
4
4
 
5
5
  ### Major Changes
6
6
 
7
- - dda38c9c3: chore: v2
7
+ - dda38c9c3e: chore: v2
8
8
 
9
9
  ### Patch Changes
10
10
 
11
- - cc971eabf: refactor: move server plugin load logic in `@modern-js/core`
11
+ - cc971eabfc: refactor: move server plugin load logic in `@modern-js/core`
12
12
  refactor:移除在 `@modern-js/core` 中的 server 插件加载逻辑
13
- - 14b712da8: fix: use consistent alias type and default value across packages
13
+ - 14b712da84: fix: use consistent alias type and default value across packages
14
14
 
15
15
  fix: 在各个包中使用一致的 alias 类型定义和默认值
16
16
 
17
- - Updated dependencies [2344eb26e]
18
- - Updated dependencies [a11fcf8b5]
19
- - Updated dependencies [e7ce0636d]
20
- - Updated dependencies [b18fa8f3e]
21
- - Updated dependencies [c9e800d39]
22
- - Updated dependencies [d032d49e0]
23
- - Updated dependencies [6aca87501]
24
- - Updated dependencies [fda836fe8]
25
- - Updated dependencies [15bf09d9c]
26
- - Updated dependencies [3e57f2bd5]
27
- - Updated dependencies [2e6031955]
28
- - Updated dependencies [fbf5eed5a]
29
- - Updated dependencies [a2509bfbd]
30
- - Updated dependencies [425e57092]
31
- - Updated dependencies [decfcd989]
32
- - Updated dependencies [e4357f185]
33
- - Updated dependencies [4369648ae]
34
- - Updated dependencies [92f0eade3]
35
- - Updated dependencies [92c099446]
36
- - Updated dependencies [edd1cfb1a]
37
- - Updated dependencies [cc971eabf]
38
- - Updated dependencies [5b9049f2e]
39
- - Updated dependencies [6bda14ed7]
40
- - Updated dependencies [92004d190]
41
- - Updated dependencies [b8bbe036c]
42
- - Updated dependencies [40ed5874c]
43
- - Updated dependencies [60d537863]
44
- - Updated dependencies [d5a31df78]
45
- - Updated dependencies [dda38c9c3]
46
- - Updated dependencies [102d32e4b]
47
- - Updated dependencies [8b8e1bb57]
48
- - Updated dependencies [3bbea92b2]
49
- - Updated dependencies [73cd29dd9]
50
- - Updated dependencies [b710adb84]
51
- - Updated dependencies [cce8ecee2]
52
- - Updated dependencies [18aaf4224]
53
- - Updated dependencies [34702d5d4]
54
- - Updated dependencies [f17974937]
55
- - Updated dependencies [fcace5b5b]
56
- - Updated dependencies [bbe4c4ab6]
57
- - Updated dependencies [e4558a0bc]
58
- - Updated dependencies [abf3421a7]
59
- - Updated dependencies [543be9558]
60
- - Updated dependencies [14b712da8]
61
- - @modern-js/runtime@0.0.0-next-20221207091327
62
- - @modern-js/prod-server@0.0.0-next-20221207091327
63
- - @modern-js/utils@0.0.0-next-20221207091327
64
- - @modern-js/babel-preset-app@0.0.0-next-20221207091327
65
- - @modern-js/plugin@0.0.0-next-20221207091327
66
- - @modern-js/babel-compiler@0.0.0-next-20221207091327
17
+ - Updated dependencies [2344eb26ed]
18
+ - Updated dependencies [a11fcf8b50]
19
+ - Updated dependencies [e7ce0636d1]
20
+ - Updated dependencies [b18fa8f3ed]
21
+ - Updated dependencies [c9e800d39a]
22
+ - Updated dependencies [d032d49e09]
23
+ - Updated dependencies [6aca875011]
24
+ - Updated dependencies [fda836fe8a]
25
+ - Updated dependencies [15bf09d9c8]
26
+ - Updated dependencies [3e57f2bd58]
27
+ - Updated dependencies [2e6031955e]
28
+ - Updated dependencies [fbf5eed5aa]
29
+ - Updated dependencies [a2509bfbdb]
30
+ - Updated dependencies [425e57092d]
31
+ - Updated dependencies [decfcd989d]
32
+ - Updated dependencies [e4357f1856]
33
+ - Updated dependencies [4369648ae2]
34
+ - Updated dependencies [92f0eade39]
35
+ - Updated dependencies [92c0994468]
36
+ - Updated dependencies [edd1cfb1af]
37
+ - Updated dependencies [cc971eabfc]
38
+ - Updated dependencies [5b9049f2e9]
39
+ - Updated dependencies [6bda14ed71]
40
+ - Updated dependencies [92004d1906]
41
+ - Updated dependencies [b8bbe036c7]
42
+ - Updated dependencies [40ed5874c6]
43
+ - Updated dependencies [60d5378632]
44
+ - Updated dependencies [d5a31df781]
45
+ - Updated dependencies [dda38c9c3e]
46
+ - Updated dependencies [102d32e4ba]
47
+ - Updated dependencies [8b8e1bb571]
48
+ - Updated dependencies [3bbea92b2a]
49
+ - Updated dependencies [73cd29dd9f]
50
+ - Updated dependencies [b710adb843]
51
+ - Updated dependencies [cce8ecee2d]
52
+ - Updated dependencies [18aaf42249]
53
+ - Updated dependencies [34702d5d47]
54
+ - Updated dependencies [f179749375]
55
+ - Updated dependencies [fcace5b5b9]
56
+ - Updated dependencies [bbe4c4ab64]
57
+ - Updated dependencies [e4558a0bc4]
58
+ - Updated dependencies [abf3421a75]
59
+ - Updated dependencies [543be9558e]
60
+ - Updated dependencies [fd1d9fd3d3]
61
+ - Updated dependencies [14b712da84]
62
+ - @modern-js/runtime@0.0.0-next-20221207112009
63
+ - @modern-js/prod-server@0.0.0-next-20221207112009
64
+ - @modern-js/utils@0.0.0-next-20221207112009
65
+ - @modern-js/babel-preset-app@0.0.0-next-20221207112009
66
+ - @modern-js/plugin@0.0.0-next-20221207112009
67
+ - @modern-js/babel-compiler@0.0.0-next-20221207112009
67
68
 
68
69
  ## 2.0.0-beta.2
69
70
 
@@ -1,36 +1,16 @@
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
1
  import { applyPatches } from "./patches";
22
2
  import { TestConfigOperator } from "./testConfigOperator";
23
- const getJestUtils = (testConfig) => {
3
+
4
+ /**
5
+ * Parse jest config
6
+ */
7
+ const getJestUtils = testConfig => {
24
8
  const testOperator = new TestConfigOperator(testConfig);
25
9
  return testOperator;
26
10
  };
27
- const patchConfig = (testOperator) => __async(void 0, null, function* () {
28
- yield applyPatches(testOperator);
11
+ const patchConfig = async testOperator => {
12
+ await applyPatches(testOperator);
29
13
  return testOperator.jestConfig;
30
- });
31
- const DEFAULT_RESOLVER_PATH = require.resolve("./resolver");
32
- export {
33
- DEFAULT_RESOLVER_PATH,
34
- getJestUtils,
35
- patchConfig
36
14
  };
15
+ export const DEFAULT_RESOLVER_PATH = require.resolve("./resolver");
16
+ export { getJestUtils, patchConfig };
@@ -1,11 +1,11 @@
1
- const patchAssetsModule = (testOperator) => {
1
+ /**
2
+ * Merge config from testConfig.jest
3
+ */
4
+ export const patchAssetsModule = testOperator => {
2
5
  testOperator.mergeJestConfig({
3
6
  moduleNameMapper: {
4
- "\\.(css|less|scss|sass)$": require.resolve("identity-obj-proxy"),
5
- "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": require.resolve("./filemock.js")
7
+ '\\.(css|less|scss|sass)$': require.resolve('identity-obj-proxy'),
8
+ '\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': require.resolve("./filemock.js")
6
9
  }
7
10
  });
8
- };
9
- export {
10
- patchAssetsModule
11
- };
11
+ };
@@ -1,4 +1 @@
1
- var filemock_default = "test-file-stub";
2
- export {
3
- filemock_default as default
4
- };
1
+ export default 'test-file-stub';
@@ -1,34 +1,11 @@
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
1
  import { patchTransformer } from "./transformer";
22
2
  import { patchAssetsModule } from "./assetsModule";
23
- const _applyPatches = (patches2, testOperator) => __async(void 0, null, function* () {
24
- for (const patch of patches2) {
25
- yield patch(testOperator);
3
+ const _applyPatches = async (patches, testOperator) => {
4
+ for (const patch of patches) {
5
+ await patch(testOperator);
26
6
  }
27
- });
28
- const patches = [patchTransformer, patchAssetsModule];
29
- const applyPatches = (testConfig) => __async(void 0, null, function* () {
30
- yield _applyPatches(patches, testConfig);
31
- });
32
- export {
33
- applyPatches
34
7
  };
8
+ const patches = [patchTransformer, patchAssetsModule];
9
+ export const applyPatches = async testConfig => {
10
+ await _applyPatches(patches, testConfig);
11
+ };
@@ -1,38 +1,43 @@
1
1
  import { readCompilerOptions } from "../../utils";
2
2
  const resolveTsCompilerOptions = () => {
3
3
  const tsCompilerOptions = readCompilerOptions() || {};
4
- const { jsx } = tsCompilerOptions;
4
+ const {
5
+ jsx
6
+ } = tsCompilerOptions;
5
7
  if (!jsx) {
6
8
  return null;
7
9
  }
8
- tsCompilerOptions.jsx = "react-jsx";
10
+ tsCompilerOptions.jsx = 'react-jsx';
9
11
  return tsCompilerOptions;
10
12
  };
11
- const patchTransformer = (testOperator) => {
12
- const { transformer } = testOperator.testConfig;
13
- if (transformer === "babel-jest") {
13
+
14
+ /**
15
+ * Map `TestConfig.transformer` to jest config
16
+ */
17
+ export const patchTransformer = testOperator => {
18
+ const {
19
+ transformer
20
+ } = testOperator.testConfig;
21
+ if (transformer === 'babel-jest') {
14
22
  testOperator.mergeJestConfig({
15
23
  transform: {
16
- "\\.[jt]sx?$": require.resolve("../transformer/babelTransformer")
24
+ '\\.[jt]sx?$': require.resolve("../transformer/babelTransformer")
17
25
  }
18
26
  });
19
27
  }
20
- if (transformer === "ts-jest") {
28
+ if (transformer === 'ts-jest') {
21
29
  testOperator.mergeJestConfig({
22
30
  transform: {
23
- "\\.[jt]sx?$": require.resolve("ts-jest")
31
+ '\\.[jt]sx?$': require.resolve('ts-jest')
24
32
  }
25
33
  });
26
34
  const compilerOptions = resolveTsCompilerOptions();
27
35
  compilerOptions && testOperator.mergeJestConfig({
28
36
  globals: {
29
- "ts-jest": {
37
+ 'ts-jest': {
30
38
  tsconfig: compilerOptions
31
39
  }
32
40
  }
33
41
  });
34
42
  }
35
- };
36
- export {
37
- patchTransformer
38
- };
43
+ };
@@ -1,21 +1,12 @@
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
- import enhanceResolve from "enhanced-resolve";
6
- var require_resolver = __commonJS({
7
- "src/base/config/resolver.ts"(exports, module) {
8
- const resolver = enhanceResolve.create.sync({
9
- conditionNames: ["require", "node", "default"],
10
- extensions: [".js", ".json", ".node", ".ts", ".tsx"]
11
- });
12
- const shouldResolveByEnhance = (url) => /^@[^/]+\/[^/]+\/.*/.test(url);
13
- module.exports = function(request, options) {
14
- if (shouldResolveByEnhance(request)) {
15
- return resolver(options.basedir, request);
16
- }
17
- return options.defaultResolver(request, options);
18
- };
19
- }
1
+ import enhanceResolve from 'enhanced-resolve';
2
+ const resolver = enhanceResolve.create.sync({
3
+ conditionNames: ['require', 'node', 'default'],
4
+ extensions: ['.js', '.json', '.node', '.ts', '.tsx']
20
5
  });
21
- export default require_resolver();
6
+ const shouldResolveByEnhance = url => /^@[^/]+\/[^/]+\/.*/.test(url);
7
+ module.exports = function (request, options) {
8
+ if (shouldResolveByEnhance(request)) {
9
+ return resolver(options.basedir, request);
10
+ }
11
+ return options.defaultResolver(request, options);
12
+ };
@@ -1,25 +1,15 @@
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 { merge } from "@modern-js/utils/lodash";
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 { merge } from '@modern-js/utils/lodash';
18
5
  class TestConfigOperator {
19
6
  constructor(testConfig) {
20
- this.defaultTestConfig = {
21
- transformer: "babel-jest"
22
- };
7
+ _defineProperty(this, "_testConfig", void 0);
8
+ _defineProperty(this, "_jestConfig", void 0);
9
+ _defineProperty(this, "userJestConfig", void 0);
10
+ _defineProperty(this, "defaultTestConfig", {
11
+ transformer: 'babel-jest'
12
+ });
23
13
  this._testConfig = testConfig;
24
14
  this._jestConfig = {};
25
15
  this.userJestConfig = testConfig.jest;
@@ -38,32 +28,36 @@ class TestConfigOperator {
38
28
  this._jestConfig = merge({}, this._jestConfig, sourceConfig);
39
29
  }
40
30
  setJestUserConfig() {
41
- const { userJestConfig } = this;
42
- if (typeof userJestConfig === "object") {
31
+ const {
32
+ userJestConfig
33
+ } = this;
34
+ if (typeof userJestConfig === 'object') {
43
35
  this.setJestConfig(userJestConfig);
44
36
  }
45
37
  }
46
38
  setJestConfig(sourceConfig, options) {
47
39
  if (options) {
48
- const { force } = options;
40
+ const {
41
+ force
42
+ } = options;
49
43
  if (force) {
50
44
  this._jestConfig = sourceConfig;
51
45
  return;
52
46
  }
53
47
  }
54
- this._jestConfig = __spreadValues(__spreadValues({}, this._jestConfig), sourceConfig);
48
+ this._jestConfig = _objectSpread(_objectSpread({}, this._jestConfig), sourceConfig);
55
49
  }
56
50
  getFinalConfig() {
57
- const { userJestConfig } = this;
51
+ const {
52
+ userJestConfig
53
+ } = this;
58
54
  if (!userJestConfig) {
59
55
  return this._jestConfig;
60
56
  }
61
- if (typeof userJestConfig === "function") {
57
+ if (typeof userJestConfig === 'function') {
62
58
  return userJestConfig(this._jestConfig);
63
59
  }
64
60
  return this.jestConfig;
65
61
  }
66
62
  }
67
- export {
68
- TestConfigOperator
69
- };
63
+ export { TestConfigOperator };
@@ -1,19 +1,11 @@
1
- var _a, _b;
2
- import babelJest from "babel-jest";
3
- const babelTransformer = (_b = (_a = babelJest).createTransformer) == null ? void 0 : _b.call(_a, {
4
- presets: [
5
- [
6
- require.resolve("@modern-js/babel-preset-app"),
7
- {
8
- appDirectory: process.cwd(),
9
- modules: "cjs"
10
- }
11
- ]
12
- ],
1
+ var _babelJest$createTran;
2
+ import babelJest from 'babel-jest';
3
+ const babelTransformer = (_babelJest$createTran = babelJest.createTransformer) === null || _babelJest$createTran === void 0 ? void 0 : _babelJest$createTran.call(babelJest, {
4
+ presets: [[require.resolve('@modern-js/babel-preset-app'), {
5
+ appDirectory: process.cwd(),
6
+ modules: 'cjs'
7
+ }]],
13
8
  configFile: false,
14
9
  babelrc: false
15
10
  });
16
- var babelTransformer_default = babelTransformer;
17
- export {
18
- babelTransformer_default as default
19
- };
11
+ export default babelTransformer;
@@ -1,12 +1,7 @@
1
- import { createAsyncPipeline, createParallelWorkflow } from "@modern-js/plugin";
2
- const jestConfigHook = createAsyncPipeline();
3
- const afterTestHook = createParallelWorkflow();
4
- const testingHooks = {
1
+ import { createAsyncPipeline, createParallelWorkflow } from '@modern-js/plugin';
2
+ export const jestConfigHook = createAsyncPipeline();
3
+ export const afterTestHook = createParallelWorkflow();
4
+ export const testingHooks = {
5
5
  jestConfig: createAsyncPipeline(),
6
6
  afterTest: createParallelWorkflow()
7
- };
8
- export {
9
- afterTestHook,
10
- jestConfigHook,
11
- testingHooks
12
- };
7
+ };
@@ -1,14 +1,6 @@
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
- };
1
+ export { runTest, runJest } from "./runJest";
2
+ export { getModuleNameMapper } from "./utils";
3
+ export { DEFAULT_RESOLVER_PATH } from "./config";
4
+ export { TestConfigOperator } from "./config/testConfigOperator";
5
+ export { testingHooks } from "./hook";
6
+ export * from "./types";
@@ -1,36 +1,24 @@
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";
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';
24
12
  import { getJestUtils, patchConfig } from "./config";
25
13
  import { debug } from "./utils";
26
- const buildArgv = (rawArgv, config) => __async(void 0, null, function* () {
27
- const argv = yield yargs(rawArgv).argv;
14
+ const buildArgv = async (rawArgv, config) => {
15
+ const argv = await yargs(rawArgv).argv;
28
16
  const result = {
29
17
  $0: argv.$0,
30
18
  _: argv._.slice(1)
31
19
  };
32
- Object.keys(argv).forEach((key) => {
33
- if (key.includes("-") || key === "_") {
20
+ Object.keys(argv).forEach(key => {
21
+ if (key.includes('-') || key === '_') {
34
22
  return;
35
23
  }
36
24
  result[key] = argv[key];
@@ -39,64 +27,65 @@ const buildArgv = (rawArgv, config) => __async(void 0, null, function* () {
39
27
  result.config = JSON.stringify(config);
40
28
  }
41
29
  return result;
42
- });
30
+ };
43
31
  const readResultsAndExit = (result, globalConfig) => {
44
32
  const code = !result || result.success ? 0 : globalConfig.testFailureExitCode;
45
- process.on("exit", () => {
46
- if (typeof code === "number" && code !== 0) {
33
+
34
+ // Only exit if needed
35
+ process.on('exit', () => {
36
+ if (typeof code === 'number' && code !== 0) {
47
37
  process.exitCode = code;
48
38
  }
49
39
  });
50
40
  if (globalConfig.forceExit) {
51
41
  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
- );
42
+ console.warn(`${chalk.bold('Force exiting Jest: ')}Have you considered using \`--detectOpenHandles\` to detect ` + `async operations that kept running after all tests finished?`);
57
43
  }
44
+
45
+ // eslint-disable-next-line no-process-exit
58
46
  process.exit(code);
59
47
  } else if (!globalConfig.detectOpenHandles) {
60
48
  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();
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();
69
51
  }
70
52
  };
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
- });
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
+ }
82
71
  }
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();
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
97
86
  });
98
- }
99
- export {
100
- runJest,
101
- runTest
102
- };
87
+ const finalConfig = testConfigOperator.getFinalConfig();
88
+ debug('Jest config:', finalConfig);
89
+ await runJest(finalConfig, pwd);
90
+ await hookRunners.afterTest();
91
+ }