@modern-js/plugin-testing 0.0.0-next-20221202071629 → 0.0.0-next-20221202125525

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 (104) hide show
  1. package/CHANGELOG.md +7 -8
  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 -25
  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 -25
  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 -54
  17. package/dist/js/modern/cli/bff/mockAPI.js +79 -66
  18. package/dist/js/modern/cli/bff/setup.js +71 -34
  19. package/dist/js/modern/cli/bff/utils/index.js +31 -17
  20. package/dist/js/modern/cli/index.js +65 -30
  21. package/dist/js/modern/cli/test.js +50 -16
  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 -16
  26. package/dist/js/modern/runtime-testing/base.js +7 -3
  27. package/dist/js/modern/runtime-testing/customRender.js +24 -12
  28. package/dist/js/modern/runtime-testing/index.js +1 -1
  29. package/dist/js/modern/runtime-testing/reduck.js +41 -21
  30. package/dist/js/modern/runtime-testing/request.js +4 -4
  31. package/dist/js/modern/runtime-testing/resolvePlugins.js +24 -10
  32. package/dist/js/node/base/config/index.js +56 -18
  33. package/dist/js/node/base/config/patches/assetsModule.js +30 -12
  34. package/dist/js/node/base/config/patches/filemock.js +22 -7
  35. package/dist/js/node/base/config/patches/index.js +50 -15
  36. package/dist/js/node/base/config/patches/transformer.js +38 -25
  37. package/dist/js/node/base/config/resolver.js +47 -13
  38. package/dist/js/node/base/config/testConfigOperator.js +49 -32
  39. package/dist/js/node/base/config/transformer/babelTransformer.js +40 -15
  40. package/dist/js/node/base/hook.js +28 -13
  41. package/dist/js/node/base/index.js +31 -63
  42. package/dist/js/node/base/runJest.js +99 -71
  43. package/dist/js/node/base/utils.js +66 -36
  44. package/dist/js/node/cli/bff/app.js +57 -22
  45. package/dist/js/node/cli/bff/constant.js +22 -7
  46. package/dist/js/node/cli/bff/index.js +121 -67
  47. package/dist/js/node/cli/bff/mockAPI.js +103 -73
  48. package/dist/js/node/cli/bff/setup.js +100 -38
  49. package/dist/js/node/cli/bff/utils/index.js +55 -24
  50. package/dist/js/node/cli/index.js +88 -46
  51. package/dist/js/node/cli/test.js +76 -25
  52. package/dist/js/node/constant.js +22 -7
  53. package/dist/js/node/index.js +19 -18
  54. package/dist/js/node/modern-app.env.d.js +0 -0
  55. package/dist/js/node/runtime-testing/app.js +53 -27
  56. package/dist/js/node/runtime-testing/base.js +30 -33
  57. package/dist/js/node/runtime-testing/customRender.js +49 -20
  58. package/dist/js/node/runtime-testing/index.js +18 -27
  59. package/dist/js/node/runtime-testing/reduck.js +67 -33
  60. package/dist/js/node/runtime-testing/request.js +32 -13
  61. package/dist/js/node/runtime-testing/resolvePlugins.js +41 -14
  62. package/dist/js/treeshaking/base/config/index.js +150 -31
  63. package/dist/js/treeshaking/base/config/patches/assetsModule.js +9 -11
  64. package/dist/js/treeshaking/base/config/patches/filemock.js +2 -1
  65. package/dist/js/treeshaking/base/config/patches/index.js +221 -64
  66. package/dist/js/treeshaking/base/config/patches/transformer.js +34 -37
  67. package/dist/js/treeshaking/base/config/resolver.js +36 -13
  68. package/dist/js/treeshaking/base/config/testConfigOperator.js +120 -69
  69. package/dist/js/treeshaking/base/config/transformer/babelTransformer.js +16 -10
  70. package/dist/js/treeshaking/base/hook.js +8 -7
  71. package/dist/js/treeshaking/base/index.js +7 -6
  72. package/dist/js/treeshaking/base/runJest.js +281 -164
  73. package/dist/js/treeshaking/base/utils.js +72 -48
  74. package/dist/js/treeshaking/cli/bff/app.js +192 -68
  75. package/dist/js/treeshaking/cli/bff/constant.js +2 -1
  76. package/dist/js/treeshaking/cli/bff/index.js +291 -130
  77. package/dist/js/treeshaking/cli/bff/mockAPI.js +181 -91
  78. package/dist/js/treeshaking/cli/bff/setup.js +185 -59
  79. package/dist/js/treeshaking/cli/bff/utils/index.js +47 -21
  80. package/dist/js/treeshaking/cli/index.js +249 -108
  81. package/dist/js/treeshaking/cli/test.js +177 -44
  82. package/dist/js/treeshaking/constant.js +2 -1
  83. package/dist/js/treeshaking/index.js +3 -3
  84. package/dist/js/treeshaking/modern-app.env.d.js +0 -0
  85. package/dist/js/treeshaking/runtime-testing/app.js +85 -39
  86. package/dist/js/treeshaking/runtime-testing/base.js +4 -3
  87. package/dist/js/treeshaking/runtime-testing/customRender.js +38 -10
  88. package/dist/js/treeshaking/runtime-testing/index.js +1 -1
  89. package/dist/js/treeshaking/runtime-testing/reduck.js +85 -33
  90. package/dist/js/treeshaking/runtime-testing/request.js +49 -19
  91. package/dist/js/treeshaking/runtime-testing/resolvePlugins.js +45 -14
  92. package/dist/types/base/config/index.d.ts +1 -0
  93. package/dist/types/base/config/patches/assetsModule.d.ts +1 -0
  94. package/dist/types/base/config/patches/filemock.d.ts +1 -0
  95. package/dist/types/base/config/patches/transformer.d.ts +1 -0
  96. package/dist/types/base/config/testConfigOperator.d.ts +2 -0
  97. package/dist/types/base/runJest.d.ts +2 -0
  98. package/dist/types/base/utils.d.ts +1 -0
  99. package/dist/types/cli/bff/index.d.ts +2 -0
  100. package/dist/types/cli/bff/mockAPI.d.ts +1 -0
  101. package/dist/types/cli/index.d.ts +2 -0
  102. package/dist/types/runtime-testing/app.d.ts +3 -0
  103. package/dist/types/runtime-testing/customRender.d.ts +2 -0
  104. package/package.json +13 -13
package/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @modern-js/plugin-testing
2
2
 
3
- ## 0.0.0-next-20221202071629
3
+ ## 0.0.0-next-20221202125525
4
4
 
5
5
  ### Major Changes
6
6
 
@@ -43,19 +43,18 @@
43
43
  - Updated dependencies [3bbea92b2]
44
44
  - Updated dependencies [73cd29dd9]
45
45
  - Updated dependencies [b710adb84]
46
- - Updated dependencies [cce8ecee2]
47
46
  - Updated dependencies [18aaf4224]
48
47
  - Updated dependencies [f17974937]
49
48
  - Updated dependencies [fcace5b5b]
50
49
  - Updated dependencies [abf3421a7]
51
50
  - Updated dependencies [543be9558]
52
51
  - Updated dependencies [14b712da8]
53
- - @modern-js/runtime@0.0.0-next-20221202071629
54
- - @modern-js/prod-server@0.0.0-next-20221202071629
55
- - @modern-js/babel-preset-app@0.0.0-next-20221202071629
56
- - @modern-js/utils@0.0.0-next-20221202071629
57
- - @modern-js/plugin@0.0.0-next-20221202071629
58
- - @modern-js/babel-compiler@0.0.0-next-20221202071629
52
+ - @modern-js/runtime@0.0.0-next-20221202125525
53
+ - @modern-js/prod-server@0.0.0-next-20221202125525
54
+ - @modern-js/babel-preset-app@0.0.0-next-20221202125525
55
+ - @modern-js/utils@0.0.0-next-20221202125525
56
+ - @modern-js/plugin@0.0.0-next-20221202125525
57
+ - @modern-js/babel-compiler@0.0.0-next-20221202125525
59
58
 
60
59
  ## 2.0.0-beta.2
61
60
 
@@ -1,16 +1,36 @@
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
+ };
1
21
  import { applyPatches } from "./patches";
2
22
  import { TestConfigOperator } from "./testConfigOperator";
3
-
4
- /**
5
- * Parse jest config
6
- */
7
- const getJestUtils = testConfig => {
23
+ const getJestUtils = (testConfig) => {
8
24
  const testOperator = new TestConfigOperator(testConfig);
9
25
  return testOperator;
10
26
  };
11
- const patchConfig = async testOperator => {
12
- await applyPatches(testOperator);
27
+ const patchConfig = (testOperator) => __async(void 0, null, function* () {
28
+ yield applyPatches(testOperator);
13
29
  return testOperator.jestConfig;
30
+ });
31
+ const DEFAULT_RESOLVER_PATH = require.resolve("./resolver");
32
+ export {
33
+ DEFAULT_RESOLVER_PATH,
34
+ getJestUtils,
35
+ patchConfig
14
36
  };
15
- export const DEFAULT_RESOLVER_PATH = require.resolve("./resolver");
16
- export { getJestUtils, patchConfig };
@@ -1,11 +1,11 @@
1
- /**
2
- * Merge config from testConfig.jest
3
- */
4
- export const patchAssetsModule = testOperator => {
1
+ const patchAssetsModule = (testOperator) => {
5
2
  testOperator.mergeJestConfig({
6
3
  moduleNameMapper: {
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")
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")
9
6
  }
10
7
  });
11
- };
8
+ };
9
+ export {
10
+ patchAssetsModule
11
+ };
@@ -1 +1,4 @@
1
- export default 'test-file-stub';
1
+ var filemock_default = "test-file-stub";
2
+ export {
3
+ filemock_default as default
4
+ };
@@ -1,11 +1,34 @@
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
+ };
1
21
  import { patchTransformer } from "./transformer";
2
22
  import { patchAssetsModule } from "./assetsModule";
3
- const _applyPatches = async (patches, testOperator) => {
4
- for (const patch of patches) {
5
- await patch(testOperator);
23
+ const _applyPatches = (patches2, testOperator) => __async(void 0, null, function* () {
24
+ for (const patch of patches2) {
25
+ yield patch(testOperator);
6
26
  }
7
- };
27
+ });
8
28
  const patches = [patchTransformer, patchAssetsModule];
9
- export const applyPatches = async testConfig => {
10
- await _applyPatches(patches, testConfig);
11
- };
29
+ const applyPatches = (testConfig) => __async(void 0, null, function* () {
30
+ yield _applyPatches(patches, testConfig);
31
+ });
32
+ export {
33
+ applyPatches
34
+ };
@@ -1,43 +1,38 @@
1
1
  import { readCompilerOptions } from "../../utils";
2
2
  const resolveTsCompilerOptions = () => {
3
3
  const tsCompilerOptions = readCompilerOptions() || {};
4
- const {
5
- jsx
6
- } = tsCompilerOptions;
4
+ const { jsx } = tsCompilerOptions;
7
5
  if (!jsx) {
8
6
  return null;
9
7
  }
10
- tsCompilerOptions.jsx = 'react-jsx';
8
+ tsCompilerOptions.jsx = "react-jsx";
11
9
  return tsCompilerOptions;
12
10
  };
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') {
11
+ const patchTransformer = (testOperator) => {
12
+ const { transformer } = testOperator.testConfig;
13
+ if (transformer === "babel-jest") {
22
14
  testOperator.mergeJestConfig({
23
15
  transform: {
24
- '\\.[jt]sx?$': require.resolve("../transformer/babelTransformer")
16
+ "\\.[jt]sx?$": require.resolve("../transformer/babelTransformer")
25
17
  }
26
18
  });
27
19
  }
28
- if (transformer === 'ts-jest') {
20
+ if (transformer === "ts-jest") {
29
21
  testOperator.mergeJestConfig({
30
22
  transform: {
31
- '\\.[jt]sx?$': require.resolve('ts-jest')
23
+ "\\.[jt]sx?$": require.resolve("ts-jest")
32
24
  }
33
25
  });
34
26
  const compilerOptions = resolveTsCompilerOptions();
35
27
  compilerOptions && testOperator.mergeJestConfig({
36
28
  globals: {
37
- 'ts-jest': {
29
+ "ts-jest": {
38
30
  tsconfig: compilerOptions
39
31
  }
40
32
  }
41
33
  });
42
34
  }
43
- };
35
+ };
36
+ export {
37
+ patchTransformer
38
+ };
@@ -1,12 +1,21 @@
1
- import enhanceResolve from 'enhanced-resolve';
2
- const resolver = enhanceResolve.create.sync({
3
- conditionNames: ['require', 'node', 'default'],
4
- extensions: ['.js', '.json', '.node', '.ts', '.tsx']
5
- });
6
- const shouldResolveByEnhance = url => /^@[^/]+\/[^/]+\/.*/.test(url);
7
- module.exports = function (request, options) {
8
- if (shouldResolveByEnhance(request)) {
9
- return resolver(options.basedir, request);
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
+ };
10
19
  }
11
- return options.defaultResolver(request, options);
12
- };
20
+ });
21
+ export default require_resolver();
@@ -1,17 +1,25 @@
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) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
4
- function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
5
- function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
6
- import { merge } from '@modern-js/utils/lodash';
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";
7
18
  class TestConfigOperator {
8
19
  constructor(testConfig) {
9
- _defineProperty(this, "_testConfig", void 0);
10
- _defineProperty(this, "_jestConfig", void 0);
11
- _defineProperty(this, "userJestConfig", void 0);
12
- _defineProperty(this, "defaultTestConfig", {
13
- transformer: 'babel-jest'
14
- });
20
+ this.defaultTestConfig = {
21
+ transformer: "babel-jest"
22
+ };
15
23
  this._testConfig = testConfig;
16
24
  this._jestConfig = {};
17
25
  this.userJestConfig = testConfig.jest;
@@ -30,36 +38,32 @@ class TestConfigOperator {
30
38
  this._jestConfig = merge({}, this._jestConfig, sourceConfig);
31
39
  }
32
40
  setJestUserConfig() {
33
- const {
34
- userJestConfig
35
- } = this;
36
- if (typeof userJestConfig === 'object') {
41
+ const { userJestConfig } = this;
42
+ if (typeof userJestConfig === "object") {
37
43
  this.setJestConfig(userJestConfig);
38
44
  }
39
45
  }
40
46
  setJestConfig(sourceConfig, options) {
41
47
  if (options) {
42
- const {
43
- force
44
- } = options;
48
+ const { force } = options;
45
49
  if (force) {
46
50
  this._jestConfig = sourceConfig;
47
51
  return;
48
52
  }
49
53
  }
50
- this._jestConfig = _objectSpread(_objectSpread({}, this._jestConfig), sourceConfig);
54
+ this._jestConfig = __spreadValues(__spreadValues({}, this._jestConfig), sourceConfig);
51
55
  }
52
56
  getFinalConfig() {
53
- const {
54
- userJestConfig
55
- } = this;
57
+ const { userJestConfig } = this;
56
58
  if (!userJestConfig) {
57
59
  return this._jestConfig;
58
60
  }
59
- if (typeof userJestConfig === 'function') {
61
+ if (typeof userJestConfig === "function") {
60
62
  return userJestConfig(this._jestConfig);
61
63
  }
62
64
  return this.jestConfig;
63
65
  }
64
66
  }
65
- export { TestConfigOperator };
67
+ export {
68
+ TestConfigOperator
69
+ };
@@ -1,11 +1,19 @@
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
- }]],
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
+ ],
8
13
  configFile: false,
9
14
  babelrc: false
10
15
  });
11
- export default babelTransformer;
16
+ var babelTransformer_default = babelTransformer;
17
+ export {
18
+ babelTransformer_default as default
19
+ };
@@ -1,7 +1,12 @@
1
- import { createAsyncPipeline, createParallelWorkflow } from '@modern-js/plugin';
2
- export const jestConfigHook = createAsyncPipeline();
3
- export const afterTestHook = createParallelWorkflow();
4
- export const testingHooks = {
1
+ import { createAsyncPipeline, createParallelWorkflow } from "@modern-js/plugin";
2
+ const jestConfigHook = createAsyncPipeline();
3
+ const afterTestHook = createParallelWorkflow();
4
+ const testingHooks = {
5
5
  jestConfig: createAsyncPipeline(),
6
6
  afterTest: createParallelWorkflow()
7
- };
7
+ };
8
+ export {
9
+ afterTestHook,
10
+ jestConfigHook,
11
+ testingHooks
12
+ };
@@ -1,6 +1,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
+ 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,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,19 +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) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
4
- function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
5
- function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
6
- import fs from 'fs';
7
- import path from 'path';
8
- import { createDebugger } from '@modern-js/utils';
9
- export const debug = createDebugger('test');
10
- /**
11
- * Read `compilerOptions` in the current pwd's tsconfig.json file
12
- */
13
- 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") => {
14
22
  let tsConfig = {};
15
23
  let extendedCompilerOptions = {};
16
- let tsconfigFile = '';
24
+ let tsconfigFile = "";
17
25
  try {
18
26
  const maybeTsconfigFile = path.join(pwd, filename);
19
27
  if (fs.existsSync(maybeTsconfigFile)) {
@@ -21,23 +29,25 @@ export const readCompilerOptions = (pwd = process.cwd(), filename = 'tsconfig.js
21
29
  } else {
22
30
  tsconfigFile = require.resolve(filename);
23
31
  }
24
- ({
25
- config: tsConfig
26
- } = require('typescript').parseConfigFileTextToJson(tsconfigFile, fs.readFileSync(tsconfigFile, 'utf8')));
32
+ ({ config: tsConfig } = require("typescript").parseConfigFileTextToJson(
33
+ tsconfigFile,
34
+ fs.readFileSync(tsconfigFile, "utf8")
35
+ ));
27
36
  } catch (e) {
28
37
  return {};
29
38
  }
30
39
  if (tsConfig.extends) {
31
- extendedCompilerOptions = readCompilerOptions(path.dirname(tsconfigFile), tsConfig.extends);
40
+ extendedCompilerOptions = readCompilerOptions(
41
+ path.dirname(tsconfigFile),
42
+ tsConfig.extends
43
+ );
32
44
  }
33
- return _objectSpread(_objectSpread({}, extendedCompilerOptions), tsConfig.compilerOptions);
45
+ return __spreadValues(__spreadValues({}, extendedCompilerOptions), tsConfig.compilerOptions);
34
46
  };
35
- export const getModuleNameMapper = alias => Object.keys(alias).reduce((memo, cur) => {
47
+ const getModuleNameMapper = (alias) => Object.keys(alias).reduce((memo, cur) => {
36
48
  const aliasValue = Array.isArray(alias[cur]) ? alias[cur] : [alias[cur]];
37
- const isFile = aliasValue.some(s => s.endsWith('.js'));
38
-
39
- // It's special for if using @modern-js/runtime alias other module @modern-js/runtime/model would not work.
40
- if (cur === '@modern-js/runtime$') {
49
+ const isFile = aliasValue.some((s) => s.endsWith(".js"));
50
+ if (cur === "@modern-js/runtime$") {
41
51
  memo[`.+${cur}`] = aliasValue[0];
42
52
  return memo;
43
53
  }
@@ -48,4 +58,9 @@ export const getModuleNameMapper = alias => Object.keys(alias).reduce((memo, cur
48
58
  const value = path.normalize(`${aliasValue}/$1`);
49
59
  memo[key] = value;
50
60
  return memo;
51
- }, {});
61
+ }, {});
62
+ export {
63
+ debug,
64
+ getModuleNameMapper,
65
+ readCompilerOptions
66
+ };